【发布时间】:2020-07-02 16:40:56
【问题描述】:
我正在尝试对使用 Postgresql 数据库的 REST 控制器使用 JDBC 身份验证。 管理身份验证的 Configuration 类如下:
@Configuration
@EnableAutoConfiguration
public class JDBCSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
DataSource dataSource;
@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.usersByUsernameQuery("select username,password, enabled from users where username=?")
.authoritiesByUsernameQuery("select username, role from user_roles where username=?");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
//HTTP Basic authentication
.httpBasic()
.and()
.authorizeRequests()
.antMatchers(HttpMethod.GET, "/**").hasRole("USER")
.antMatchers(HttpMethod.POST, "/").hasRole("ADMIN")
.and()
.csrf().disable()
.formLogin().disable();
}
}
数据源配置应该没问题:
spring.datasource.url=jdbc:postgresql://localhost:5432/springdb
spring.datasource.username=user
spring.datasource.password=password
但是,当我尝试调用 Controller 的方法时,使用角色中的用户:
@RequestMapping(path= "/", method = RequestMethod.GET, produces = {"application/json"})
public List<Customer> find()
{
return repository.getCustomers();
}
然后会导致如下错误:
java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
at org.springframework.security.crypto.password.DelegatingPasswordEncoder$UnmappedIdPasswordEncoder.matches(DelegatingPasswordEncoder.java:244) ~[spring-security-core-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.security.crypto.password.DelegatingPasswordEncoder.matches(DelegatingPasswordEncoder.java:198) ~[spring-security-core-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration$LazyPasswordEncoder.matches(AuthenticationConfiguration.java:289) ~[spring-security-config-5.1.6.RELEASE.jar:5.1.6.RELEASE]
Configuration 类有什么问题吗? 谢谢
【问题讨论】:
-
在
.formLogin().disable();之后在protected void configure(HttpSecurity http)中添加.passwordEncoder(NoOpPasswordEncoder.getInstance());。 -
你的密码在你的数据库中加密了吗?
标签: spring-boot spring-security