Background
Hey all,
We have Spring
project which uses Spring security
. We have defined the security filters by defining
<b:bean id="springSecurityFilterChain" class="org.springframework.security.web.FilterChainProxy">
whith filter-chain-map
and in the web.xml
we do
<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>
and it all works well :). Now when hooking up Spring session
with redis
according to the doc
the next following lines
<context:annotation-config />
<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>
create a filter
named springSessionRepositoryFilter
. So basically what we did is in every custom filter-chain
we add that filter to be the very first filter . i.e:
<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 />
The results: the app seems to work good and also monitoring
via redis-cli
shows the spring
is communicating with redis
.
The question
Does the use of springSessionRepositoryFilter
inside the filter-chain
is ok? or we abused the filtering system?
Thanks,
Oak
Edit
It seems that above will not work for the case one wants to Authenticate
the user from code i.e
Authentication authentication = authenticationManager
.authenticate(authenticationToken);
SecurityContext securityContext = SecurityContextHolder
.getContext();
securityContext.setAuthentication(authentication);
will failed. Maybe because its not enough to run it via filter-chain
of org.springframework.security.web.FilterChainProxy
.
What do you think on run it as filter
in 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>
The above will force running springSessionRepositoryFilter
before springSecurityFilterChain
but in this example org.springframework.web.filter.DelegatingFilterProxy
is being called twice. any other ways to make springSessionRepositoryFilter
run as a filter before out springSecurityFilterChain
filter?