背景
大家好,我们有Spring
使用Spring security
. 我们通过定义定义了安全过滤器
<b:bean id="springSecurityFilterChain" class="org.springframework.security.web.FilterChainProxy">
哪里filter-chain-map
在web.xml
我们做的
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
一切都很好:)。现在Spring session
根据 以下几行进行连接redis
时doc
<context:annotation-config />
<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>
创建一个filter
名为springSessionRepositoryFilter
. 所以基本上我们所做的就是在每个自定义filter-chain
中添加该过滤器作为第一个过滤器。IE:
<b:bean id="springSecurityFilterChain" class="org.springframework.security.web.FilterChainProxy">
<filter-chain-map request-matcher="ant">
<filter-chain pattern="/api/someapieformobilelogin" filters="none" /> <!-- no filter on login -->
<filter-chain pattern="/api/**"
filters="springSessionRepositoryFilter, securityContextFilter,and some other spring security filter />
<filter-chain pattern="/**"
filters="springSessionRepositoryFilter, securityContextFilter,and some other spring security filter />
结果:该应用程序似乎运行良好,并且monitoring
通过redis-cli
显示spring
正在与之通信redis
。
问题
在springSessionRepositoryFilter
里面使用filter-chain
可以吗?还是我们滥用了过滤系统?
谢谢,
橡木
编辑
似乎上述方法不适用于希望Authenticate
从代码中获取用户的情况,即
Authentication authentication = authenticationManager
.authenticate(authenticationToken);
SecurityContext securityContext = SecurityContextHolder
.getContext();
securityContext.setAuthentication(authentication);
将失败。也许是因为它不足以通过filter-chain
of运行它org.springframework.security.web.FilterChainProxy
。
你觉得如何运行filter
它web.xml
?
<filter>
<filter-name>springSessionRepositoryFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSessionRepositoryFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
以上将强制运行springSessionRepositoryFilter
,springSecurityFilterChain
但在此示例org.springframework.web.filter.DelegatingFilterProxy
中被调用两次。springSessionRepositoryFilter
在过滤器之前作为过滤器运行的任何其他方法springSecurityFilterChain
?