java - 如何使用 Apache Shiro 将哈希 pws 存储到数据库中?

标签 java database dao shiro jdbcrealm

我一直在寻找所有地方,但对我想做的事情并不走运。

我希望对我的用户密码进行哈希处理和加盐,并将它们存储到数据库中。问题是,我该如何存储它们?

我看过这个http://shiro.apache.org/realm.html#Realm-authentication我找到了类似的答案,但这没有意义。

import org.apache.shiro.crypto.hash.Sha256Hash;
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
...

//We'll use a Random Number Generator to generate salts.  This
//is much more secure than using a username as a salt or not
//having a salt at all.  Shiro makes this easy.
//
//Note that a normal app would reference an attribute rather
//than create a new RNG every time:
RandomNumberGenerator rng = new SecureRandomNumberGenerator();
Object salt = rng.nextBytes();

//Now hash the plain-text password with the random salt and multiple
//iterations and then Base64-encode the value (requires less space than Hex):
String hashedPasswordBase64 = new Sha256Hash(plainTextPassword, salt, 1024).toBase64();

User user = new User(username, hashedPasswordBase64);
//save the salt with the new account.  The HashedCredentialsMatcher
//will need it later when handling login attempts:
user.setPasswordSalt(salt);
userDAO.create(user);

据我所见,用户和“UserDAO”目前都不存在,并且所有这些示例似乎都使用较旧的 Shiro 示例。

当我查看我阅读的“PasswordService”javadoc 时

帐户创建或密码重置

Whenever you create a new user account or reset that account's password,

我们必须将最终用户提交的原始/明文密码值转换为 存储起来更安全的字符串格式。您可以通过调用 encryptPassword(Object) 方法创建更安全的值。例如:

 String submittedPlaintextPassword = ...
 String encryptedValue = passwordService.encryptPassword(submittedPlaintextPassword);
 ...
 userAccount.setPassword(encryptedValue);
 userAccount.save(); //create or update to your data store

Be sure to save this encrypted password in your data store 
and never the original/raw submitted password.

但是“userAccount”是什么?

很多时候文档非常模糊。

但是我确实注意到有一个“SubjectDAO”类,但是没有 UserDAO 类...

是的,我对下一步该做什么感到困惑,所以如果有人能帮助我,我将不胜感激!

非常感谢!

最佳答案

似乎文档将 UserUserDAO 引用为您自己的用户模型实体(表)和用户数据访问层实体(要保存、更新、删除和删除的类)取回)。这些不一定是 Apache Shiro 的一部分(原因是一些数据访问层可能在 RDBMS 中,一些在内存数据库中,一些甚至可能在属性文件中,为什么不呢?)

您必须实现 UserUserDAO 才能保存到您自己的持久性存储中。

UserAccount 也是您在注册用户帐户时使用的模型对象。就像 Gmail 注册一样。

您必须知道 Apache Shiro 只是一个安全层(身份验证、授权等)。持久性必须由您实现。

强烈建议您检查 Spring Data JPA 和 Spring Security。

关于java - 如何使用 Apache Shiro 将哈希 pws 存储到数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25777994/

相关文章:

java - JAVA 中的日期和时间选择器

java - 我的 Servlet 如何从 multipart/form-data 表单接收参数?

java - java中反序列化步骤

sql - Oracle 中的部分索引还是完整索引?

sql - 使用一张表和分开多表各有优缺点吗?

java - 在 java 中生成 X509v(1/3) 证书(有弹性的城堡)

mysql - 我应该将字段 PRICE 作为 int 还是作为 float int 存储在数据库中?

ms-access - querydefs 运行时错误 3265 : item not found in this collection

Java - DAL 项目中的数据库 URL

Java DAO模式多表原子事务