2017-02-18 11 views
0

Я хочу защитить конечные точки, используя Basic Auth.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(); 
    } 

} 
+0

body для 'userDetailsService()' –

ответ