-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecurityConfig.java
More file actions
106 lines (92 loc) · 4.15 KB
/
Copy pathSecurityConfig.java
File metadata and controls
106 lines (92 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.semosan.api.common.config;
import com.semosan.api.common.jwt.JwtFilter;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import java.util.List;
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig {
private final JwtFilter jwtFilter;
/**
* 스웨거 관련 경로
*/
public static final String[] SWAGGER_URIS = {
"/swagger-ui/**",
"/v3/api-docs/**",
"/swagger-resources/**",
"/swagger-ui.html"
};
/**
* 소셜 인증(카카오, 애플) 관련 경로
*/
public static final String[] OAUTH_URIS = {
"/api/oauth/kakao/login",
"/api/oauth/apple/login",
};
/**
* 인증(회원가입, 로그인 등) 관련 경로
*/
public static final String[] AUTH_URIS = {
"/api/auth/test/login",
"/api/auth/token/reissue"
};
/**
* WebSocket(STOMP) 엔드포인트.
* 핸드셰이크는 HTTP JWT 필터로 인증하지 않고 통과시키고,
* STOMP CONNECT 프레임에서 StompAuthChannelInterceptor 가 JWT 를 검증한다.
*/
public static final String[] WEBSOCKET_URIS = {
"/ws/tracking/**"
};
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.csrf(AbstractHttpConfigurer::disable)
.cors(cors -> cors.configurationSource(corsConfigurationSource()))
.formLogin(AbstractHttpConfigurer::disable)
.httpBasic(AbstractHttpConfigurer::disable)
.sessionManagement(session ->
session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
)
.authorizeHttpRequests(auth -> auth
.requestMatchers(SWAGGER_URIS).permitAll()
.requestMatchers(OAUTH_URIS).permitAll()
.requestMatchers(AUTH_URIS).permitAll()
.requestMatchers(WEBSOCKET_URIS).permitAll()
.anyRequest().authenticated()
)
.addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class);
return http.build();
}
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedOrigins(List.of("http://localhost:3000", "https://lgenius.site"));
config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "PATCH"));
config.setAllowedHeaders(List.of("*"));
config.setAllowCredentials(true);
// WebSocket(STOMP) 핸드셰이크는 다양한 origin(모바일/로컬 테스트 페이지 등)에서 들어옴.
// /ws/** 만 별도 정책으로 풀어준다.
// TODO: production 에서는 모바일 앱 origin 만 명시적으로 허용하도록 좁힐 것.
CorsConfiguration wsConfig = new CorsConfiguration();
wsConfig.setAllowedOriginPatterns(List.of("*"));
wsConfig.setAllowedMethods(List.of("GET"));
wsConfig.setAllowedHeaders(List.of("*"));
wsConfig.setAllowCredentials(true);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/ws/**", wsConfig);
source.registerCorsConfiguration("/**", config);
return source;
}
}