| <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>IniSecurityManagerFactory.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Apache Shiro :: Core</a> > <a href="index.source.html" class="el_package">org.apache.shiro.config</a> > <span class="el_source">IniSecurityManagerFactory.java</span></div><h1>IniSecurityManagerFactory.java</h1><pre class="source lang-java linenums">/* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| package org.apache.shiro.config; |
| |
| import org.apache.shiro.mgt.DefaultSecurityManager; |
| import org.apache.shiro.mgt.RealmSecurityManager; |
| import org.apache.shiro.mgt.SecurityManager; |
| import org.apache.shiro.realm.Realm; |
| import org.apache.shiro.realm.RealmFactory; |
| import org.apache.shiro.realm.text.IniRealm; |
| import org.apache.shiro.util.CollectionUtils; |
| import org.apache.shiro.util.Factory; |
| import org.apache.shiro.util.LifecycleUtils; |
| import org.apache.shiro.util.Nameable; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| import java.util.ArrayList; |
| import java.util.Collection; |
| import java.util.Collections; |
| import java.util.LinkedHashMap; |
| import java.util.List; |
| import java.util.Map; |
| |
| /** |
| * A {@link Factory} that creates {@link SecurityManager} instances based on {@link Ini} configuration. |
| * |
| * @since 1.0 |
| * @deprecated use Shiro's {@code Environment} mechanisms instead. |
| */ |
| @Deprecated |
| public class IniSecurityManagerFactory extends IniFactorySupport<SecurityManager> { |
| |
| public static final String MAIN_SECTION_NAME = "main"; |
| |
| public static final String SECURITY_MANAGER_NAME = "securityManager"; |
| public static final String INI_REALM_NAME = "iniRealm"; |
| |
| <span class="fc" id="L55"> private static transient final Logger log = LoggerFactory.getLogger(IniSecurityManagerFactory.class);</span> |
| |
| private ReflectionBuilder builder; |
| |
| /** |
| * Creates a new instance. See the {@link #getInstance()} JavaDoc for detailed explanation of how an INI |
| * source will be resolved to use to build the instance. |
| */ |
| <span class="fc" id="L63"> public IniSecurityManagerFactory() {</span> |
| <span class="fc" id="L64"> this.builder = new ReflectionBuilder();</span> |
| <span class="fc" id="L65"> }</span> |
| |
| public IniSecurityManagerFactory(Ini config) { |
| <span class="fc" id="L68"> this();</span> |
| <span class="fc" id="L69"> setIni(config);</span> |
| <span class="fc" id="L70"> }</span> |
| |
| public IniSecurityManagerFactory(String iniResourcePath) { |
| <span class="fc" id="L73"> this(Ini.fromResourcePath(iniResourcePath));</span> |
| <span class="fc" id="L74"> }</span> |
| |
| public Map<String, ?> getBeans() { |
| <span class="nc bnc" id="L77" title="All 2 branches missed."> return this.builder != null ? Collections.unmodifiableMap(builder.getObjects()) : null;</span> |
| } |
| |
| public void destroy() { |
| <span class="nc bnc" id="L81" title="All 2 branches missed."> if(getReflectionBuilder() != null) {</span> |
| <span class="nc" id="L82"> getReflectionBuilder().destroy();</span> |
| } |
| <span class="nc" id="L84"> }</span> |
| |
| private SecurityManager getSecurityManagerBean() { |
| <span class="fc" id="L87"> return getReflectionBuilder().getBean(SECURITY_MANAGER_NAME, SecurityManager.class);</span> |
| } |
| |
| protected SecurityManager createDefaultInstance() { |
| <span class="fc" id="L91"> return new DefaultSecurityManager();</span> |
| } |
| |
| protected SecurityManager createInstance(Ini ini) { |
| <span class="pc bpc" id="L95" title="1 of 2 branches missed."> if (CollectionUtils.isEmpty(ini)) {</span> |
| <span class="nc" id="L96"> throw new NullPointerException("Ini argument cannot be null or empty.");</span> |
| } |
| <span class="fc" id="L98"> SecurityManager securityManager = createSecurityManager(ini);</span> |
| <span class="pc bpc" id="L99" title="1 of 2 branches missed."> if (securityManager == null) {</span> |
| <span class="nc" id="L100"> String msg = SecurityManager.class + " instance cannot be null.";</span> |
| <span class="nc" id="L101"> throw new ConfigurationException(msg);</span> |
| } |
| <span class="fc" id="L103"> return securityManager;</span> |
| } |
| |
| private SecurityManager createSecurityManager(Ini ini) { |
| <span class="fc" id="L107"> return createSecurityManager(ini, getConfigSection(ini));</span> |
| } |
| |
| private Ini.Section getConfigSection(Ini ini) { |
| |
| <span class="fc" id="L112"> Ini.Section mainSection = ini.getSection(MAIN_SECTION_NAME);</span> |
| <span class="fc bfc" id="L113" title="All 2 branches covered."> if (CollectionUtils.isEmpty(mainSection)) {</span> |
| //try the default: |
| <span class="fc" id="L115"> mainSection = ini.getSection(Ini.DEFAULT_SECTION_NAME);</span> |
| } |
| <span class="fc" id="L117"> return mainSection;</span> |
| } |
| |
| protected boolean isAutoApplyRealms(SecurityManager securityManager) { |
| <span class="fc" id="L121"> boolean autoApply = true;</span> |
| <span class="pc bpc" id="L122" title="1 of 2 branches missed."> if (securityManager instanceof RealmSecurityManager) {</span> |
| //only apply realms if they haven't been explicitly set by the user: |
| <span class="fc" id="L124"> RealmSecurityManager realmSecurityManager = (RealmSecurityManager) securityManager;</span> |
| <span class="fc" id="L125"> Collection<Realm> realms = realmSecurityManager.getRealms();</span> |
| <span class="fc bfc" id="L126" title="All 2 branches covered."> if (!CollectionUtils.isEmpty(realms)) {</span> |
| <span class="fc" id="L127"> log.info("Realms have been explicitly set on the SecurityManager instance - auto-setting of " +</span> |
| "realms will not occur."); |
| <span class="fc" id="L129"> autoApply = false;</span> |
| } |
| } |
| <span class="fc" id="L132"> return autoApply;</span> |
| } |
| |
| @SuppressWarnings({"unchecked"}) |
| private SecurityManager createSecurityManager(Ini ini, Ini.Section mainSection) { |
| |
| <span class="fc" id="L138"> getReflectionBuilder().setObjects(createDefaults(ini, mainSection));</span> |
| <span class="fc" id="L139"> Map<String, ?> objects = buildInstances(mainSection);</span> |
| |
| <span class="fc" id="L141"> SecurityManager securityManager = getSecurityManagerBean();</span> |
| |
| <span class="fc" id="L143"> boolean autoApplyRealms = isAutoApplyRealms(securityManager);</span> |
| |
| <span class="fc bfc" id="L145" title="All 2 branches covered."> if (autoApplyRealms) {</span> |
| //realms and realm factory might have been created - pull them out first so we can |
| //initialize the securityManager: |
| <span class="fc" id="L148"> Collection<Realm> realms = getRealms(objects);</span> |
| //set them on the SecurityManager |
| <span class="fc bfc" id="L150" title="All 2 branches covered."> if (!CollectionUtils.isEmpty(realms)) {</span> |
| <span class="fc" id="L151"> applyRealmsToSecurityManager(realms, securityManager);</span> |
| } |
| } |
| |
| <span class="fc" id="L155"> return securityManager;</span> |
| } |
| |
| protected Map<String, ?> createDefaults(Ini ini, Ini.Section mainSection) { |
| <span class="fc" id="L159"> Map<String, Object> defaults = new LinkedHashMap<String, Object>();</span> |
| |
| <span class="fc" id="L161"> SecurityManager securityManager = createDefaultInstance();</span> |
| <span class="fc" id="L162"> defaults.put(SECURITY_MANAGER_NAME, securityManager);</span> |
| |
| <span class="fc bfc" id="L164" title="All 2 branches covered."> if (shouldImplicitlyCreateRealm(ini)) {</span> |
| <span class="fc" id="L165"> Realm realm = createRealm(ini);</span> |
| <span class="pc bpc" id="L166" title="1 of 2 branches missed."> if (realm != null) {</span> |
| <span class="fc" id="L167"> defaults.put(INI_REALM_NAME, realm);</span> |
| } |
| } |
| |
| // The values from 'getDefaults()' will override the above. |
| <span class="fc" id="L172"> Map<String, ?> defaultBeans = getDefaults();</span> |
| <span class="pc bpc" id="L173" title="1 of 2 branches missed."> if (!CollectionUtils.isEmpty(defaultBeans)) {</span> |
| <span class="nc" id="L174"> defaults.putAll(defaultBeans);</span> |
| } |
| |
| <span class="fc" id="L177"> return defaults;</span> |
| } |
| |
| private Map<String, ?> buildInstances(Ini.Section section) { |
| <span class="fc" id="L181"> return getReflectionBuilder().buildObjects(section);</span> |
| } |
| |
| private void addToRealms(Collection<Realm> realms, RealmFactory factory) { |
| <span class="nc" id="L185"> LifecycleUtils.init(factory);</span> |
| <span class="nc" id="L186"> Collection<Realm> factoryRealms = factory.getRealms();</span> |
| //SHIRO-238: check factoryRealms (was 'realms'): |
| <span class="nc bnc" id="L188" title="All 2 branches missed."> if (!CollectionUtils.isEmpty(factoryRealms)) {</span> |
| <span class="nc" id="L189"> realms.addAll(factoryRealms);</span> |
| } |
| <span class="nc" id="L191"> }</span> |
| |
| private Collection<Realm> getRealms(Map<String, ?> instances) { |
| |
| //realms and realm factory might have been created - pull them out first so we can |
| //initialize the securityManager: |
| <span class="fc" id="L197"> List<Realm> realms = new ArrayList<Realm>();</span> |
| |
| //iterate over the map entries to pull out the realm factory(s): |
| <span class="fc bfc" id="L200" title="All 2 branches covered."> for (Map.Entry<String, ?> entry : instances.entrySet()) {</span> |
| |
| <span class="fc" id="L202"> String name = entry.getKey();</span> |
| <span class="fc" id="L203"> Object value = entry.getValue();</span> |
| |
| <span class="pc bpc" id="L205" title="1 of 2 branches missed."> if (value instanceof RealmFactory) {</span> |
| <span class="nc" id="L206"> addToRealms(realms, (RealmFactory) value);</span> |
| <span class="fc bfc" id="L207" title="All 2 branches covered."> } else if (value instanceof Realm) {</span> |
| <span class="fc" id="L208"> Realm realm = (Realm) value;</span> |
| //set the name if null: |
| <span class="fc" id="L210"> String existingName = realm.getName();</span> |
| <span class="pc bpc" id="L211" title="2 of 4 branches missed."> if (existingName == null || existingName.startsWith(realm.getClass().getName())) {</span> |
| <span class="nc bnc" id="L212" title="All 2 branches missed."> if (realm instanceof Nameable) {</span> |
| <span class="nc" id="L213"> ((Nameable) realm).setName(name);</span> |
| <span class="nc" id="L214"> log.debug("Applied name '{}' to Nameable realm instance {}", name, realm);</span> |
| } else { |
| <span class="nc" id="L216"> log.info("Realm does not implement the {} interface. Configured name will not be applied.",</span> |
| <span class="nc" id="L217"> Nameable.class.getName());</span> |
| } |
| } |
| <span class="fc" id="L220"> realms.add(realm);</span> |
| } |
| <span class="fc" id="L222"> }</span> |
| |
| <span class="fc" id="L224"> return realms;</span> |
| } |
| |
| private void assertRealmSecurityManager(SecurityManager securityManager) { |
| <span class="pc bpc" id="L228" title="1 of 2 branches missed."> if (securityManager == null) {</span> |
| <span class="nc" id="L229"> throw new NullPointerException("securityManager instance cannot be null");</span> |
| } |
| <span class="pc bpc" id="L231" title="1 of 2 branches missed."> if (!(securityManager instanceof RealmSecurityManager)) {</span> |
| <span class="nc" id="L232"> String msg = "securityManager instance is not a " + RealmSecurityManager.class.getName() +</span> |
| " instance. This is required to access or configure realms on the instance."; |
| <span class="nc" id="L234"> throw new ConfigurationException(msg);</span> |
| } |
| <span class="fc" id="L236"> }</span> |
| |
| protected void applyRealmsToSecurityManager(Collection<Realm> realms, SecurityManager securityManager) { |
| <span class="fc" id="L239"> assertRealmSecurityManager(securityManager);</span> |
| <span class="fc" id="L240"> ((RealmSecurityManager) securityManager).setRealms(realms);</span> |
| <span class="fc" id="L241"> }</span> |
| |
| /** |
| * Returns {@code true} if the Ini contains account data and a {@code Realm} should be implicitly |
| * {@link #createRealm(Ini) created} to reflect the account data, {@code false} if no realm should be implicitly |
| * created. |
| * |
| * @param ini the Ini instance to inspect for account data resulting in an implicitly created realm. |
| * @return {@code true} if the Ini contains account data and a {@code Realm} should be implicitly |
| * {@link #createRealm(Ini) created} to reflect the account data, {@code false} if no realm should be |
| * implicitly created. |
| */ |
| protected boolean shouldImplicitlyCreateRealm(Ini ini) { |
| <span class="pc bpc" id="L254" title="1 of 2 branches missed."> return !CollectionUtils.isEmpty(ini) &&</span> |
| <span class="fc bfc" id="L255" title="All 2 branches covered."> (!CollectionUtils.isEmpty(ini.getSection(IniRealm.ROLES_SECTION_NAME)) ||</span> |
| <span class="fc bfc" id="L256" title="All 2 branches covered."> !CollectionUtils.isEmpty(ini.getSection(IniRealm.USERS_SECTION_NAME)));</span> |
| } |
| |
| /** |
| * Creates a {@code Realm} from the Ini instance containing account data. |
| * |
| * @param ini the Ini instance from which to acquire the account data. |
| * @return a new Realm instance reflecting the account data discovered in the {@code Ini}. |
| */ |
| protected Realm createRealm(Ini ini) { |
| //IniRealm realm = new IniRealm(ini); changed to support SHIRO-322 |
| <span class="fc" id="L267"> IniRealm realm = new IniRealm();</span> |
| <span class="fc" id="L268"> realm.setName(INI_REALM_NAME);</span> |
| <span class="fc" id="L269"> realm.setIni(ini); //added for SHIRO-322</span> |
| <span class="fc" id="L270"> return realm;</span> |
| } |
| |
| /** |
| * Returns the ReflectionBuilder instance used to create SecurityManagers object graph. |
| * @return ReflectionBuilder instance used to create SecurityManagers object graph. |
| * @since 1.4 |
| */ |
| public ReflectionBuilder getReflectionBuilder() { |
| <span class="fc" id="L279"> return builder;</span> |
| } |
| |
| /** |
| * Sets the ReflectionBuilder that will be used to create the SecurityManager based on the contents of |
| * the Ini configuration. |
| * @param builder The ReflectionBuilder used to parse the Ini configuration. |
| * @since 1.4 |
| */ |
| @SuppressWarnings("unused") |
| public void setReflectionBuilder(ReflectionBuilder builder) { |
| <span class="nc" id="L290"> this.builder = builder;</span> |
| <span class="nc" id="L291"> }</span> |
| } |
| </pre><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.7.7.201606060606</span></div></body></html> |