Recover logged in user

Asked

Viewed 1,762 times

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, only I want that at the time of doing this insertion I capture which user did this insertion, I want to get the usual. How can I do that?

Code of the button I insert:

<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() makes the insertion:

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

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

I want to know which user did this insertion according to each user who logs in to my page.

This is my login() method, when right 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 "";
}
  • Ivan, post your login code to the system and remember to hide any information that might compromise your application’s security.

  • Why do you want to do an Audit on your system ? If that is your question follow link. https://michelzanini.wordpress.com/2008/09/22/aop-na-pratica/

  • I edited my question by putting the login method()

3 answers

1

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

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

session.setAttribute("usuario", usuario);

And then rescue him whenever he wants:

Usuario usuarioLogado = (Usuario) session.getAttribute("usuario");

0

To enter something in the bank at the time the user authenticates, I do it directly in my service class.

@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 while browsing the user, recommend a session bean

0

I believe your method doLogin() be in a ManagedBean correct? Just enter your managedbean that logs in as a Property of the other managedbeans to have 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();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.