title: How to get User ID from CustomUser on Spring Security
body: <p>usign Spring Security ,I'm trying to get the user id from my CustomUser instance returned from loadUserByUsername method on my CustomUserDetailsService, like I do to get get the Name (get.Name()) with Authentication. Thanks for any tips!</p>
  
  <p>This is how I get the current name of logged user:</p>
  
  <pre><code>Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
  String name = authentication.getName();
  </code></pre>
  
  <p>And this is the CustomUser</p>
  
  <pre><code>public class CustomUser extends User {
  
      private final int userID;
  
      public CustomUser(String username, String password, boolean enabled, boolean accountNonExpired,
                        boolean credentialsNonExpired,
                        boolean accountNonLocked,
                        Collection&lt;? extends GrantedAuthority&gt; authorities, int userID) {
          super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
          this.userID = userID;
      }
  }
  </code></pre>
  
  <p>And the loadUserByUsername method on my Service</p>
  
  <pre><code>@Override
  public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
  
      Usuario u = usuarioDAO.getUsuario(s);
  
      return new CustomUser(u.getLogin(), u.getSenha(), u.isAtivo(), u.isContaNaoExpirada(), u.isContaNaoExpirada(),
              u.isCredencialNaoExpirada(), getAuthorities(u.getRegraByRegraId().getId()),u.getId()
      );
  }
  </code></pre>