title: Get username in session Spring Security
body: <p>I am in doubt as to how to get the user name in the session. I am using Spring Security 4.2</p>
  
  <p>I have my Class Usuario</p>
  
  <pre><code>@Entity
  @Data
  public class Usuario {
  
      @Id @GeneratedValue
      private Integer id;
  
      private String login;
      private String senha;
      private String papel;
  
  }
  </code></pre>
  
  <p>My class UsuarioController</p>
  
  <pre><code>@Named
  @ViewScoped
  public class UsuarioController {
  
      @Autowired
      private UsuarioRepository usuarioRepository;
  
      @Getter @Setter
      private List&lt;Usuario&gt; usuarios;
  
      @Getter @Setter
      private Usuario usuario = new Usuario();
  
  }
  </code></pre>
  
  <p>And my class SecurityConfig, which plays the role of the filter, already built into Spring Security.</p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      private UsuarioRepository usuarioRepository;
  
      @Override
      protected void configure(HttpSecurity http) {
          try {
              http.csrf().disable();
              http
                  .userDetailsService(userDetailsService())
                  .authorizeRequests()
                  .antMatchers("/").permitAll()
                  .antMatchers("/cliente.jsf").permitAll()
                  .anyRequest().authenticated()
                  .and()
                  .formLogin()
                  .loginPage("/login.jsf")
                  .permitAll()
                  .failureUrl("/login.jsf?error=true")
                  .defaultSuccessUrl("/cliente.jsf")
                  .and()
                  .logout()
                  .logoutSuccessUrl("/login.jsf");
          }
          catch (Exception ex) {
              throw new RuntimeException(ex);
          }
      }
  
      @Override
      protected UserDetailsService userDetailsService() {
  
          List&lt;Usuario&gt; usuarios = usuarioRepository.findAll();
  
          List&lt;UserDetails&gt; users = new ArrayList&lt;&gt;();
  
          for(Usuario u: usuarios){
              UserDetails user = new User(u.getLogin(), u.getSenha(), AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_"+u.getPapel()));
              users.add(user);
          } return new InMemoryUserDetailsManager(users);
  
      }
  }
  </code></pre>
  
  <p>I already researched other posts in the forum, did not help, any tips? Do I need to create another class?</p>