Код: Выделить всё
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password); Authentication authentication = authenticationManager.authenticate(authenticationToken); SecurityContextHolder.getContext().setAuthentication(authentication);Код: Выделить всё
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.requestMatchers("/logged", "/login", "/register").permitAll() // Allow public access
.anyRequest().authenticated() // All other requests require authentication
.and()
.formLogin()
.loginPage("/login") // Specify your login page
.permitAll() // Allow everyone to see the login page
.and()
.logout()
.permitAll();
return http.build();
}
@Bean
public AuthenticationManager authenticationManager(HttpSecurity http) throws Exception {
AuthenticationManagerBuilder authenticationManagerBuilder =
http.getSharedObject(AuthenticationManagerBuilder.class);
authenticationManagerBuilder.userDetailsService(userDetailsService()).passwordEncoder(passwordEncoder());
return authenticationManagerBuilder.build();
}
@Bean
public CustomUserDetailsService userDetailsService() {
return new CustomUserDetailsService();
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... in-session