Skip to content

Commit

Permalink
[SHIRO-893] Fix NPE in ShiroFilter.init()
Browse files Browse the repository at this point in the history
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().
  • Loading branch information
julian-computes authored and bmarwell committed Oct 26, 2022
1 parent 0d27bd0 commit 9543284
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,20 @@ public ServletContext getServletContext() {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
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;
Expand Down Expand Up @@ -63,6 +65,24 @@ public void singleServiceTest() throws Exception {
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 {

Expand Down

0 comments on commit 9543284

Please sign in to comment.