使用 Hibernate 进行用户身份验证

发布于 2024-12-14 05:09:37 字数 593 浏览 2 评论 0原文

我是冬眠新手。 我想用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

つ低調成傷 2024-12-21 05:09:37

您的拆分类将不起作用,因为您必须将类链接到 Hibernate。

您的 DAO 不必返回类本身。您可以编写这样的 HQL 查询:

select username
from User

看到了吗?

那么你的 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:

select username
from User

See?

Then your DAO would have a method like public Collection getUserNames()

冰之心 2024-12-21 05:09:37

你可以使用
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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文