Dzień dobry, mam problem z Spring Security i autoryzacją użytkownika poprzez bazę danych. Używam Spring 4, Spring Boot, Hibernate i Spring Data.
Mianowicie problem jest taki, że podając poprawne dane Spring Security uparcie odmawia mi dostępu.
Co wiem:
- Spring Data poprawnie zwraca hasło i nazwę użytkownika.
- W RESTAuthenticationFailureHandler widzę, że z frontendu zostało poprawnie przesłane hasło i nazwa
Konfiguracja:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private RESTAuthenticationEntryPoint authenticationEntryPoint;
@Autowired
private RESTAuthenticationFailureHandler authenticationFailureHandler;
@Autowired
private RESTAuthenticationSuccessHandler authenticationSuccessHandler;
@Autowired
private UserAuthService userAuthService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/home", "/").permitAll()
.antMatchers("/login").permitAll()
.antMatchers("/addGame").hasRole("USER")
.and()
.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint)
.and()
.formLogin()
.successHandler(authenticationSuccessHandler)
.failureHandler(authenticationFailureHandler);
}
@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userAuthService);
}
UserAuthService:
import org.springframework.security.core.userdetails.User;
@Service
public class UserAuthService implements UserDetailsService{
@Autowired
UserDatabaseService userDatabaseService;
public UserAuthService(){
super();
}
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
UserEntity user = userDatabaseService.getUserByUsername(username);
if ( user == null ){
throw new UsernameNotFoundException(username);
}
List<SimpleGrantedAuthority> grantedAuthority = new ArrayList<>();
grantedAuthority.add( new SimpleGrantedAuthority("USER") );
return new User(user.getUsername(), user.getPassword(), true, true,true,true, grantedAuthority );
}
UserDatabaseService Praktycznie pobiera tylko dane przez JpaRepository i ewentualnie zamienia DTO na Entity i na odwrót. Cała implementacja została maksymalnie uproszczona, ale wciąż nie działa.
Przepraszam za tytuł, ale problem ten doprowadza mnie do szewskiej pasji, 5 dni temu założyłem o tym temat na StackOverflow, bez odzewu niestety. Szukałem na internecie odpowiedzi i znalazłem nawet podobne tematy, ale bez odpowiedzi. Za wszelką pomoc byłbym wdzięczny.