Somtirtha Ghosh
Somtirtha Ghosh

Reputation: 57

Add a filter bean to spring-security filter chain based on condition in applicationContext.xml

We have a spring-security filter chain as below where we provide the list of filters for each url pattern in the applicationContext.xml

    <bean id="filterChainProxy" class="org.springframework.security.web.FilterChainProxy"> <constructor-arg index="0">
        <list>
            <security:filter-chain pattern="/rest" filters="
                sessionContextIntegrationFilter,
                ${bean.loggingFilter},
                ${bean.basicProcessingFilter},`

Now, is there any way to add a filter to this chain based on a condition? Something similar to below using SpEL

"#{'${some.condition}'.equalsIgnoreCase('true') ? actualFilter: dummyFilter}" />

Without using annotations or profiles please suggest a solution which can be implemented in the same xml file.

Update: I have tried below code before posting this question and it did not work for me, but as per comments from @R.G looks like it should work. Please point me where it was wrong (For simplicity I replaced the condition with 'true')

<bean id="filterChainProxy" class="org.springframework.security.web.FilterChainProxy"> <constructor-arg index="0">
<list>
    <security:filter-chain pattern="/rest" filters="
        sessionContextIntegrationFilter,
        ${bean.loggingFilter},
        ${bean.basicProcessingFilter},
        #{ 'true' == 'true' ? 'actualFilter' : 'dummyFilter' }"/>
</list></constructor-arg>
</bean>

Upvotes: 1

Views: 1569

Answers (1)

R.G
R.G

Reputation: 7131

Following would switch between filter beans based on condition

    <bean id="filterChainProxy" class="org.springframework.security.web.FilterChainProxy"> <constructor-arg index="0">
    <list>
        <security:filter-chain pattern="/rest" filters="
            sessionContextIntegrationFilter,
            ${bean.loggingFilter},
            ${bean.basicProcessingFilter},
            #{ some.condition == 'true' ? 'actualFilter' : 'dummyFilter' }"/>
    </list></constructor-arg>
    </bean>

Upvotes: 1

Related Questions