Spring 启动 JDBC 身份验证不工作
Spring Boot JDBC authentication not working
我创建了一个简单的 JDBC 身份验证服务。
安全配置:
package com.zsl.qrav.backend.BackendApplication;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private DataSource dataSource;
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication()
.dataSource(dataSource)
.passwordEncoder(passwordEncoder())
.usersByUsernameQuery("select username, password, enabled from users where username=?")
.authoritiesByUsernameQuery("select username, role from users where username=?")
;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin().permitAll()
.and()
.logout().permitAll();
}
}
这是我的数据库:
+---------+----------+---------------------------------------------------------------+---------+-----------+
| user_id | username | password | enabled | role |
+---------+----------+---------------------------------------------------------------+---------+-----------+
| 1 | qrav | y$SYZVfjzt/iwXscoTPp5sf.in3fZ8K9OUNWBWP35T5zh9V.aILxpA2 | 1 | ROLE_USER |
+---------+----------+---------------------------------------------------------------+---------+-----------+
密码只是“密码”散列使用:
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
String pass = "password";
String encoded = encoder.encode(pass);
System.out.println(encoded);
问题是,每当我尝试使用凭据 qrav:password 登录时,它只是说凭据不正确。
MySQL 连接和数据库都没有问题(因为它几乎是 YouTube 教程中的复制粘贴数据库)。
我不知道出了什么问题,所以非常感谢您的帮助。
如果您使用的版本 < 5.2,则问题在于生成的哈希值。它应该以 a$
开头。例如,以下是 10 轮生成的: password = azE9z3rfDEi7WvF.6Sy2Y.UV2MoVTTkX/AzVXEGpjzG3cZ5EsA1YK
Spring 进行以下检查:
private Pattern BCRYPT_PATTERN = Pattern.compile("\A\a?\$\d\d\$[./0-9A-Za-z]{53}");
这意味着哈希必须以 a
开头,而您的哈希以 y
开头。
无参数构造函数 BCryptPasswordEncoder 生成一个基于 Bcrypt 版本 2a 的散列,但存储在您的数据库中的是版本 2y,这就是问题所在。您需要检查您是否在 BCryptPasswordEncoder 构造函数中指定了版本。
我创建了一个简单的 JDBC 身份验证服务。
安全配置:
package com.zsl.qrav.backend.BackendApplication;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private DataSource dataSource;
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication()
.dataSource(dataSource)
.passwordEncoder(passwordEncoder())
.usersByUsernameQuery("select username, password, enabled from users where username=?")
.authoritiesByUsernameQuery("select username, role from users where username=?")
;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin().permitAll()
.and()
.logout().permitAll();
}
}
这是我的数据库:
+---------+----------+---------------------------------------------------------------+---------+-----------+
| user_id | username | password | enabled | role |
+---------+----------+---------------------------------------------------------------+---------+-----------+
| 1 | qrav | y$SYZVfjzt/iwXscoTPp5sf.in3fZ8K9OUNWBWP35T5zh9V.aILxpA2 | 1 | ROLE_USER |
+---------+----------+---------------------------------------------------------------+---------+-----------+
密码只是“密码”散列使用:
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
String pass = "password";
String encoded = encoder.encode(pass);
System.out.println(encoded);
问题是,每当我尝试使用凭据 qrav:password 登录时,它只是说凭据不正确。
MySQL 连接和数据库都没有问题(因为它几乎是 YouTube 教程中的复制粘贴数据库)。
我不知道出了什么问题,所以非常感谢您的帮助。
如果您使用的版本 < 5.2,则问题在于生成的哈希值。它应该以 a$
开头。例如,以下是 10 轮生成的: password = azE9z3rfDEi7WvF.6Sy2Y.UV2MoVTTkX/AzVXEGpjzG3cZ5EsA1YK
Spring 进行以下检查:
private Pattern BCRYPT_PATTERN = Pattern.compile("\A\a?\$\d\d\$[./0-9A-Za-z]{53}");
这意味着哈希必须以 a
开头,而您的哈希以 y
开头。
无参数构造函数 BCryptPasswordEncoder 生成一个基于 Bcrypt 版本 2a 的散列,但存储在您的数据库中的是版本 2y,这就是问题所在。您需要检查您是否在 BCryptPasswordEncoder 构造函数中指定了版本。