SHIRO-593 - Allow defaults added to the IniWebEnvironment to be passed into the 
SecurityManager and FilterChainResolver factories


Project: http://git-wip-us.apache.org/repos/asf/shiro/repo
Commit: http://git-wip-us.apache.org/repos/asf/shiro/commit/86d951ef
Tree: http://git-wip-us.apache.org/repos/asf/shiro/tree/86d951ef
Diff: http://git-wip-us.apache.org/repos/asf/shiro/diff/86d951ef

Branch: refs/heads/master
Commit: 86d951ef8dd9db10f43ebd8c31b4201bf637a061
Parents: 6e9a20a
Author: Brian Demers <bdem...@apache.org>
Authored: Thu Sep 29 11:24:00 2016 -0400
Committer: Brian Demers <bdem...@apache.org>
Committed: Tue Oct 18 13:19:02 2016 -0400

----------------------------------------------------------------------
 .../apache/shiro/web/env/IniWebEnvironment.java | 35 ++++++++++++++------
 .../shiro/web/env/IniWebEnvironmentTest.groovy  |  4 +--
 2 files changed, 27 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/shiro/blob/86d951ef/web/src/main/java/org/apache/shiro/web/env/IniWebEnvironment.java
----------------------------------------------------------------------
diff --git a/web/src/main/java/org/apache/shiro/web/env/IniWebEnvironment.java 
b/web/src/main/java/org/apache/shiro/web/env/IniWebEnvironment.java
index f07406a..bff933a 100644
--- a/web/src/main/java/org/apache/shiro/web/env/IniWebEnvironment.java
+++ b/web/src/main/java/org/apache/shiro/web/env/IniWebEnvironment.java
@@ -22,10 +22,7 @@ import org.apache.shiro.config.ConfigurationException;
 import org.apache.shiro.config.Ini;
 import org.apache.shiro.config.IniFactorySupport;
 import org.apache.shiro.io.ResourceUtils;
-import org.apache.shiro.util.CollectionUtils;
-import org.apache.shiro.util.Destroyable;
-import org.apache.shiro.util.Initializable;
-import org.apache.shiro.util.StringUtils;
+import org.apache.shiro.util.*;
 import org.apache.shiro.web.config.IniFilterChainResolverFactory;
 import org.apache.shiro.web.config.WebIniSecurityManagerFactory;
 import org.apache.shiro.web.filter.mgt.FilterChainResolver;
@@ -37,6 +34,7 @@ import org.slf4j.LoggerFactory;
 import javax.servlet.ServletContext;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.HashMap;
 import java.util.Map;
 
 /**
@@ -47,6 +45,7 @@ import java.util.Map;
 public class IniWebEnvironment extends ResourceBasedWebEnvironment implements 
Initializable, Destroyable {
 
     public static final String DEFAULT_WEB_INI_RESOURCE_PATH = 
"/WEB-INF/shiro.ini";
+    public static final String FILTER_CHAIN_RESOLVER_NAME = 
"filterChainResolver";
 
     private static final Logger log = 
LoggerFactory.getLogger(IniWebEnvironment.class);
 
@@ -259,7 +258,13 @@ public class IniWebEnvironment extends 
ResourceBasedWebEnvironment implements In
             Ini.Section filters = 
ini.getSection(IniFilterChainResolverFactory.FILTERS);
             if (!CollectionUtils.isEmpty(urls) || 
!CollectionUtils.isEmpty(filters)) {
                 //either the urls section or the filters section was defined.  
Go ahead and create the resolver:
-                IniFilterChainResolverFactory factory = new 
IniFilterChainResolverFactory(ini, this.objects);
+
+                Factory<FilterChainResolver> factory = 
(Factory<FilterChainResolver>) this.objects.get(FILTER_CHAIN_RESOLVER_NAME);
+                if (factory instanceof IniFactorySupport) {
+                    IniFactorySupport iniFactory = (IniFactorySupport) factory;
+                    iniFactory.setIni(ini);
+                    iniFactory.setDefaults(this.objects);
+                }
                 resolver = factory.getInstance();
             }
         }
@@ -268,12 +273,16 @@ public class IniWebEnvironment extends 
ResourceBasedWebEnvironment implements In
     }
 
     protected WebSecurityManager createWebSecurityManager() {
-        WebIniSecurityManagerFactory factory;
+        WebIniSecurityManagerFactory factory = new 
WebIniSecurityManagerFactory();
+
         Ini ini = getIni();
-        if (CollectionUtils.isEmpty(ini)) {
-            factory = new WebIniSecurityManagerFactory();
-        } else {
-            factory = new WebIniSecurityManagerFactory(ini);
+        if (!CollectionUtils.isEmpty(ini)) {
+            factory.setIni(ini);
+        }
+
+        Map<String, Object> defaults = getDefaults();
+        if (!CollectionUtils.isEmpty(defaults)) {
+            factory.setDefaults(defaults);
         }
 
         WebSecurityManager wsm = (WebSecurityManager)factory.getInstance();
@@ -382,4 +391,10 @@ public class IniWebEnvironment extends 
ResourceBasedWebEnvironment implements In
     public void setIni(Ini ini) {
         this.ini = ini;
     }
+
+    protected Map<String, Object> getDefaults() {
+        Map<String, Object> defaults = new HashMap<String, Object>();
+        defaults.put(FILTER_CHAIN_RESOLVER_NAME, new 
IniFilterChainResolverFactory());
+        return defaults;
+    }
 }

http://git-wip-us.apache.org/repos/asf/shiro/blob/86d951ef/web/src/test/groovy/org/apache/shiro/web/env/IniWebEnvironmentTest.groovy
----------------------------------------------------------------------
diff --git 
a/web/src/test/groovy/org/apache/shiro/web/env/IniWebEnvironmentTest.groovy 
b/web/src/test/groovy/org/apache/shiro/web/env/IniWebEnvironmentTest.groovy
index 84b698d..7dcb40b 100644
--- a/web/src/test/groovy/org/apache/shiro/web/env/IniWebEnvironmentTest.groovy
+++ b/web/src/test/groovy/org/apache/shiro/web/env/IniWebEnvironmentTest.groovy
@@ -49,8 +49,8 @@ class IniWebEnvironmentTest {
         env.init()
 
         assertNotNull env.objects
-        //asserts that the objects size = securityManager (1) + the event bus 
(1) + num custom objects + num default filters
-        def expectedSize = 3 + DefaultFilter.values().length
+        //asserts that the objects size = securityManager (1) + the event bus 
(1) + filterChainResolverFactory (1) + num custom objects + num default filters
+        def expectedSize = 4 + DefaultFilter.values().length
         assertEquals expectedSize, env.objects.size()
         assertNotNull env.objects['securityManager']
         assertNotNull env.objects['compositeBean']

Reply via email to