title: org.springframework.security.core.userdetails.User cannot be cast to MyUserDetails
body: <p>I am getting class cast exception when I implement User class of spring security. I want to add few additional details in MyUserDetails (id) but I am not able to get the result.</p>
  
  <p>This question is answered <a href="https://stackoverflow.com/questions/12078404/org-springframework-security-core-userdetails-user-cannot-be-cast-to-myuser">here</a> but then two I am getting error, don't know where I am missing. </p>
  
  <p>Below is my code :</p>
  
  <p><strong>SecurityConfig.java:</strong></p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      @Qualifier("authenticationProvider")
      AuthenticationProvider authenticationProvider;
  
      @Autowired
      DataSource dataSource;
  
      @Autowired
      public void configAuthentication(AuthenticationManagerBuilder auth)
              throws Exception {
  
          auth.jdbcAuthentication()
                  .dataSource(dataSource)
                  .passwordEncoder(passwordEncoder())
                  .usersByUsernameQuery(
                          "select username,password, enabled from users where username=?")
                  .authoritiesByUsernameQuery(
                          "select username, role from user_roles where username=?");
  
      }
  
      @Bean
      public PasswordEncoder passwordEncoder() {
          PasswordEncoder encoder = new BCryptPasswordEncoder();
          return encoder;
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
  
          http.headers().cacheControl().and().xssProtection().and()
                  .frameOptions().and();
  
          http.authorizeRequests()
                  .antMatchers("/sadmin/**")
                  .access("hasRole('ROLE_SADMIN')")
                  .antMatchers("/admin/**")
                  .access("hasRole('ROLE_ADMIN') or hasRole('ROLE_SADMIN')")
                  .antMatchers("/")
                  .access("hasRole('ROLE_ADMIN') or hasRole('ROLE_SADMIN') or hasRole('ROLE_USER')")
                  .and().formLogin()
                  .loginProcessingUrl("/j_spring_security_check")
                  .loginPage("/login").failureUrl("/login?error")
                  .usernameParameter("username").passwordParameter("password")
                  .and().logout().logoutUrl("/j_spring_security_logout")
                  .logoutSuccessUrl("/login?logout").and().exceptionHandling()
                  .accessDeniedPage("/403").and().csrf();
  
          // session management URL for reference
          // https://github.com/spring-projects/spring-security-javaconfig/blob/master/spring-security-javaconfig/src/test/groovy/org/springframework/security/config/annotation/web/configurers/NamespaceSessionManagementTests.groovy
      }
  </code></pre>
  
  <p><strong>MyUserDetails.java:</strong></p>
  
  <pre><code>public class MyUserDetails extends User {
      private int id;
  
      public MyUserDetails(String username, String password, boolean enabled,
              boolean accountNonExpired, boolean credentialsNonExpired,
              boolean accountNonLocked,
              Collection&lt;? extends GrantedAuthority&gt; authorities) {
          super(username, password, enabled, accountNonExpired,
                  credentialsNonExpired, accountNonLocked, authorities);
  
      }
      // getter setter ....
  }
  </code></pre>
  
  <p><strong>CustomUserDetailsService.java:</strong></p>
  
  <pre><code>@Service("userDetailsService")
  public class CustomUserDetailsService extends JdbcDaoImpl{
      @Override
      protected List&lt;UserDetails&gt; loadUsersByUsername(String username) {
          return getJdbcTemplate().query(super.getUsersByUsernameQuery(), new String[] { username },
                  new RowMapper&lt;UserDetails&gt;() {
                      public UserDetails mapRow(ResultSet rs, int rowNum) throws SQLException {
                          String username = rs.getString("username");
                          String password = rs.getString("password");
                          boolean enabled = rs.getBoolean("enabled");
                          boolean accountNonExpired = rs.getBoolean("accountNonExpired");
                          boolean credentialsNonExpired = rs.getBoolean("credentialsNonExpired");
                          boolean accountNonLocked = rs.getBoolean("accountNonLocked");
                          int id = rs.getInt("id");
  
                          MyUserDetails myUserDetails = new MyUserDetails(username, password, enabled, accountNonExpired, credentialsNonExpired,
                                  accountNonLocked, AuthorityUtils.NO_AUTHORITIES);
  
                          myUserDetails.setId(id);
                          return myUserDetails;
                      }
  
                  });
      }
  
      @Override
      protected UserDetails createUserDetails(String username, UserDetails userFromUserQuery,
              List&lt;GrantedAuthority&gt; combinedAuthorities) {
          String returnUsername = userFromUserQuery.getUsername();
  
          if (!isUsernameBasedPrimaryKey()) {
              returnUsername = username;
          }
  
          final MyUserDetails myUserDetails = new MyUserDetails(returnUsername, userFromUserQuery.getPassword(), userFromUserQuery.isEnabled(),
                  userFromUserQuery.isAccountNonExpired(), userFromUserQuery.isCredentialsNonExpired(),
                  userFromUserQuery.isAccountNonLocked(), combinedAuthorities);
  
          myUserDetails.setId(((MyUserDetails) userFromUserQuery).getId());
          return myUserDetails;
      }
  }
  </code></pre>
  
  <p><strong>Code to get MyUserDetails:</strong></p>
  
  <pre><code>MyUserDetails userDetails = (MyUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
  </code></pre>
  
  <p><strong>pom.xml:</strong></p>
  
  <pre><code>&lt;org.springframework.version&gt;4.0.5.RELEASE&lt;/org.springframework.version&gt;
  &lt;spring.security.version&gt;4.0.1.RELEASE&lt;/spring.security.version&gt;
  </code></pre>
  
  <p><strong>Stacktrace:</strong></p>
  
  <pre><code>SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/housing] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: org.springframework.security.core.userdetails.User cannot be cast to com.esociety.dto.MyUserDetails] with root cause
  java.lang.ClassCastException: org.springframework.security.core.userdetails.User cannot be cast to com.esociety.dto.MyUserDetails
      at com.esociety.controller.LoginController.dashboard(LoginController.java:38)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
      at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
      at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:316)
      at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:126)
      at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:90)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
      at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
      at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:122)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
      at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
      at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:168)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
      at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:48)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
      at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:205)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
      at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:120)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
      at org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:96)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
      at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
      at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:91)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
      at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:53)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
      at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:213)
      at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:176)
      at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
      at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
      at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:655)
      at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      at java.lang.Thread.run(Unknown Source)
  </code></pre>