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 committed Oct 26, 2022
1 parent 0b277a6 commit 97b4222
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public class DefaultWebEnvironment extends DefaultEnvironment implements Mutable

private ServletContext servletContext;

private ShiroFilterConfiguration filterConfiguration;

public DefaultWebEnvironment() {
super();
}
Expand Down Expand Up @@ -97,6 +95,12 @@ public void setShiroFilterConfiguration(ShiroFilterConfiguration filterConfigura

@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;
}
}
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;

Expand Down Expand Up @@ -60,6 +62,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()
@Ignore
public void multipleServiceTest() throws Exception {
Expand Down

0 comments on commit 97b4222

Please sign in to comment.