我需要修改一个安全的登录代码:
@Override
public User login(String username, String password, Boolean rememberMe) {
log.info("Logging in username="+username);
UsernamePasswordToken token;
String hashed = BCrypt.hashpw(password, BCrypt.gensalt());
log.info("Hashed password=" + hashed);
token = new UsernamePasswordToken(username, hashed);
// ”Remember Me” built-in, just do this:
token.setRememberMe(rememberMe);
try {
// With most of Shiro, you'll always want to make sure you're working with the currently executing user,
// referred to as the subject
Subject currentUser = SecurityUtils.getSubject();
// Authenticate
//currentUser.login(token);
User user = userDAO.fetchUserByName(username, hashed);
return user;
} catch (org.apache.shiro.authc.AuthenticationException e) {
throw new AuthenticationException("Failure in authentication");
} catch (IllegalStateException e){
throw new AuthenticationException("Application is in a illegal state");
} catch (Exception e){
throw new AuthenticationException("Some other error during login was caught.");
}
}
在 DAO 级别:
- 使用用户名和哈希密码获取用户对象
但是,现在,存储在数据库中的密码是普通密码,我将用散列密码替换它。这里的问题是:
- BCrypt.hashpw() 方法生成不同的哈希,正如我在记录此代码时看到的那样。
所以问题是如何在每次更改时存储散列密码。
我的想法是
- 用户在 UI 中输入明文密码,在这个登录方法中,密码会被哈希,然后通过 fetchUserByName(username, hashed) 方法获取用户;但这似乎不是这种特殊的 Shiro 和 BCrypt 组合的解决方案。
处理这个问题的正确方法是什么?