SpringSecurity3.X remember-me

Source: Internet
Author: User

When configuring remember-me in ingSecurity, The remember-me does not work. Follow the instructions in the official documentation, you only need to add the <remember-me/> Configuration in


1 <input id = "_ spring_security_remember_me" name = "_ spring_security_remember_me" type = "checkbox" value = "true"/>


Briefly describe the login process of SpringSecurity:


1 UsernamePasswordAuthenticationFilter: obtains the user name and password information submitted by the user.

2 --> UserDetailsService: encapsulate user objects

3 --> AbstractUserDetailsAuthenticationProvider

4 --> ProviderManager: the Authentication is encapsulated in SecurityContext (by default, ProviderManager clears the Authentication password after successful Authentication, but retains the user name)

5 --> AbstractRememberMeServices: determines whether the request parameter contains the _ spring_security_remember_me parameter. If any of the parameter values is true, on, yes, and 1, the user name and password are saved in the cookie. Otherwise, no processing is performed.

6 --> AccessDecisionManager: determines whether to allow access.

7

After 8, when requesting protected resources, RememberMeAuthenticationFilter first checks whether the Authentication has expired. If it fails, it tries to search for it from the cookie and find it, then re-encapsulate the Authentication.


The problem lies in ProviderManager. Its Boolean attribute eraseCredentialsAfterAuthentication defaults to true. If it is true, the password information in Authentication is cleared after the username and password are verified successfully, however, the condition for saving the cookie by using AbstractRememberMeServices requires that username and password be obtained from the Authentication at the same time, which leads to the fact that AbstractRememberMeServices will never store username and password in the cookie by default, to ensure that username and password can be correctly stored in cookies, we need to modify the eraseCredentialsAfterAuthentication value to false. Fortunately, it is convenient to modify this attribute, as shown below:


1 <authentication-manager erase-credentials = "false">

2 <authentication-provider user-service-ref = "userService">

3 <password-encoder hash = "md5"/>

4 </authentication-provider>

5 </authentication-manager>


1 <remember-me token-validity-seconds = "123456789"/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.