title: Spring Security: custom userdetails
body: <p>I'm pretty new to Java and Spring 3 (used primarily PHP the past 8 years). I've gotten spring security 3 to work with all the default userDetails and userDetailsService and I know I can access the logged in user's username in a controller by using:</p>
  
  <pre><code>Authentication auth = SecurityContextHolder.getContext().getAuthentication();
  String username = auth.getName(); //get logged in username
  </code></pre>
  
  <p>But there are two problems I can't figure out:</p>
  
  <ol>
  <li><p>There are a lot of other user details I would like stored when a user logs in (such as DOB, gender, etc.) and to be accessible via the controllers later on. What do I need to do so that the userDetails object that is created contains my custom fields?</p></li>
  <li><p>I'm already calling "HttpSession session = request.getSession(true);" at the top of each of my methods in my controller. Is it possible to store the logged in user's userDetails in a session upon login so that I don't need to also call "Authentication auth = SecurityContextHolder.getContext().getAuthentication();" at the beginning of every method?</p></li>
  </ol>
  
  <p>Security-applicationContext.xml:</p>
  
  <pre><code>&lt;global-method-security secured-annotations="enabled"&gt;&lt;/global-method-security&gt;     
  &lt;http auto-config='true' access-denied-page="/access-denied.html"&gt;
      &lt;!-- NO RESTRICTIONS --&gt;        
      &lt;intercept-url pattern="/login.html" access="IS_AUTHENTICATED_ANONYMOUSLY" /&gt;
      &lt;intercept-url pattern="/*.html" access="IS_AUTHENTICATED_ANONYMOUSLY"  /&gt; 
      &lt;!-- RESTRICTED PAGES --&gt;
      &lt;intercept-url pattern="/admin/*.html" access="ROLE_ADMIN" /&gt;
      &lt;intercept-url pattern="/member/*.html" access="ROLE_ADMIN, ROLE_STAFF" /&gt;
  
      &lt;form-login login-page="/login.html"
                  login-processing-url="/loginProcess"
                  authentication-failure-url="/login.html?login_error=1"
                  default-target-url="/member/home.html" /&gt;
      &lt;logout logout-success-url="/login.html"/&gt;
  &lt;/http&gt;
  
  &lt;authentication-manager&gt;
      &lt;authentication-provider&gt;
          &lt;jdbc-user-service data-source-ref="dataSource" authorities-by-username-query="SELECT U.username, UR.authority, U.userid FROM users U, userroles UR WHERE U.username=? AND U.roleid=UR.roleid LIMIT 1" /&gt;
          &lt;password-encoder hash="md5"/&gt;
      &lt;/authentication-provider&gt;
  &lt;/authentication-manager&gt;
  </code></pre>
  
  <p></p>
  
  <p>login.jsp:</p>
  
  <pre><code>&lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;
  &lt;%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %&gt;
  &lt;%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%&gt;
  
  &lt;tiles:insertDefinition name="header" /&gt;
  &lt;tiles:insertDefinition name="menu" /&gt;
  &lt;tiles:insertDefinition name="prebody" /&gt;
  
  &lt;h1&gt;Login&lt;/h1&gt;
  
  &lt;c:if test="${not empty param.login_error}"&gt;
      &lt;font color="red"&gt;&lt;c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}"/&gt;.&lt;br /&gt;&lt;br /&gt;&lt;/font&gt;
  &lt;/c:if&gt;
  &lt;form name="f" action="&lt;c:url value='/loginProcess'/&gt;" method="POST"&gt;
      &lt;table&gt;
          &lt;tr&gt;&lt;td&gt;User:&lt;/td&gt;&lt;td&gt;&lt;input type='text' name='j_username' value='&lt;c:if test="${not empty param.login_error}"&gt;&lt;c:out value="${SPRING_SECURITY_LAST_USERNAME}"/&gt;&lt;/c:if&gt;' /&gt;&lt;/td&gt;&lt;/tr&gt;
              &lt;tr&gt;&lt;td&gt;Password:&lt;/td&gt;&lt;td&gt;&lt;input type='password' name='j_password' /&gt;&lt;/td&gt;&lt;/tr&gt;
              &lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;&lt;input type="checkbox" name="_spring_security_remember_me" /&gt; Remember Me&lt;/td&gt;&lt;/tr&gt;
              &lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;&lt;input name="submit" type="submit" value="Login" /&gt;&lt;/td&gt;&lt;/tr&gt;
          &lt;/table&gt;
      &lt;/form&gt;
  
  &lt;tiles:insertDefinition name="postbody" /&gt;
  &lt;tiles:insertDefinition name="footer" /&gt;
  </code></pre>