Alex Rivera | Logout

Spring security - how to mention both form based and basic authentication

Asked 2009-08-19T00:13:48.030
13

Is it possible to mention both form-based and basic authentication in Spring security using namespace configuration without overriding other ? So that the appliciation could serve both browser based request and remoting client.

Edit
Report

1 Answer

6

The end result you want is possible, I have ran into that exact same problem and here is my solution.

Anytime when defining form-login in the namespace it will override automatically any other authentication filters you apply via namespace. This is done through the ordering of the filter chain look at FilterChainOrder.java in the spring security to see how the order is actually applied to each filter.

To get around this remove the http-basic tag from the namespace then manually define the bean to handle basic authentication and place its order before the AuthenticationProcessingFilter because this is the spring security filter that will handle the form-login.

The BasicProcessingFilter spring provides to handle Basic authentication is a passive filter, meaning that if the credentials are missing it will continue down the filter chain until it finds the appropriate filter to handle the request.

Now by manually defining the BasicProcessingFilter bean we can set the order that it will appear in the filter chain. Below is an example of the additional xml declarations you will need to supply in the security xml (Spring Security < 3.x)

<bean id="basicProcessingFilter" class="org.springframework.security.ui.basicauth.BasicProcessingFilter">
    <property name="authenticationManager"><ref bean="authenticationManager"/></property>
     <security:custom-filter before="AUTHENTICATION_PROCESSING_FILTER"/>
    <property name="authenticationEntryPoint"><ref bean="authenticationEntryPoint"/></property>
</bean>

<bean id="authenticationEntryPoint"
    class="org.springframework.security.ui.basicauth.BasicProcessingFilterEntryPoint">
              <property name="realmName" value="My Realm Here"/>
</bean>

Also note if your authenticationManager reference isn't found you can add an alias to your namespace like the one below.


                        
                        
answered 2009-11-06T19:45:54.163

Your Answer