How can I disable spring form based login for RESTful endpoints?

前端 未结 1 1304
心在旅途
心在旅途 2020-12-05 11:54

I have spring-security configured using basic and form based authentication as per auto-config=\'true\'.

I would like the endpoints under /api/**<

相关标签:
1条回答
  • 2020-12-05 12:30

    With Spring Security 3.1, your best option is to split the restful and non-restful parts of your application into separate filter chains by using two separate <http> elements. The restful API chain can then be configured to be stateless and use basic authentication, while the default chain can use a normal form-login configuration.

    You would then have something like:

    <http pattern="/api/**" create-session="stateless">
        <intercept-url pattern="/api/**" access="ROLE_API_USER" />
        <http-basic />        
    </http>
    
    <!-- No pattern attribute, so defaults to matching any request -->
    <http>
        <intercept-url pattern="/**" access="ROLE_USER" />
        <form-login />        
    </http>
    

    The chain definitions must be ordered from most specific pattern to most general, so the default chain comes last.

    0 讨论(0)
提交回复
热议问题