You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shiro.apache.org by bm...@apache.org on 2022/10/26 19:11:57 UTC

[shiro] branch 1.10.x updated: [SHIRO-893] Fix NPE in ShiroFilter.init()

This is an automated email from the ASF dual-hosted git repository.

bmarwell pushed a commit to branch 1.10.x
in repository https://gitbox.apache.org/repos/asf/shiro.git


The following commit(s) were added to refs/heads/1.10.x by this push:
     new 97b42229 [SHIRO-893] Fix NPE in ShiroFilter.init()
     new efbd28cb Merge pull request #499 from julian-computes/shiro_filter_config_npe_fix
97b42229 is described below

commit 97b422297c157617f0ce1dc2ebec5710a5a87222
Author: julian-computes <ju...@gmail.com>
AuthorDate: Wed Oct 26 08:53:30 2022 -0400

    [SHIRO-893] Fix NPE in ShiroFilter.init()
    
    In 1.10.0, an NPE can be thrown because DefaultWebEnvironment has no default value for ShiroFilterConfiguration. This commit adds a default value determined by WebEnvironment.getShiroFilterConfiguration().
---
 .../apache/shiro/web/env/DefaultWebEnvironment.java  | 10 +++++++---
 .../shiro/web/env/EnvironmentLoaderServiceTest.java  | 20 ++++++++++++++++++++
 2 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/web/src/main/java/org/apache/shiro/web/env/DefaultWebEnvironment.java b/web/src/main/java/org/apache/shiro/web/env/DefaultWebEnvironment.java
index 7ce3d870..692f1c36 100644
--- a/web/src/main/java/org/apache/shiro/web/env/DefaultWebEnvironment.java
+++ b/web/src/main/java/org/apache/shiro/web/env/DefaultWebEnvironment.java
@@ -39,8 +39,6 @@ public class DefaultWebEnvironment extends DefaultEnvironment implements Mutable
 
     private ServletContext servletContext;
 
-    private ShiroFilterConfiguration filterConfiguration;
-
     public DefaultWebEnvironment() {
         super();
     }
@@ -97,6 +95,12 @@ public class DefaultWebEnvironment extends DefaultEnvironment implements Mutable
 
     @Override
     public ShiroFilterConfiguration getShiroFilterConfiguration() {
-        return getObject(SHIRO_FILTER_CONFIG_NAME, ShiroFilterConfiguration.class);
+        ShiroFilterConfiguration config = getObject(SHIRO_FILTER_CONFIG_NAME, ShiroFilterConfiguration.class);
+        // Use the default configuration if config is null
+        if (config == null) {
+            config = MutableWebEnvironment.super.getShiroFilterConfiguration();
+            setShiroFilterConfiguration(config);
+        }
+        return config;
     }
 }
diff --git a/web/src/test/java/org/apache/shiro/web/env/EnvironmentLoaderServiceTest.java b/web/src/test/java/org/apache/shiro/web/env/EnvironmentLoaderServiceTest.java
index 2fbc0a1a..13df5d71 100644
--- a/web/src/test/java/org/apache/shiro/web/env/EnvironmentLoaderServiceTest.java
+++ b/web/src/test/java/org/apache/shiro/web/env/EnvironmentLoaderServiceTest.java
@@ -30,6 +30,8 @@ import java.io.InputStream;
 import static org.easymock.EasyMock.expect;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.instanceOf;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
 import static org.hamcrest.Matchers.sameInstance;
 import static org.hamcrest.Matchers.stringContainsInOrder;
 
@@ -60,6 +62,24 @@ public class EnvironmentLoaderServiceTest {
         assertThat(environmentStub.getServletContext(), sameInstance(servletContext));
     }
 
+    @Test
+    public void testDefaultWebEnvironment() {
+        ServletContext servletContext = EasyMock.mock(ServletContext.class);
+        expect(servletContext.getInitParameter("shiroEnvironmentClass"))
+                .andReturn(DefaultWebEnvironment.class.getName());
+        expect(servletContext.getInitParameter("shiroConfigLocations")).andReturn(null);
+
+        EasyMock.replay(servletContext);
+
+        WebEnvironment environment = new EnvironmentLoader().createEnvironment(servletContext);
+
+        EasyMock.verify(servletContext);
+
+        assertThat(environment, instanceOf(DefaultWebEnvironment.class));
+        assertThat(environment.getShiroFilterConfiguration(), is(notNullValue()));
+        assertThat(environment.getServletContext(), sameInstance(servletContext));
+    }
+
     @Test()
     @Ignore
     public void multipleServiceTest() throws Exception {