/
dmuruz
/
Course_Service
Обзор
Документация
Войти
/
dmuruz
/
Course_Service
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
src/main/java/org/example/config/SecurityConfig.java
42 строки
2 KB
Danil Muruz
MVP
24 апр 2025, 09:39
24 апр 2025, 09:39
16b1c45
Код
Авторство
О чём код?
package org.example.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpMethod; import org.springframework.security.config.Customizer; import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.SecurityFilterChain; @Configuration @EnableMethodSecurity public class SecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http .csrf(csrf -> csrf.disable()) .authorizeHttpRequests(auth -> auth .requestMatchers(HttpMethod.POST, "/register").permitAll() .requestMatchers(HttpMethod.POST, "/admin/**").hasRole("ADMIN") .requestMatchers(HttpMethod.POST, "/courses/**").hasRole("ADMIN") .requestMatchers(HttpMethod.PUT, "/courses/**").hasRole("ADMIN") .requestMatchers(HttpMethod.DELETE, "/courses/**").hasRole("ADMIN") .requestMatchers(HttpMethod.POST, "/lessons/**").hasRole("ADMIN") .requestMatchers(HttpMethod.PUT, "/lessons/**").hasRole("ADMIN") .requestMatchers(HttpMethod.DELETE, "/lessons/**").hasRole("ADMIN") .anyRequest().authenticated() ) .httpBasic(Customizer.withDefaults()); return http.build(); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } }