Skip to content
Advertisement

Spring Security – How to mix xml and Java SecurityFilterChain declaration configurations?

Working on an application that was using XML Schema-based Spring configuration only, I’m slowly introducing Java based configuration.

Unfortunately I’m not able to use Spring Security html xml element and Java based SecurityFilterChain configuration at the same time.

When I inspect the filterChains list in the FilterChainProxy, it only contains the filter chains from the xml configuration. Despite the Java method creating the SecurityFilterChain bean being called, this filter chain is not added to the FilterChainProxy.

If I remove the html elements from the xml configuration, the SecurityFilterChain from the Java configuration is added to the FilterChainProxy and called as expected.

Spring versions I’m using:

  • Spring Framework: 5.3.16
  • Spring Security: 5.6.2

Here is the xml configuration and Java configuration I’m trying to use simultaneously:

JavaScript
JavaScript

web.xml

JavaScript

Did I make a mistake, is there a way to achieve what I want, or is it simply not possible ?

Advertisement

Answer

Edit: @rwinch solution posted here

You can combine the XML and Java Configuration and exposes it as a different bean name using something like this:

JavaScript

Then ensure your XML configuration picks it up:

JavaScript

Initial workaround

Until better solution, it is possible to use the workaround below (if really needed). It adds the missing SecurityFilterChain to the FilterChainProxy once Spring Application context is initialized. I implemented it against Spring Security 5.6.2. Since it uses reflection, it might break after any upgrade of Spring Security version.

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement