I am in doubt as to how to get the user name in the session. I am using Spring Security 4.2
I have my Class Usuario
@Entity
@Data
public class Usuario {
@Id @GeneratedValue
private Integer id;
private String login;
private String senha;
private String papel;
}
My class UsuarioController
@Named
@ViewScoped
public class UsuarioController {
@Autowired
private UsuarioRepository usuarioRepository;
@Getter @Setter
private List<Usuario> usuarios;
@Getter @Setter
private Usuario usuario = new Usuario();
}
And my class SecurityConfig, which plays the role of the filter, already built into Spring Security.
@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<Usuario> usuarios = usuarioRepository.findAll();
List<UserDetails> users = new ArrayList<>();
for(Usuario u: usuarios){
UserDetails user = new User(u.getLogin(), u.getSenha(), AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_"+u.getPapel()));
users.add(user);
} return new InMemoryUserDetailsManager(users);
}
}
I already researched other posts in the forum, did not help, any tips? Do I need to create another class?