title: Spring Security - Get username in AuthenticationSuccessHandler without UserDetails/UserDetailsService
body: <p>I'm trying to set the LastLogin time for a user in a custom AuthenticationSuccessHandler, however I don't know of a way of retrieving the username (since all the authority functions seem to return null because I'm not working with UserDetails).
  The user data is stored in a MYSQL table and I'm using Hibernate to retrieve/create/update users. Within my application I'm using a self-written User class that doesn't have anything to do with the Spring User class. I don't have any custom UserDetails/UserDetailsService and I would like to avoid them, since I cannot change the DB table layout (as in add additional values)</p>
  
  <p>The AuthenticationSuccessHandler looks like this:</p>
  
  <pre><code>public class PostSuccessfulAuthenticationHandler extends SimpleUrlAuthenticationSuccessHandler {
  @Autowired
  private UserService userService;
  @Override
  public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
          Authentication authentication) throws ServletException, IOException 
          {
              userService.trackUserLogin(authentication.getName()); //Doesn't work, getName seems to return null
              super.onAuthenticationSuccess(request, response, authentication);
          }
  }
  </code></pre>
  
  <p>My applicationContext-security.xml looks like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &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"
  xmlns:p="http://www.springframework.org/schema/p"
  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.0.xsd"&gt;
  
  &lt;global-method-security pre-post-annotations="enabled"/&gt;
  &lt;!-- authentication-success-handler-ref='authSuccHandler' --&gt;
  &lt;http use-expressions="true" disable-url-rewriting="true"&gt;
      &lt;intercept-url pattern="..." access="hasRole('ROLE_USER')" /&gt;
      &lt;form-login login-page="/index.htm"
                  authentication-failure-handler-ref='authFailureHandler'
                  authentication-success-handler-ref='authSuccHandler'
                  default-target-url='...'
                  always-use-default-target='true'/&gt;
      &lt;logout logout-success-url="..."/&gt;
      &lt;session-management invalid-session-url="/index.htm"&gt;
          &lt;concurrency-control max-sessions="2" error-if-maximum-exceeded="true" /&gt;
      &lt;/session-management&gt;
  &lt;/http&gt;
  
  &lt;authentication-manager&gt;
      &lt;authentication-provider&gt;
          &lt;jdbc-user-service data-source-ref="mysqldataSource" 
                      authorities-by-username-query="select username, authority from benutzer where username = ?"
                      users-by-username-query="select username, password, enabled from benutzer where username = ?"/&gt;
      &lt;/authentication-provider&gt;
  &lt;/authentication-manager&gt;
  
  &lt;/beans:beans&gt;
  </code></pre>
  
  <p>The login itself works fine (even if I just comment out the trackUserLogin line in my AuthenticationSuccessHandler) which leads me to believe that there has to be a way to get that username. Can anyone help?</p>