I’m working on an application using Spring security.
The application is extensible and I would like to block extensions from programmatically changing the filters in the filter chain map of Spring’s FilterChainProxy. What I intend to do is the following:
-
Implement a
CustomFilterChainProxyimplementing all ofFilterChainProxy‘s implemented interfaces (Filter, InitializingBean, ApplicationContextAware). In it I will hold a privateFilterChainProxymember and delegate all of the interface calls to it. -
Use Spring’s
DelegatingFilterProxyby declaring in theweb.xmlfile:<filter> <filter-name>customSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> -
In the Spring configuration files, instead of using Spring’s
FilterChainProxydirectly I will have my bean have theCustomFilterChainProxyas its class, as follows:<bean id="customSecurityFilterChain" class="....CustomFilterChainProxy"> <security:filter-chain-map ...> <security:filter-chain pattern="..." filters="..." /> <security:filter-chain pattern="..." filters="..." /> ... </security:filter-chain-map> </bean> -
In order to be able to set the filter chain map during Spring bean loading I must supply a setter in my
CustomFilterChainProxyclass. That I will do. And in order to prevent setting the filter chain map after Spring bean loading I will make sure that after bean construction (in a@PostConstructmethod) an exception will be thrown from that setter.
By having a CustomFilterChainProxy instead of a FilterChainProxy, am I causing any Spring process to malfunction?
I saw the only Spring class referencing the FilterChainProxy object itself is FilterChainProxyPostProcessor but couldn’t find out if this should affect my implementation choice. Any input?
Thanks a lot.
This is unlikely to be sufficient to protect you from malicious extension code.
If the extension can access your bean, then it can also just access the original
FilterChainProxythrough theApplicationContext. In fact, it can probably access any other bean in the same configuration, so it could potentially:If you have untrusted code in your app then you would need to use a
SecurityManagerto prevent this kind of thing and you can then also prevent access to Spring Security classes. Configuring aSecurityManagercan be a pain, but it’s probably the only option if you have code you don’t trust running in the same VM.Update: If your only concern is preventing anyone from calling the
setFilterChainMapmethod then overriding this method will obviously prevent anyone from accidentally calling this through a reference to your bean (this method is actually deprecated in 3.1 in favour of a constructor. However, it’s not clear from your question why someone would obtain a reference to your instance rather than the original bean, or why this is your main concern. TheFilterChainProxyis not normally accessed by user code in an application. To do so, you’d have to explicitly request it from the bean factory.