Retrieve logged in user

4

I have Java Web application, but I want to do the following:

When I log into my application with a certain user then I need to do some operations, such as saving some information in the database, but I want it at the time I do this insertion, I'll capture which user made this insertion , I want to get the user. How can I do this?

Button code that does the insert action:

<p:commandLink id="btn_save_users_modal"
               action="#{messageBean.insert()}"
               styleClass="btn btn-success"
               update=":message_form"
               validateClient="true">
    <i class="fa fa-check fa-fw" /> #{bundle['system.ui.label.save']}
 </p:commandLink>

Method below messageBean.insert() inserts:

public void insert() {       
    try {    
        messageFacade.save(message);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Here, for example, I want to insert this object of my message and with the user that I just logged into my page.

I want to know which user made this insertion according to each user who logged into my page.

This is my login method (), when the page on the page makes some validations.

public String doLogin() {

    // Força a JVM para pt-BR, assim a formatação numérica fica no formato
    // brasileiro
    Locale.setDefault(new Locale("pt", "BR"));

    // Validações

    if (this.username == null || this.username.equals("")) {
        MessageGrowl.warn(
                MessageProperties.getString("message.loginVazio"), false);
    } else if (this.password == null || this.password.equals("")) {
        MessageGrowl
                .warn(MessageProperties.getString("message.passwordVazio"),
                        false);
    } else {

        // String hashedPassword = SecurityUtils.getHashedString(password);

        this.loggedUser = loginUserFacade.getValideUser(username,password);

        if (this.loggedUser != null) {
            isAuthenticated = true;
            redirectIfAlreadyLogged();

        }

        else {

            MessageGrowl.error(MessageProperties
                    .getString("login.autenticacao"));
            clear();
        }
    }

    return "";
}
    
asked by anonymous 11.06.2015 / 22:35

3 answers

1

When the user logs in, you can add them to the HTTP session:

HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
    .getExternalContext().getSession(true);

session.setAttribute("usuario", usuario);

And then redeem it anytime:

Usuario usuarioLogado = (Usuario) session.getAttribute("usuario");
    
22.06.2017 / 13:21
0

To insert something into the database at the time the user authenticates, I do it right in my class of service.

@Transactional
public Usuario verificaLoginSenha(String email, String senha) {
    Usuario usuario = usuarioDao.verificaLoginSenha(email,senha);
    if(usuario!=null){
        if(usuario.getCadastroAtivo()){
            logAutenticacaoService.salvar(new LogAutenticacao(new Date(),usuario));             
        }
    }
    return usuario;
}

For other checks during user browsing, I recommend a session Bean

    
15.04.2016 / 22:15
0

I believe your doLogin() method is in ManagedBean correct? Just insert your managedbean that logs in as a property of the other managedbeans to get the user logged in.

Ex:

@ManagedProperty(value = "#{loginMB}")
private LoginMB loginMB;
public void setLoginMB(LoginMB loginMB) {this.loginMB = loginMB;}

public void insert() {       
    //pegando o usuario: loginMB.getUsername();
}
    
14.01.2016 / 12:17