密码编码-BCrypt-不授权哈希密码,仅对纯文本授权
问题内容:
我有一个宠物项目,我一直在慢慢努力,最近我决定为用户合并BCrypt密码编码。
我遇到的问题是,它启用后,它从未授权任何用户,而且我不知道为什么。
希望你们能提供帮助。
以下是我的SecurityConfig.java文件中的配置
@Autowired
private PasswordEncoder passwordEncoder;
@Override
protected void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(passwordEncoder);
}
@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
我的控制器注册了新用户并将其登录:
@RequestMapping(value = "/user_create", method = RequestMethod.POST)
public String createUser(@ModelAttribute User user) {
user.setPlain(user.getPassword());
user.setPassword(BCrypt.hashpw(user.getPassword(),BCrypt.gensalt()));
userInterface.saveUser(user);
return "redirect:/";
}
创建新用户的工作非常完美(将其加载到数据库中),结果如下所示:
ID | USERNAME | PLAIN_PASSWORD | HASHED_PASSWORD
1 | hash | hash | $2a$10$BYpLI.Dd06HS1myhxxeyb.lZ/74lfYVXSk9Tuy.iYTzy4t2Yx5FtW
我无法授权用户的原因是什么?
更新1
我还添加了另一个用户(通过后端),所以我有2个用户看起来像这样
ID | USERNAME | PLAIN_PASSWORD | HASHED_PASSWORD
1 | hash | hash | $2a$10$BYpLI.Dd06HS1myhxxeyb.lZ/74lfYVXSk9Tuy.iYTzy4t2Yx5FtW
2 | admin | admin | admin
通过上面的设置,我可以使用“ admin”和“ admin”登录,即使它没有被散列,甚至可以打开密码编码器。
更新2
数据库由Hibernate管理,设置如下:
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue
private Integer id;
@NotNull
@Column(name = "username")
private String login;
@NotNull
@Column(name = "hashed_password")
private String password;
@Column(name = "plain_password")
private String plain;
// GETTERS AND SETTERS //
}
问题答案:
我已经确定了问题....而且非常简单。
更改了以下内容:
@Override
protected void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(passwordEncoder);
}
到下面
@Override
protected void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(customUserService).passwordEncoder(passwordEncoder).and
jdbcAuthentication().dataSource(dataSource);
}
从HttpSecurity配置中删除了它。