Пользователь пока ничего не рассказал о себе

Наибольший вклад в теги

Все теги (1)

Лучшие ответы пользователя

Все ответы (2)
  • Почему при нажатии на кнопку отправить, логин форма просто обновляется(spring boot security)?

    @NatashaBal Автор вопроса
    В моем случае ошибкой было определение sessionCreationPolicy(SessionCreationPolicy.STATELESS); Я удалила эту строку.
    В своей программе я создала свой собственный компонент для перенаправления после успешной аутентификации, но я забыла использовать его в WebSecurityConfig.
    Правильный код

    @Configuration
    @EnableWebSecurity
    public class WebSecurityConfig {
    @Autowired
    Securityhandler successHandler;

    @Bean
    public UserDetailsService userDetailsService() {
    return new UserDetailsServiceImpl();
    }

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
    }

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authConfig) throws Exception {
    return authConfig.getAuthenticationManager();
    }

    @Bean
    public DaoAuthenticationProvider authenticationProvider() {
    DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
    authProvider.setUserDetailsService(userDetailsService());
    authProvider.setPasswordEncoder(passwordEncoder());
    return authProvider;
    }

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    http.authorizeHttpRequests().requestMatchers("/acthasform/").permitAll().anyRequest().authenticated();
    http.authenticationProvider(authenticationProvider());
    http.formLogin().loginPage("/login").permitAll().successHandler(successHandler).usernameParameter("username")
    .passwordParameter("password").permitAll().and().logout()
    .permitAll().and().exceptionHandling().accessDeniedPage("/403");

    return http.build();


    }
    }

    @Component
    public class Securityhandler implements AuthenticationSuccessHandler{


    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
    Authentication authentication) throws IOException {
    Set roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities());
    if (roles.contains("ROLE_ADMIN")) {
    response.sendRedirect("/regulatoryform/");
    }
    else {
    response.sendRedirect("/regulatoryact/");
    }
    }
    }
    Ответ написан
    Комментировать