Здравствуйте. Помогите, пожалуйста, решить следующую проблему.
Пытаюсь поднять spring security
Его конфигурация
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
@Qualifier("customUserDetailsService")
UserDetailsService userDetailsService;
@Autowired
CustomSuccessHandler customSuccessHandler;
@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/admin/*").access("hasRole('Manager')")
.antMatchers("/user/*").access("hasRole('userAvailable')")
//.and().formLogin().loginPage("/login")
.and().formLogin().loginPage("/login").successHandler(customSuccessHandler)
.usernameParameter("username").passwordParameter("password")
.and().csrf()
.and().exceptionHandling().accessDeniedPage("/404");
}
}
Разделение по ролям
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
@Qualifier("customUserDetailsService")
UserDetailsService userDetailsService;
@Autowired
CustomSuccessHandler customSuccessHandler;
@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/admin/*").access("hasRole('Manager')")
.antMatchers("/user/*").access("hasRole('userAvailable')")
//.and().formLogin().loginPage("/login")
.and().formLogin().loginPage("/login").successHandler(customSuccessHandler)
.usernameParameter("username").passwordParameter("password")
.and().csrf()
.and().exceptionHandling().accessDeniedPage("/404");
}
}
Но при авторизации меня кидает на
login?error, а в сервлете
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String loginPage(HttpServletRequest request, Locale locale, Model model) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
System.out.println(auth.getDetails());
System.out.println(auth.getPrincipal());
System.out.println(principal);
return "index";
вижу
anonymousUser. Что я делаю неправильно?