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

前端 未结 4 761
孤街浪徒
孤街浪徒 2020-12-24 09:16

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

4条回答
  •  太阳男子
    2020-12-24 09:34

    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)

    
        
         
        
    
    
    
                  
    
    

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

    
    

    The end result is the basic filter will be applied as a passive filter and if its required credentials are missing it will continue down the filter chain and the form-login filter will then handle it.

    The problem with this approach is that if credentials are correctly entered, the response back is the login page from the form-login filter.

    However, It appears that this problem will be fixed by spring in version 3.1 of spring security and this work around will no longer be needed.

提交回复
热议问题