用Hibernate登录并存储用户名记录 [英] Login with Hibernate and store username logged

查看:104
本文介绍了用Hibernate登录并存储用户名记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的项目云,登录后的注册用户可以下载和上传文件。
我正在使用Eclipse Java EE,JSF和Hibernate进行开发。
在我的项目中,我可以登录到用户,如果数据正确,我打开用户菜单,但无法找出存储关于用户登录信息的位置。目前尚不清楚会议的概念。我已经阅读了关于Hibernate和会话的几件事情,但我还不明白。我需要保持至少当前登录的用户的用户名,使用什么对象?和哪里?你可以帮帮我吗?谢谢

I'm developing a simple project cloud in which a registered user after login can download and upload files. I'm developing with Eclipse Java EE, JSF and Hibernate. In my project I can sign in to the user and if the data are correct I open the user menu but can not figure out where to store the information about the user logged. It is not clear so the concept of session. I've read several things about Hibernate and the session but I do not understand yet. I need to keep at least the username of the user currently logged in, use what object? and where?. Could you help me? thanks

@ManagedBean
@SessionScoped
public class Login {

private String userId;
private String password;

public Login() {}

public String getUserId() {
    return userId;
}
public void setUserId(String userId) {
    this.userId = userId;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}

public String loginUser (){
    SessionHandler s = new SessionHandler();
    Session sessione = s.getDb();
    sessione.beginTransaction();
    Query query = sessione.createQuery( "Select password from Utenti where username= :id" );
    System.out.println("Qui lo prende: " + this.getUserId());
    query.setParameter("id", userId);
    System.out.println("tag debug");
    List<Utenti> res= query.list();
    if(password.equals(res.get(0))){
        this.setUserId(userId);
        return "first";

    }
    else
        return "noaccess";
}

我需要在TreeBean中使用userid。这是我的TreeBean:

I need to use userid in TreeBean. This is my TreeBean:

@ManagedBean
@SessionScoped
public class TreeBean {

@ManagedProperty(value="#{login.userId}")
private String user;

private TreeNode root;
private TreeNode selectedNode;
private Session db;

public Session getDb() {
    return db;
}

public void setDb(Session db) {
    this.db = db;
}

public void setUser(String user) {
    this.user = user;
}

public String getUser(){
    return user;
}

public TreeBean() {  
    root=new DefaultTreeNode("Root", null);
    System.out.println("Prova"+ user);
    recursive(0, root);

}    

在.xhtml文件中可以读取没有任何问题的用户标识。为什么?
谢谢

In .xhtml files can read userid without any problems. Why? Thanks

推荐答案

您需要创建会话范围的ManagedBean。在登录时,您可以在该ManagedBean中设置用户对象(您从数据库中获得的)。然后,您将可以通过所有应用程序访问它。

You need to create a ManagedBean of session scope. On login, you set the user object (that you got from DB) in that ManagedBean. And then, you will be able to access it through all the application.

另一种方法是直接在会话中设置用户对象:

Alternative way, is to set the user object directly in session:

ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
Map<String, Object> sessionMap = externalContext.getSessionMap();

然后:

And then:

sessionMap.put("attributeName", whateverYouWantToSaveInSession);

但我会选择第一种方法。

But I would choose the first approach.

这篇关于用Hibernate登录并存储用户名记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆