0
我想使用基本身份驗證來保護端點。Spring,使用數據庫進行身份驗證
當我嘗試使用正確的用戶名和密碼登錄時 - 一切正常。
我的問題是,當我試圖用錯誤的用戶名和密碼登錄 - 我得到:
java.lang.StackOverflowError: null
at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:442) ~[spring-security-config-4.2.1.RELEASE.jar:4.2.1.RELEASE]
我怎樣才能解決這個問題?
SecurityConfig.java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private static final String AUTHORITIES_QUERY = "SELECT u.username, r.role FROM user u " +
"INNER JOIN user_role ur ON u.id = ur.user_id " +
"INNER JOIN role r ON ur.role_id = r.id " +
"WHERE u.username=?";
private static final String USERS_QUERY = "SELECT username, password, is_active FROM user WHERE username=?";
@Autowired
private DataSource dataSource;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.jdbcAuthentication()
.usersByUsernameQuery(USERS_QUERY)
.authoritiesByUsernameQuery(AUTHORITIES_QUERY)
.dataSource(dataSource).and()
.userDetailsService(userDetailsService());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/user").hasAuthority("USER")
.antMatchers("/admin").hasAuthority("ADMIN")
.anyRequest().fullyAuthenticated()
.and()
.httpBasic().and()
.csrf()
.disable();
}
}
body for userDetailsService()' –