Spring security update last login date on authentication success

后端 未结 1 1091
傲寒
傲寒 2021-01-06 23:22

How do I fire my method on authentication success? I wanted to update my database column \'last login date\'. Looked around on google but still can\'t understand how it shou

相关标签:
1条回答
  • 2021-01-06 23:56

    You can override authenticationSuccessHandler with any custom implementation you wanted to perform. Here you want to update user login date or some other similar activities

    public class CustomAuthenticationSuccessHandler extends  
                         SavedRequestAwareAuthenticationSuccessHandler {
    
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, 
                                        HttpServletResponse response,          
                                        Authentication authentication) throws IOException,ServletException {
    
        super.onAuthenticationSuccess(request, response, authentication);
        //Now add your custom logic to update database 
      }
    }
    

    Now you need to update authenticationSuccessHandler confiuguration in xml file as shown below.

    <beans:bean id="authenticationSuccessHandler" class="yourpackage.CustomAuthenticationSuccessHandler">
          <beans:property name="useReferer" value="true" />
    </beans:bean>
    

    Optionally,

    <beans:bean id="authenticationSuccessHandlerWithoutReferer" class="yourpackage.CustomAuthenticationSuccessHandler">
          <beans:property name="useReferer" value="false" />
    </beans:bean>
    
    0 讨论(0)
提交回复
热议问题