Java Spring Boot,MySQL:如何使用哈希密码进行数据库身份验证?
你好,我有一个Spring启动应用程序,它只有一个管理员,没有用户。 我已经创建了一个名为“administrator”的表,其中包含用户名和带有哈希密码的字符串。(我将密码放入转换器中以执行此操作。) 现在我的问题是我应该在Java Spring Boot,MySQL:如何使用哈希密码进行数据库身份验证?,java,hash,spring-security,spring-boot,password-encryption,Java,Hash,Spring Security,Spring Boot,Password Encryption,你好,我有一个Spring启动应用程序,它只有一个管理员,没有用户。 我已经创建了一个名为“administrator”的表,其中包含用户名和带有哈希密码的字符串。(我将密码放入转换器中以执行此操作。) 现在我的问题是我应该在WebSecurityConfiguration.java中更改什么,以便代码使用我在登录表单中编写的字符串检查数据库中的哈希值,以便能够登录 我的代码: @Autowired public void configureGlobal(AuthenticationManage
WebSecurityConfiguration.java
中更改什么,以便代码使用我在登录表单中编写的字符串检查数据库中的哈希值,以便能够登录
我的代码:
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("admin").password("admin").roles("ADMIN");
}
我发现了以下问题,但我仍然不知道该怎么办:
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(this.dataSource)
.usersByUsernameQuery("select email, password, active from user where email=?")
.authoritiesByUsernameQuery("select u.email, r.role from user u inner join user_role ur on(u.user_id=ur.user_id) inner join role r on(ur.role_id=r.role_id) where u.email=?").passwordEncoder(passwordencoder());
}
@Bean(name="passwordEncoder")
public PasswordEncoder passwordencoder(){
return new BCryptPasswordEncoder();
}
它看起来是这样的:
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(this.dataSource)
.usersByUsernameQuery("select email, password, active from user where email=?")
.authoritiesByUsernameQuery("select u.email, r.role from user u inner join user_role ur on(u.user_id=ur.user_id) inner join role r on(ur.role_id=r.role_id) where u.email=?").passwordEncoder(passwordencoder());
}
@Bean(name="passwordEncoder")
public PasswordEncoder passwordencoder(){
return new BCryptPasswordEncoder();
}
你看过医生了吗?我查看了它,但它并没有真正显示如何进行身份验证。我读到我需要对密码进行编码。但我不知道我必须做什么更改,它对我在WebSecurity中定义的密码进行编码,保存在数据库中,并能够在我尝试授权时从数据库中对其进行编码。例如,请参阅并使用
PasswordEncoder
作为BCryptPasswordEncoder
,因此,您可以将Spring Security配置为密码的单向编码器。密码永远不应该被解码,这是安全最佳实践之一。你读过文档了吗?我查看了它,但它并没有真正显示如何进行身份验证。我读到我需要对密码进行编码。但我不知道我必须做什么更改,它对我在WebSecurity中定义的密码进行编码,保存在数据库中,并能够在我尝试授权时从数据库中对其进行编码。例如,请参阅并使用PasswordEncoder
作为BCryptPasswordEncoder
,因此,您可以将Spring Security配置为密码的单向编码器。密码永远不应该被解码,这是安全最佳实践之一。