Get username in session jsf


I'm in doubt as to how to get the user's name in the session. I'm using Spring Security 4.2

I have my User Class

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import lombok.Data;

public class Usuario {

    @Id @GeneratedValue
    private Integer id;

    private String login;
    private String senha;
    private String papel;


My UserController class

import java.util.List;

import javax.faces.bean.ViewScoped;
import javax.inject.Named;

import lombok.Getter;
import lombok.Setter;

import org.springframework.beans.factory.annotation.Autowired;

public class UsuarioController {

    private UsuarioRepository usuarioRepository;

    @Getter @Setter
    private List<Usuario> usuarios;

    @Getter @Setter
    private Usuario usuario = new Usuario();


And my SecurityConfig class, which plays the role of the filter, already built into Spring Security.

public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private UsuarioRepository usuarioRepository;

    protected void configure(HttpSecurity http) {
        try {
        catch (Exception ex) {
            throw new RuntimeException(ex);

    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()));
        } return new InMemoryUserDetailsManager(users);


Should I create another class to retrieve the name, id of the user?

asked by anonymous 27.08.2017 / 17:47

0 answers