Adding Spring's Filters to ShiroFilterFactorBean when using Java config
In the days of XML this was defined, but was missed when we ported it to Java config
Fixes: SHIRO-687
diff --git a/support/spring/pom.xml b/support/spring/pom.xml
index a0b4d55..e076b7b 100644
--- a/support/spring/pom.xml
+++ b/support/spring/pom.xml
@@ -72,6 +72,11 @@
<scope>test</scope>
</dependency>
<dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-web</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-aspectj</artifactId>
<scope>test</scope>
diff --git a/support/spring/src/main/java/org/apache/shiro/spring/web/config/AbstractShiroWebFilterConfiguration.java b/support/spring/src/main/java/org/apache/shiro/spring/web/config/AbstractShiroWebFilterConfiguration.java
index fb12601..e15d50d 100644
--- a/support/spring/src/main/java/org/apache/shiro/spring/web/config/AbstractShiroWebFilterConfiguration.java
+++ b/support/spring/src/main/java/org/apache/shiro/spring/web/config/AbstractShiroWebFilterConfiguration.java
@@ -24,6 +24,9 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
+import javax.servlet.Filter;
+import java.util.Map;
+
/**
* @since 1.4.0
*/
@@ -35,6 +38,9 @@
@Autowired
protected ShiroFilterChainDefinition shiroFilterChainDefinition;
+ @Autowired
+ protected Map<String, Filter> filterMap;
+
@Value("#{ @environment['shiro.loginUrl'] ?: '/login.jsp' }")
protected String loginUrl;
@@ -53,6 +59,7 @@
filterFactoryBean.setSecurityManager(securityManager);
filterFactoryBean.setFilterChainDefinitionMap(shiroFilterChainDefinition.getFilterChainMap());
+ filterFactoryBean.setFilters(filterMap);
return filterFactoryBean;
}
diff --git a/support/spring/src/test/groovy/org/apache/shiro/spring/config/ShiroWebFilterConfigurationTest.groovy b/support/spring/src/test/groovy/org/apache/shiro/spring/config/ShiroWebFilterConfigurationTest.groovy
new file mode 100644
index 0000000..6ced8ba
--- /dev/null
+++ b/support/spring/src/test/groovy/org/apache/shiro/spring/config/ShiroWebFilterConfigurationTest.groovy
@@ -0,0 +1,82 @@
+package org.apache.shiro.spring.config
+
+import org.apache.shiro.mgt.SecurityManager
+import org.apache.shiro.spring.testconfig.RealmTestConfiguration
+import org.apache.shiro.spring.web.ShiroFilterFactoryBean
+import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition
+import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition
+import org.apache.shiro.spring.web.config.ShiroWebFilterConfiguration
+import org.apache.shiro.web.filter.mgt.FilterChainManager
+import org.junit.Test
+import org.springframework.beans.factory.annotation.Autowired
+import org.springframework.context.annotation.Bean
+import org.springframework.context.annotation.Configuration
+import org.springframework.test.context.ContextConfiguration
+import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests
+import org.springframework.test.context.web.WebAppConfiguration
+
+import javax.servlet.Filter
+import javax.servlet.FilterChain
+import javax.servlet.FilterConfig
+import javax.servlet.ServletException
+import javax.servlet.ServletRequest
+import javax.servlet.ServletResponse
+
+import static org.hamcrest.Matchers.contains
+import static org.hamcrest.Matchers.instanceOf
+import static org.hamcrest.Matchers.notNullValue
+import static org.hamcrest.MatcherAssert.assertThat
+
+/**
+ * Test ShiroWebFilterConfiguration creates a ShiroFilterFactoryBean that contains Servlet filters that are available for injection.
+ */
+@WebAppConfiguration
+@ContextConfiguration(classes = [RealmTestConfiguration, FilterConfiguration, ShiroConfiguration, ShiroWebFilterConfiguration])
+class ShiroWebFilterConfigurationTest extends AbstractJUnit4SpringContextTests {
+
+ @Autowired
+ private SecurityManager securityManager
+
+ @Autowired
+ private ShiroFilterFactoryBean shiroFilterFactoryBean
+
+ @Test
+ void testShiroFilterFactoryBeanContainsSpringFilters() {
+
+ assertThat shiroFilterFactoryBean, notNullValue()
+
+ // create the filter chain manager
+ FilterChainManager filterChainManager = shiroFilterFactoryBean.createFilterChainManager()
+ // lookup the chain by name
+ assertThat filterChainManager.getChain("/test-me"), contains(instanceOf(ExpectedTestFilter))
+ }
+
+ @Configuration
+ static class FilterConfiguration {
+
+ // random custom filter, which will be looked up via the shiroFilterFactoryBean
+ @Bean
+ Filter testFilter() {
+ return new ExpectedTestFilter()
+ }
+
+ @Bean
+ ShiroFilterChainDefinition shiroFilterChainDefinition() {
+ DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition()
+ chainDefinition.addPathDefinition("/test-me", "testFilter") // def matches the bean name
+ chainDefinition.addPathDefinition("/**", "authc")
+ return chainDefinition
+ }
+ }
+
+ static class ExpectedTestFilter implements Filter {
+ @Override
+ void init(FilterConfig filterConfig) throws ServletException {}
+
+ @Override
+ void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {}
+
+ @Override
+ void destroy() {}
+ }
+}
\ No newline at end of file