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:14:09 UTC

[shiro] branch SHIRO-893_main_npe_filter_init created (now 95432841)

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

bmarwell pushed a change to branch SHIRO-893_main_npe_filter_init
in repository https://gitbox.apache.org/repos/asf/shiro.git


      at 95432841 [SHIRO-893] Fix NPE in ShiroFilter.init()

This branch includes the following new commits:

     new 95432841 [SHIRO-893] Fix NPE in ShiroFilter.init()

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[shiro] 01/01: [SHIRO-893] Fix NPE in ShiroFilter.init()

Posted by bm...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

bmarwell pushed a commit to branch SHIRO-893_main_npe_filter_init
in repository https://gitbox.apache.org/repos/asf/shiro.git

commit 95432841f69d91bb68c17b2174fc4de3ff209755
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  | 16 ++++++++++++++++
 .../shiro/web/env/EnvironmentLoaderServiceTest.java  | 20 ++++++++++++++++++++
 2 files changed, 36 insertions(+)

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 d5658ab4..28864222 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
@@ -84,4 +84,20 @@ public class DefaultWebEnvironment extends DefaultEnvironment implements Mutable
     public void setServletContext(ServletContext servletContext) {
         this.servletContext = servletContext;
     }
+
+    @Override
+    public void setShiroFilterConfiguration(ShiroFilterConfiguration filterConfiguration) {
+        setObject(SHIRO_FILTER_CONFIG_NAME, filterConfiguration);
+    }
+
+    @Override
+    public ShiroFilterConfiguration getShiroFilterConfiguration() {
+        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 9a45d20d..db4eb5b3 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.util.List;
 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;
 import static org.mockito.Mockito.mock;
@@ -63,6 +65,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()
     public void multipleServiceTest() throws Exception {