|
1 | 1 | package umc.th.juinjang.config; |
2 | 2 |
|
3 | | -import lombok.RequiredArgsConstructor; |
| 3 | +import java.util.Arrays; |
| 4 | + |
4 | 5 | import org.springframework.context.annotation.Bean; |
5 | 6 | import org.springframework.context.annotation.Configuration; |
6 | 7 | import org.springframework.core.annotation.Order; |
|
16 | 17 | import org.springframework.security.config.http.SessionCreationPolicy; |
17 | 18 | import org.springframework.security.web.SecurityFilterChain; |
18 | 19 | import org.springframework.security.web.util.matcher.AntPathRequestMatcher; |
| 20 | + |
| 21 | +import lombok.RequiredArgsConstructor; |
19 | 22 | import umc.th.juinjang.jwt.JwtAuthenticationFilter; |
20 | 23 | import umc.th.juinjang.jwt.JwtExceptionFilter; |
21 | 24 | import umc.th.juinjang.service.auth.JwtService; |
22 | 25 |
|
23 | | -import java.util.Arrays; |
24 | | - |
25 | 26 | @Configuration |
26 | 27 | @EnableWebSecurity |
27 | 28 | @RequiredArgsConstructor |
28 | 29 | public class SecurityConfig { |
29 | | - private final AuthenticationConfiguration authenticationConfiguration; |
30 | | - |
31 | | - private final JwtService jwtService; |
32 | | - |
33 | | - private final JwtExceptionFilter jwtExceptionFilter; |
34 | | - |
35 | | - private final Environment environment; |
36 | | - @Bean |
37 | | - @Order(0) |
38 | | - public WebSecurityCustomizer webSecurityCustomizer(){ |
39 | | - String[] activeProfiles = environment.getActiveProfiles(); |
40 | | - boolean isProd = Arrays.asList(activeProfiles).contains("prod"); |
41 | | - |
42 | | - //prod아닐때 |
43 | | - if (!isProd) { |
44 | | - return web -> web.ignoring() |
45 | | - .requestMatchers("/swagger-ui/**", "/swagger/**", "/swagger-resources/**", "/swagger-ui.html", "/test", |
46 | | - "/configuration/ui", "/v3/api-docs/**", "/h2-console/**", "/api/auth/regenerate-token", |
47 | | - "/api/auth/kakao/**", "/api/auth/apple/**", "/actuator/prometheus", |
48 | | - "/api/auth/v2/apple/**", "/api/auth/v2/kakao/**"); |
49 | | - } |
50 | | - else { |
51 | | - return web -> web.ignoring() |
52 | | - .requestMatchers("/h2-console/**", "/api/auth/regenerate-token", |
53 | | - "/api/auth/kakao/**", "/api/auth/apple/**", "/actuator/prometheus", |
54 | | - "/api/auth/v2/apple/**", "/api/auth/v2/kakao/**"); |
55 | | - } |
56 | | - |
57 | | - } |
58 | | - |
59 | | - //선언 방식이 3.x에서 바뀜 |
60 | | - @Bean AuthenticationManager authenticationManager(AuthenticationConfiguration authConfiguration) throws Exception |
61 | | - { return authConfiguration.getAuthenticationManager(); } |
62 | | - |
63 | | - @Bean |
64 | | - protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception { |
65 | | - |
66 | | - http |
67 | | - .csrf(AbstractHttpConfigurer::disable) |
68 | | - .formLogin(Customizer.withDefaults()) |
69 | | - .sessionManagement((sessionManagement) -> |
70 | | - sessionManagement.sessionCreationPolicy(SessionCreationPolicy.STATELESS) |
71 | | -// 세션을 사용하지 않는다고 설정함 |
72 | | - ) |
73 | | - .addFilter(new JwtAuthenticationFilter(authenticationManager(authenticationConfiguration),jwtService)) |
74 | | -// JwtAuthenticationFilter를 필터에 넣음 |
75 | | - .authorizeHttpRequests((authorizeRequests) -> |
76 | | - authorizeRequests |
77 | | - .requestMatchers( |
78 | | - AntPathRequestMatcher.antMatcher("/api/auth/**") |
79 | | - ).authenticated() |
80 | | - .requestMatchers( |
81 | | - AntPathRequestMatcher.antMatcher("/h2-console/**") |
82 | | - ).permitAll() |
83 | | - |
84 | | - .anyRequest().authenticated() |
85 | | - |
86 | | - ) |
87 | | - .headers( |
88 | | - headersConfigurer -> |
89 | | - headersConfigurer |
90 | | - .frameOptions( |
91 | | - HeadersConfigurer.FrameOptionsConfig::sameOrigin |
92 | | - ) |
93 | | - ) |
94 | | - .addFilterBefore(jwtExceptionFilter, JwtAuthenticationFilter.class); |
95 | | - |
96 | | - return http.build(); |
97 | | - } |
| 30 | + private final AuthenticationConfiguration authenticationConfiguration; |
| 31 | + |
| 32 | + private final JwtService jwtService; |
| 33 | + |
| 34 | + private final JwtExceptionFilter jwtExceptionFilter; |
| 35 | + |
| 36 | + private final Environment environment; |
| 37 | + |
| 38 | + @Bean |
| 39 | + @Order(0) |
| 40 | + public WebSecurityCustomizer webSecurityCustomizer() { |
| 41 | + String[] activeProfiles = environment.getActiveProfiles(); |
| 42 | + boolean isProd = Arrays.asList(activeProfiles).contains("prod"); |
| 43 | + |
| 44 | + //prod아닐때 |
| 45 | + if (!isProd) { |
| 46 | + return web -> web.ignoring() |
| 47 | + .requestMatchers("/swagger-ui/**", "/swagger/**", "/swagger-resources/**", "/swagger-ui.html", "/test", |
| 48 | + "/configuration/ui", "/v3/api-docs/**", "/h2-console/**", "/api/auth/regenerate-token", |
| 49 | + "/api/auth/kakao/**", "/api/auth/apple/**", "/actuator/prometheus", |
| 50 | + "/api/auth/v2/apple/**", "/api/auth/v2/kakao/**"); |
| 51 | + } else { |
| 52 | + return web -> web.ignoring() |
| 53 | + .requestMatchers("/h2-console/**", "/api/auth/regenerate-token", |
| 54 | + "/api/auth/kakao/**", "/api/auth/apple/**", "/actuator/prometheus", |
| 55 | + "/api/auth/v2/apple/**", "/api/auth/v2/kakao/**", "/api/app/version/ios"); |
| 56 | + } |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + //선언 방식이 3.x에서 바뀜 |
| 61 | + @Bean |
| 62 | + AuthenticationManager authenticationManager(AuthenticationConfiguration authConfiguration) throws Exception { |
| 63 | + return authConfiguration.getAuthenticationManager(); |
| 64 | + } |
| 65 | + |
| 66 | + @Bean |
| 67 | + protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception { |
| 68 | + |
| 69 | + http |
| 70 | + .csrf(AbstractHttpConfigurer::disable) |
| 71 | + .formLogin(Customizer.withDefaults()) |
| 72 | + .sessionManagement((sessionManagement) -> |
| 73 | + sessionManagement.sessionCreationPolicy(SessionCreationPolicy.STATELESS) |
| 74 | + // 세션을 사용하지 않는다고 설정함 |
| 75 | + ) |
| 76 | + .addFilter(new JwtAuthenticationFilter(authenticationManager(authenticationConfiguration), jwtService)) |
| 77 | + // JwtAuthenticationFilter를 필터에 넣음 |
| 78 | + .authorizeHttpRequests((authorizeRequests) -> |
| 79 | + authorizeRequests |
| 80 | + .requestMatchers( |
| 81 | + AntPathRequestMatcher.antMatcher("/api/auth/**") |
| 82 | + ).authenticated() |
| 83 | + .requestMatchers( |
| 84 | + AntPathRequestMatcher.antMatcher("/h2-console/**"), |
| 85 | + AntPathRequestMatcher.antMatcher("/api/app/version/ios") |
| 86 | + ).permitAll() |
| 87 | + |
| 88 | + .anyRequest().authenticated() |
| 89 | + |
| 90 | + ) |
| 91 | + .headers( |
| 92 | + headersConfigurer -> |
| 93 | + headersConfigurer |
| 94 | + .frameOptions( |
| 95 | + HeadersConfigurer.FrameOptionsConfig::sameOrigin |
| 96 | + ) |
| 97 | + ) |
| 98 | + .addFilterBefore(jwtExceptionFilter, JwtAuthenticationFilter.class); |
| 99 | + |
| 100 | + return http.build(); |
| 101 | + } |
98 | 102 |
|
99 | 103 | } |
100 | 104 |
|
0 commit comments