title: Spring security update last login date on authentication success
body: <p>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 should be done.</p>
  
  <p>Here is my spring-security.xml</p>
  
  <pre><code>&lt;beans:beans xmlns="http://www.springframework.org/schema/security"
           xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/security
  http://www.springframework.org/schema/security/spring-security-3.2.xsd"
           xmlns:security="http://www.springframework.org/schema/security"&gt;
  
  &lt;beans:bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
      &lt;beans:property name="driverClassName" value="com.mysql.jdbc.Driver"/&gt;
      &lt;beans:property name="url" value="jdbc:mysql://localhost:3306/myDB"/&gt;
      &lt;beans:property name="username" value="root"/&gt;
      &lt;beans:property name="password" value="root"/&gt;
  &lt;/beans:bean&gt;
  
  &lt;!-- login page are exempted from security--&gt;
  &lt;security:http pattern="/login" security="none"/&gt;
  
  &lt;security:http auto-config="true"&gt;
      &lt;intercept-url pattern="/page1" access="ROLE_USER_ADMIN,ROLE_ADMIN,ROLE_PAGE1" /&gt;
      &lt;intercept-url pattern="/page2" access="ROLE_USER_ADMIN,ROLE_ADMIN,ROLE_PAGE2" /&gt;
      &lt;intercept-url pattern="/page3" access="ROLE_USER_ADMIN,ROLE_ADMIN,ROLE_PAGE3" /&gt;    
      &lt;intercept-url pattern="/*" access="ROLE_USER_ADMIN,ROLE_ACCOUNT" /&gt;  &lt;!--/** all url --&gt;
  
      &lt;security:session-management&gt;
          &lt;security:concurrency-control
              max-sessions="2"
              expired-url="/login"  /&gt;
      &lt;/security:session-management&gt;
  
  
      &lt;!-- access deny for non privileged user --&gt;
      &lt;access-denied-handler error-page="/access-denied" /&gt;
  
      &lt;!-- Logout --&gt;
      &lt;logout logout-success-url="/login?logout"  /&gt;
  &lt;/security:http&gt;
  
  
  &lt;beans:bean id="authenticationSuccessHandler" class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler"&gt;
      &lt;!-- After login, return to the last visited page --&gt;
      &lt;beans:property name="useReferer" value="true" /&gt;
  &lt;/beans:bean&gt;
  
  &lt;beans:bean id="authenticationSuccessHandlerWithoutReferer" class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler"&gt;
      &lt;!-- After login, stay to the same page --&gt;
      &lt;beans:property name="useReferer" value="false" /&gt;
  &lt;/beans:bean&gt;
  &lt;beans:bean class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler"/&gt;
  &lt;authentication-manager&gt;
      &lt;authentication-provider&gt;
          &lt;jdbc-user-service data-source-ref="dataSource1" 
                             users-by-username-query="query-for-username-and-password"
                             authorities-by-username-query="query-for-username-enabled-authority" /&gt;
          &lt;password-encoder hash="md5"/&gt;
  
      &lt;/authentication-provider&gt;
  &lt;/authentication-manager&gt;
  </code></pre>
  
  <p></p>
  
  <p>I am new to Spring Security. Hope someone could help me out.</p>
  
  <p><strong>EDIT</strong></p>
  
  <pre><code>@Component
  public class MyAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
  
  @Autowired
  AppUserDAO appUserDAO;
  
  @Override
  public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
      Authentication authentication) throws IOException, ServletException {
  
       SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      String a = df.format(new Date());
  
  
      System.out.println(authentication.getName()+"@@@ "+a);
  
      Authentication auth = SecurityContextHolder.getContext().getAuthentication();
  
          String username = auth.getPrincipal().toString();
  
          appUserDAO.updateLastLoginAndIp(a, username);
  
  
  }
  }
  </code></pre>