Эм, или я не понял пунктов, или всё довольно просто:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private DataSource dataSource;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().cors().disable().authorizeRequests()
.anyRequest().authenticated()
.and().formLogin();
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource);
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
При запросе на любой authenticated()
метод происходит редирект на formLogin()
.
При POST /login
выдается кука, которая привязывается к сессии.
В примере используется JdbcUserDetailsManagerConfigurer
у которого свой провайдер авторизации, но можно свой провайдер добавить:
public class CustomAuthenticationProvider implements AuthenticationProvider {
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
...
}
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(customAuthenticationProvider);
}
Для избавления редиректа можно костыльнуть, задизейблив форму .formLogin().disable()
и добавив страницу /auth для анонимов: .antMatchers("/auth").anonymous()
И добавив, например, такой контроллер для авторизации:
@GetMapping("auth")
public String auth(@RequestParam String username, @RequestParam String password) throws Exception {
try {
UserDetails userDetails = userDetailsService.loadUserByUsername(username);
if(passwordEncoder.matches(password, userDetails.getPassword())) {
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(username, null, userDetails.getAuthorities());
authentication.setDetails(userDetails);
SecurityContextHolder.getContext().setAuthentication(authentication);
}
else {
return "PASSWORD_NOT_MATCHES";
}
}
catch (UsernameNotFoundException ex) {
return "USER_NOT_FOUND";
}
return "OK";
}
Попробуем раскостылить: оставить стандартный механизм авторизации через форму, но без редиректов (уберем редиректы успешной/неуспешной авторизации, редиректы с других страниц):
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().cors().disable().authorizeRequests()
.anyRequest()
.authenticated()
.and().formLogin()
.successHandler((httpServletRequest, httpServletResponse, authentication) -> httpServletResponse.setStatus(HttpStatus.OK.value()))
.failureHandler((httpServletRequest, httpServletResponse, e) -> httpServletResponse.setStatus(HttpStatus.UNAUTHORIZED.value()))
.and().exceptionHandling()
.authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/login") {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
if(authException != null) {
response.sendError(HttpStatus.FORBIDDEN.value());
}
}
});
}