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