blob: 285862e45ec400f70e22a1b437591e9494dbb62c [file] [log] [blame]
<?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 :: Jar Bundle</a> &gt; <a href="../index.html" class="el_bundle">shiro-core</a> &gt; <a href="index.source.html" class="el_package">org.apache.shiro.config</a> &gt; <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
* &quot;License&quot;); 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
* &quot;AS IS&quot; 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
*/
public class IniSecurityManagerFactory extends IniFactorySupport&lt;SecurityManager&gt; {
public static final String MAIN_SECTION_NAME = &quot;main&quot;;
public static final String SECURITY_MANAGER_NAME = &quot;securityManager&quot;;
public static final String INI_REALM_NAME = &quot;iniRealm&quot;;
<span class="fc" id="L53"> 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="L61"> public IniSecurityManagerFactory() {</span>
<span class="fc" id="L62"> }</span>
<span class="fc" id="L64"> public IniSecurityManagerFactory(Ini config) {</span>
<span class="fc" id="L65"> setIni(config);</span>
<span class="fc" id="L66"> }</span>
public IniSecurityManagerFactory(String iniResourcePath) {
<span class="fc" id="L69"> this(Ini.fromResourcePath(iniResourcePath));</span>
<span class="fc" id="L70"> }</span>
public Map&lt;String, ?&gt; getBeans() {
<span class="fc bfc" id="L73" title="All 2 branches covered."> return this.builder != null ? Collections.unmodifiableMap(builder.getObjects()) : null;</span>
}
public void destroy() {
<span class="nc bnc" id="L77" title="All 2 branches missed."> if(this.builder != null) {</span>
<span class="nc" id="L78"> builder.destroy();</span>
}
<span class="nc" id="L80"> }</span>
private SecurityManager getSecurityManagerBean() {
<span class="fc" id="L83"> return builder.getBean(SECURITY_MANAGER_NAME, SecurityManager.class);</span>
}
protected SecurityManager createDefaultInstance() {
<span class="fc" id="L87"> return new DefaultSecurityManager();</span>
}
protected SecurityManager createInstance(Ini ini) {
<span class="pc bpc" id="L91" title="1 of 2 branches missed."> if (CollectionUtils.isEmpty(ini)) {</span>
<span class="nc" id="L92"> throw new NullPointerException(&quot;Ini argument cannot be null or empty.&quot;);</span>
}
<span class="fc" id="L94"> SecurityManager securityManager = createSecurityManager(ini);</span>
<span class="pc bpc" id="L95" title="1 of 2 branches missed."> if (securityManager == null) {</span>
<span class="nc" id="L96"> String msg = SecurityManager.class + &quot; instance cannot be null.&quot;;</span>
<span class="nc" id="L97"> throw new ConfigurationException(msg);</span>
}
<span class="fc" id="L99"> return securityManager;</span>
}
private SecurityManager createSecurityManager(Ini ini) {
<span class="fc" id="L103"> Ini.Section mainSection = ini.getSection(MAIN_SECTION_NAME);</span>
<span class="fc bfc" id="L104" title="All 2 branches covered."> if (CollectionUtils.isEmpty(mainSection)) {</span>
//try the default:
<span class="fc" id="L106"> mainSection = ini.getSection(Ini.DEFAULT_SECTION_NAME);</span>
}
<span class="fc" id="L108"> return createSecurityManager(ini, mainSection);</span>
}
protected boolean isAutoApplyRealms(SecurityManager securityManager) {
<span class="fc" id="L112"> boolean autoApply = true;</span>
<span class="pc bpc" id="L113" 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="L115"> RealmSecurityManager realmSecurityManager = (RealmSecurityManager) securityManager;</span>
<span class="fc" id="L116"> Collection&lt;Realm&gt; realms = realmSecurityManager.getRealms();</span>
<span class="fc bfc" id="L117" title="All 2 branches covered."> if (!CollectionUtils.isEmpty(realms)) {</span>
<span class="fc" id="L118"> log.info(&quot;Realms have been explicitly set on the SecurityManager instance - auto-setting of &quot; +</span>
&quot;realms will not occur.&quot;);
<span class="fc" id="L120"> autoApply = false;</span>
}
}
<span class="fc" id="L123"> return autoApply;</span>
}
@SuppressWarnings({&quot;unchecked&quot;})
private SecurityManager createSecurityManager(Ini ini, Ini.Section mainSection) {
<span class="fc" id="L129"> Map&lt;String, ?&gt; defaults = createDefaults(ini, mainSection);</span>
<span class="fc" id="L130"> Map&lt;String, ?&gt; objects = buildInstances(mainSection, defaults);</span>
<span class="fc" id="L132"> SecurityManager securityManager = getSecurityManagerBean();</span>
<span class="fc" id="L134"> boolean autoApplyRealms = isAutoApplyRealms(securityManager);</span>
<span class="fc bfc" id="L136" 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="L139"> Collection&lt;Realm&gt; realms = getRealms(objects);</span>
//set them on the SecurityManager
<span class="fc bfc" id="L141" title="All 2 branches covered."> if (!CollectionUtils.isEmpty(realms)) {</span>
<span class="fc" id="L142"> applyRealmsToSecurityManager(realms, securityManager);</span>
}
}
<span class="fc" id="L146"> return securityManager;</span>
}
protected Map&lt;String, ?&gt; createDefaults(Ini ini, Ini.Section mainSection) {
<span class="fc" id="L150"> Map&lt;String, Object&gt; defaults = new LinkedHashMap&lt;String, Object&gt;();</span>
<span class="fc" id="L152"> SecurityManager securityManager = createDefaultInstance();</span>
<span class="fc" id="L153"> defaults.put(SECURITY_MANAGER_NAME, securityManager);</span>
<span class="fc bfc" id="L155" title="All 2 branches covered."> if (shouldImplicitlyCreateRealm(ini)) {</span>
<span class="fc" id="L156"> Realm realm = createRealm(ini);</span>
<span class="pc bpc" id="L157" title="1 of 2 branches missed."> if (realm != null) {</span>
<span class="fc" id="L158"> defaults.put(INI_REALM_NAME, realm);</span>
}
}
<span class="fc" id="L162"> return defaults;</span>
}
private Map&lt;String, ?&gt; buildInstances(Ini.Section section, Map&lt;String, ?&gt; defaults) {
<span class="fc" id="L166"> this.builder = new ReflectionBuilder(defaults);</span>
<span class="fc" id="L167"> return this.builder.buildObjects(section);</span>
}
private void addToRealms(Collection&lt;Realm&gt; realms, RealmFactory factory) {
<span class="nc" id="L171"> LifecycleUtils.init(factory);</span>
<span class="nc" id="L172"> Collection&lt;Realm&gt; factoryRealms = factory.getRealms();</span>
//SHIRO-238: check factoryRealms (was 'realms'):
<span class="nc bnc" id="L174" title="All 2 branches missed."> if (!CollectionUtils.isEmpty(factoryRealms)) {</span>
<span class="nc" id="L175"> realms.addAll(factoryRealms);</span>
}
<span class="nc" id="L177"> }</span>
private Collection&lt;Realm&gt; getRealms(Map&lt;String, ?&gt; instances) {
//realms and realm factory might have been created - pull them out first so we can
//initialize the securityManager:
<span class="fc" id="L183"> List&lt;Realm&gt; realms = new ArrayList&lt;Realm&gt;();</span>
//iterate over the map entries to pull out the realm factory(s):
<span class="fc bfc" id="L186" title="All 2 branches covered."> for (Map.Entry&lt;String, ?&gt; entry : instances.entrySet()) {</span>
<span class="fc" id="L188"> String name = entry.getKey();</span>
<span class="fc" id="L189"> Object value = entry.getValue();</span>
<span class="pc bpc" id="L191" title="1 of 2 branches missed."> if (value instanceof RealmFactory) {</span>
<span class="nc" id="L192"> addToRealms(realms, (RealmFactory) value);</span>
<span class="fc bfc" id="L193" title="All 2 branches covered."> } else if (value instanceof Realm) {</span>
<span class="fc" id="L194"> Realm realm = (Realm) value;</span>
//set the name if null:
<span class="fc" id="L196"> String existingName = realm.getName();</span>
<span class="pc bpc" id="L197" title="2 of 4 branches missed."> if (existingName == null || existingName.startsWith(realm.getClass().getName())) {</span>
<span class="nc bnc" id="L198" title="All 2 branches missed."> if (realm instanceof Nameable) {</span>
<span class="nc" id="L199"> ((Nameable) realm).setName(name);</span>
<span class="nc" id="L200"> log.debug(&quot;Applied name '{}' to Nameable realm instance {}&quot;, name, realm);</span>
} else {
<span class="nc" id="L202"> log.info(&quot;Realm does not implement the {} interface. Configured name will not be applied.&quot;,</span>
<span class="nc" id="L203"> Nameable.class.getName());</span>
}
}
<span class="fc" id="L206"> realms.add(realm);</span>
}
<span class="fc" id="L208"> }</span>
<span class="fc" id="L210"> return realms;</span>
}
private void assertRealmSecurityManager(SecurityManager securityManager) {
<span class="pc bpc" id="L214" title="1 of 2 branches missed."> if (securityManager == null) {</span>
<span class="nc" id="L215"> throw new NullPointerException(&quot;securityManager instance cannot be null&quot;);</span>
}
<span class="pc bpc" id="L217" title="1 of 2 branches missed."> if (!(securityManager instanceof RealmSecurityManager)) {</span>
<span class="nc" id="L218"> String msg = &quot;securityManager instance is not a &quot; + RealmSecurityManager.class.getName() +</span>
&quot; instance. This is required to access or configure realms on the instance.&quot;;
<span class="nc" id="L220"> throw new ConfigurationException(msg);</span>
}
<span class="fc" id="L222"> }</span>
protected void applyRealmsToSecurityManager(Collection&lt;Realm&gt; realms, SecurityManager securityManager) {
<span class="fc" id="L225"> assertRealmSecurityManager(securityManager);</span>
<span class="fc" id="L226"> ((RealmSecurityManager) securityManager).setRealms(realms);</span>
<span class="fc" id="L227"> }</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="L240" title="1 of 2 branches missed."> return !CollectionUtils.isEmpty(ini) &amp;&amp;</span>
<span class="fc bfc" id="L241" title="All 2 branches covered."> (!CollectionUtils.isEmpty(ini.getSection(IniRealm.ROLES_SECTION_NAME)) ||</span>
<span class="fc bfc" id="L242" 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="L253"> IniRealm realm = new IniRealm();</span>
<span class="fc" id="L254"> realm.setName(INI_REALM_NAME);</span>
<span class="fc" id="L255"> realm.setIni(ini); //added for SHIRO-322</span>
<span class="fc" id="L256"> return realm;</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>