title: How to save ip address to a DB from authenticated user with Spring security?
body: <p>I need to keep track of the ip address when users log in my spring application. </p>
  
  <p>security.xml:</p>
  
  <pre><code>&lt;authentication-manager alias="authenticationManager"&gt;
  &lt;authentication-provider user-service-ref="userService"&gt;
      &lt;password-encoder ref="passwordEncoder"&gt;
          &lt;salt-source user-property='username' /&gt;
      &lt;/password-encoder&gt;
  &lt;/authentication-provider&gt;
  </code></pre>
  
  <p>with bean:</p>
  
  <pre><code>&lt;beans:bean id="passwordEncoder"
      class="org.springframework.security.authentication.encoding.ShaPasswordEncoder"&gt;
      &lt;beans:constructor-arg value="512" /&gt;
  &lt;/beans:bean&gt;
  </code></pre>
  
  <p>I've a custom userService with a method loadUserByUsername() returning a custom UserDetails. This method get the UserDetails from a database, via a DAO. The UserDetails contains stuffs related to the user such as his username, password, authorities, email address, but also application-specific variables. I need to access these variables in my JSP pages.</p>
  
  <p>I want to save into a database (via a call to a method in a custom service, which call a DAO method) the ip address, timestamp and user id when a user is authenticated successfully in my application.</p>
  
  <p>I'm not sure what to do: should I implement a custom authentication provider? extends DaoAuthenticationProvider? or AbstractUserDetailsAuthenticationProvider? or something else?</p>
  
  <p>More general questions:</p>
  
  <p>A. Where can I add a method to call once a user provides the right credentials?</p>
  
  <p>B. How can I retrieve the ip address of the user? (knowing that tomcat runs behind apache in a reverse-proxy).</p>
  
  <p>I tried to look at related questions/answers, but it just made me more confused. If someone could provide a very simple step-by-step implementation, it would be awesome. thanks!</p>