/
ilyaBu
/
WebSpringJava
Обзор
Документация
Войти
/
ilyaBu
/
WebSpringJava
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/main/java/ru/gb/spring/diplomawebjavaspringgb/config/SecurityConfig.java
81 строка
3 KB
IliaBu
Update code
14 июл 2024, 15:26
14 июл 2024, 15:26
3414678
Код
Авторство
О чём код?
package ru.gb.spring.diplomawebjavaspringgb.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.util.matcher.AntPathRequestMatcher; /** * Класс настроек безопасности */ @Configuration @EnableWebSecurity public class SecurityConfig { @Autowired private UserDetailsService userDetailsService; @Autowired private AuthHandler authenticationSuccessHandler; /** * Кодирование паролей * * @return новый хэшированный пароль алгоритмом bcrypt */ @Bean public static PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } /** * Цепочка фильтров безопасности. * * @param http запрос http * @return запрос с выбранными фильтрами безопасности * @throws Exception исключение */ @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http .authorizeHttpRequests((authorize) -> authorize .requestMatchers("/clear", "/register/**", "/error", "/webjars/**", "/css/**", "/img/**").permitAll() .requestMatchers("/home", "/add/**", "/list-parts/**", "/delete/**").hasRole("USER") .requestMatchers("/home", "/home-admin/**", "/list-parts/**", "/register-admin/**", "/add/**", "/delete/**", "/user-update/**", "/user-delete/**", "/upload-files").hasRole("ADMIN") .anyRequest().authenticated() ).formLogin( form -> form .loginPage("/login") .loginProcessingUrl("/login") .successHandler(authenticationSuccessHandler) .failureUrl("/login?error") .permitAll() ).logout( logout -> logout .logoutRequestMatcher(new AntPathRequestMatcher("/logout")) .permitAll() ); return http.build(); } /** * Настройка аутентификации * * @param auth аутентификация в памяти * @throws Exception исключение */ @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .userDetailsService(userDetailsService) .passwordEncoder(passwordEncoder()); } }