1
+ package com .lesso .neverland .common .configuration ;
2
+
3
+ import com .lesso .neverland .common .jwt .JwtAuthenticationFilter ;
4
+ import com .lesso .neverland .common .jwt .JwtExceptionFilter ;
5
+ import com .lesso .neverland .user .application .AuthService ;
6
+ import com .lesso .neverland .user .application .UserService ;
7
+ import lombok .RequiredArgsConstructor ;
8
+ import org .springframework .context .annotation .Bean ;
9
+ import org .springframework .context .annotation .Configuration ;
10
+ import org .springframework .data .redis .core .RedisTemplate ;
11
+ import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
12
+ import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
13
+ import org .springframework .security .config .annotation .web .configurers .AbstractHttpConfigurer ;
14
+ import org .springframework .security .config .http .SessionCreationPolicy ;
15
+ import org .springframework .security .web .SecurityFilterChain ;
16
+ import org .springframework .security .web .authentication .UsernamePasswordAuthenticationFilter ;
17
+ import org .springframework .security .web .util .matcher .AntPathRequestMatcher ;
18
+ import org .springframework .web .cors .CorsConfiguration ;
19
+ import org .springframework .web .cors .CorsConfigurationSource ;
20
+ import org .springframework .web .cors .UrlBasedCorsConfigurationSource ;
21
+
22
+ import java .util .List ;
23
+
24
+ @ Configuration
25
+ @ EnableWebSecurity
26
+ @ RequiredArgsConstructor
27
+ public class WebSecurityConfig {
28
+ private final UserService userService ;
29
+ private final AuthService authService ;
30
+ private final JwtExceptionFilter jwtExceptionFilter ;
31
+ private final RedisTemplate <String , String > redisTemplate ;
32
+
33
+ // @Bean
34
+ // CorsConfigurationSource corsConfigurationSource() {
35
+ // CorsConfiguration configuration = new CorsConfiguration();
36
+ // configuration.setAllowedHeaders(List.of("*"));
37
+ // configuration.setAllowedOrigins(List.of("http://localhost:8080")); //TODO: 프론트 로컬, 배포 url 추가
38
+ // configuration.setAllowedMethods(List.of("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"));
39
+ // configuration.setAllowCredentials(true);
40
+ // UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
41
+ // source.registerCorsConfiguration("/**", configuration);
42
+ // return source;
43
+ // }
44
+
45
+ @ Bean
46
+ protected SecurityFilterChain securityFilterChain (HttpSecurity httpSecurity ) throws Exception {
47
+ return httpSecurity
48
+ .csrf (AbstractHttpConfigurer ::disable )
49
+ .sessionManagement ((sessionManagement ) ->
50
+ sessionManagement .sessionCreationPolicy (SessionCreationPolicy .STATELESS ))
51
+ .authorizeHttpRequests ((authorizeRequests ) -> authorizeRequests
52
+ .requestMatchers (
53
+ new AntPathRequestMatcher ("/users/login" ),
54
+ new AntPathRequestMatcher ("/users/signup" ),
55
+ new AntPathRequestMatcher ("/users/nickname" ),
56
+ new AntPathRequestMatcher ("/users/loginId" ),
57
+ new AntPathRequestMatcher ("/users/reissue-token" ),
58
+ new AntPathRequestMatcher ("/**" , "GET" ),
59
+ new AntPathRequestMatcher ("/gpt3/completePuzzle" ),
60
+ new AntPathRequestMatcher ("/home" , "GET" )).permitAll ()
61
+ .anyRequest ().authenticated ())
62
+ .addFilterBefore (new JwtAuthenticationFilter (authService , userService , redisTemplate ), UsernamePasswordAuthenticationFilter .class )
63
+ .addFilterBefore (jwtExceptionFilter , JwtAuthenticationFilter .class )
64
+ .build ();
65
+ }
66
+ }
0 commit comments