使用 Hibernate 进行用户身份验证
我是冬眠新手。 我想用 java 和 hibernate 构建一个简单的身份验证/登录系统。
假设我有一个 User 类,
public class User {
private int id;
private String username;
private String passwordHash;
...
}
现在我有一个 DAO 来存储新用户,并获取所有用户(作为列表)。现在我想知道,是否有可能获得没有passwordHash字段的用户列表(出于安全原因)?
如果是配置问题就好了。
另一个想法是将 User 类拆分为
public class User {
private int id;
private String username;
...
}
public class UserWithPassword extends User {
private String passwordHash;
...
}
所以我可以使用 UserWithPassword 将新用户存储到数据库中并使用 User 类查询所有用户列表(无密码)。
还有其他建议吗?
im new in hibernate.
I would like to build a simple autentication / login system in java with hibernate.
So let's say, i have a class User
public class User {
private int id;
private String username;
private String passwordHash;
...
}
Now i have a DAO to store a new User, and to get all users (as a list). Now im wondering, if its possible to get a list of users without the passwordHash field (for security reason)?
It would be nice if it was a question of configuration.
An other idea would be to split the User class into
public class User {
private int id;
private String username;
...
}
public class UserWithPassword extends User {
private String passwordHash;
...
}
So i could use UserWithPassword to store a new user into the database and use
the User class to query the list of all users (without password).
Any other suggestion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的拆分类将不起作用,因为您必须将类链接到 Hibernate。
您的 DAO 不必返回类本身。您可以编写这样的 HQL 查询:
看到了吗?
那么你的 DAO 将有一个类似 public Collection getUserNames() 的方法
Your split class won't work because you have to link a class to Hibernate.
Your DAO doesn't have to return the class itself. You can write an HQL query such:
See?
Then your DAO would have a method like public Collection getUserNames()
你可以使用
java.util.List temp = hibernateTemplate.find("从用户 u 中选择 u ");
您可以从 temp 获取所有用户;
但如果你想进行身份验证,你可以使用 spring security,我建议
you can use
java.util.List temp = hibernateTemplate.find("select u from user u ");
you can take all user from temp;
but if you want authenticate,you can use spring security,i suggest