|
1 | 1 | package com.back.web7_9_codecrete_be.global.security; |
2 | 2 |
|
3 | | -import com.back.web7_9_codecrete_be.domain.auth.service.TokenService; |
4 | | -import lombok.RequiredArgsConstructor; |
| 3 | +import java.util.List; |
| 4 | + |
5 | 5 | import org.springframework.context.annotation.Bean; |
6 | 6 | import org.springframework.context.annotation.Configuration; |
7 | 7 | import org.springframework.security.authentication.AuthenticationManager; |
|
14 | 14 | import org.springframework.web.cors.CorsConfiguration; |
15 | 15 | import org.springframework.web.cors.UrlBasedCorsConfigurationSource; |
16 | 16 |
|
17 | | -import java.util.List; |
| 17 | +import com.back.web7_9_codecrete_be.domain.auth.service.TokenService; |
| 18 | + |
| 19 | +import lombok.RequiredArgsConstructor; |
18 | 20 |
|
19 | 21 | @Configuration |
20 | 22 | @RequiredArgsConstructor |
21 | 23 | public class SecurityConfig { |
22 | 24 |
|
23 | | - private final JwtTokenProvider jwtTokenProvider; |
24 | | - private final JwtProperties jwtProperties; |
25 | | - private final CustomUserDetailService customUserDetailService; |
26 | | - private final TokenService tokenService; |
| 25 | + private final JwtTokenProvider jwtTokenProvider; |
| 26 | + private final JwtProperties jwtProperties; |
| 27 | + private final CustomUserDetailService customUserDetailService; |
| 28 | + private final TokenService tokenService; |
27 | 29 |
|
28 | | - @Bean |
29 | | - public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { |
| 30 | + @Bean |
| 31 | + public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { |
30 | 32 |
|
31 | | - http |
32 | | - .csrf(csrf -> csrf.disable()) |
33 | | - .cors(Customizer.withDefaults()) |
| 33 | + http |
| 34 | + .csrf(csrf -> csrf.disable()) |
| 35 | + .cors(Customizer.withDefaults()) |
34 | 36 |
|
35 | | - // 기본 로그인 폼 비활성화 |
36 | | - .formLogin(form -> form.disable()) |
| 37 | + // 기본 로그인 폼 비활성화 |
| 38 | + .formLogin(form -> form.disable()) |
37 | 39 |
|
38 | | - // HTTP Basic 인증 비활성화 |
39 | | - .httpBasic(basic -> basic.disable()) |
| 40 | + // HTTP Basic 인증 비활성화 |
| 41 | + .httpBasic(basic -> basic.disable()) |
40 | 42 |
|
41 | | - // H2 Console 설정 |
42 | | - .headers(headers -> headers.frameOptions(frame -> frame.disable())) |
| 43 | + // H2 Console 설정 |
| 44 | + .headers(headers -> headers.frameOptions(frame -> frame.disable())) |
43 | 45 |
|
44 | | - // 세션 관리 설정 - Stateless |
45 | | - .sessionManagement((session) -> session |
46 | | - .sessionCreationPolicy(SessionCreationPolicy.STATELESS)) |
| 46 | + // 세션 관리 설정 - Stateless |
| 47 | + .sessionManagement((session) -> session |
| 48 | + .sessionCreationPolicy(SessionCreationPolicy.STATELESS)) |
47 | 49 |
|
48 | | - // Authorization 설정 |
49 | | - .authorizeHttpRequests(auth -> auth |
50 | | - .requestMatchers( |
51 | | - "/api/v1/auth/**", // 로그인/회원가입은 허용 |
52 | | - "/v3/api-docs/**", // Swagger |
53 | | - "/swagger-ui/**", // Swagger UI |
54 | | - "/h2-console/**", // H2 Console |
55 | | - "/api/v1/location/**", //location 정보 조회 도메인(임시) |
56 | | - "/api/v1/concerts/**", // concert 정보 조회 도메인 |
57 | | - "/api/v1/artists/**" // artist 정보 저장 도메인(임시) |
58 | | - ).permitAll() |
| 50 | + // Authorization 설정 |
| 51 | + .authorizeHttpRequests(auth -> auth |
| 52 | + .requestMatchers( |
| 53 | + "/actuator/**", |
| 54 | + "/api/v1/auth/**", // 로그인/회원가입은 허용 |
| 55 | + "/v3/api-docs/**", // Swagger |
| 56 | + "/swagger-ui/**", // Swagger UI |
| 57 | + "/h2-console/**", // H2 Console |
| 58 | + "/api/v1/location/**", //location 정보 조회 도메인(임시) |
| 59 | + "/api/v1/concerts/**", // concert 정보 조회 도메인 |
| 60 | + "/api/v1/artists/**" // artist 정보 저장 도메인(임시) |
| 61 | + ).permitAll() |
59 | 62 |
|
60 | | - // ADMIN 전용 |
61 | | - .requestMatchers("/api/v1/admin/**").hasRole("ADMIN") |
| 63 | + // ADMIN 전용 |
| 64 | + .requestMatchers("/api/v1/admin/**").hasRole("ADMIN") |
62 | 65 |
|
63 | | - // USER, ADMIN 허용 |
64 | | - .requestMatchers("/api/v1/users/**").hasAnyRole("USER", "ADMIN") |
| 66 | + // USER, ADMIN 허용 |
| 67 | + .requestMatchers("/api/v1/users/**").hasAnyRole("USER", "ADMIN") |
65 | 68 |
|
66 | | - .anyRequest().authenticated() |
67 | | - ) |
| 69 | + .anyRequest().authenticated() |
| 70 | + ) |
68 | 71 |
|
69 | | - .addFilterBefore( |
70 | | - new JwtAuthenticationFilter(jwtTokenProvider, jwtProperties, tokenService), |
71 | | - UsernamePasswordAuthenticationFilter.class |
72 | | - ); |
| 72 | + .addFilterBefore( |
| 73 | + new JwtAuthenticationFilter(jwtTokenProvider, jwtProperties, tokenService), |
| 74 | + UsernamePasswordAuthenticationFilter.class |
| 75 | + ); |
73 | 76 |
|
74 | | - return http.build(); |
75 | | - } |
| 77 | + return http.build(); |
| 78 | + } |
76 | 79 |
|
77 | | - @Bean |
78 | | - public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration) throws Exception { |
79 | | - return configuration.getAuthenticationManager(); |
80 | | - } |
| 80 | + @Bean |
| 81 | + public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration) throws Exception { |
| 82 | + return configuration.getAuthenticationManager(); |
| 83 | + } |
81 | 84 |
|
82 | | - // CORS 설정(로컬 프론트 통신 허용) |
83 | | - @Bean |
84 | | - public UrlBasedCorsConfigurationSource corsConfigurationSource() { |
85 | | - CorsConfiguration configuration =new CorsConfiguration(); |
| 85 | + // CORS 설정(로컬 프론트 통신 허용) |
| 86 | + @Bean |
| 87 | + public UrlBasedCorsConfigurationSource corsConfigurationSource() { |
| 88 | + CorsConfiguration configuration = new CorsConfiguration(); |
86 | 89 |
|
87 | | - configuration.setAllowedOrigins(List.of("http://localhost:3000","https://web-6-7-codecrete-fe.vercel.app", "https://www.naeconcertbutakhae.shop")); |
88 | | - configuration.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS")); |
| 90 | + configuration.setAllowedOrigins(List.of("http://localhost:3000", "https://web-6-7-codecrete-fe.vercel.app", "https://www.naeconcertbutakhae.shop")); |
| 91 | + configuration.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS")); |
89 | 92 |
|
90 | | - configuration.setAllowedHeaders(List.of("*")); |
| 93 | + configuration.setAllowedHeaders(List.of("*")); |
91 | 94 |
|
92 | | - //쿠키 자동으로 넘어가게 설정 |
93 | | - configuration.setAllowCredentials(true); |
| 95 | + //쿠키 자동으로 넘어가게 설정 |
| 96 | + configuration.setAllowCredentials(true); |
94 | 97 |
|
95 | | - UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); |
96 | | - source.registerCorsConfiguration("/api/**", configuration); |
| 98 | + UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); |
| 99 | + source.registerCorsConfiguration("/api/**", configuration); |
97 | 100 |
|
98 | | - return source; |
99 | | - } |
| 101 | + return source; |
| 102 | + } |
100 | 103 | } |
0 commit comments