Web

CORS

잔망루피 2022. 10. 7. 19:25
반응형

SOP

Same Origin Policy

다른 출처의 리소스를 사용하는 것에 제한을 두는 보안 방식

Protocol, Host, Port가 같으면 같은 출처다.

ex) http://localhost:80과 http://localhost는 같다.

 

 

CORS

Cross-Origin Resource Sharing

추가 HTTP header를 사용해 한 출처에서 실행 중인 web applicaion이 다른 출처의 선택한 자원에 접근할 수 있는 권한을 부여

pre-flight 요청은 쿠키(JSESSIONID)가 포함되지 않으므로 CORS는 Spring Security 전에 실행되어야 한다.
요청에 쿠키가 포함되지 않고 Spring Security가 먼저라면 사용자가 인증되지 않았다고 판단하고 거절한다.
CORS가 먼저 처리되도록 하는 가장 쉬운 방법은 CorsWebFilter를 사용하는 것이다.
사용자는 CorsConfigurationSource를 제공함으로써 CorsWebFilter를 Spring Security와 통합할 수 있다.

@Bean
CorsConfigurationSource corsConfigurationSource() {
	CorsConfiguration configuration = new CorsConfiguration();
	configuration.setAllowedOrigins(Arrays.asList("https://example.com"));
	configuration.setAllowedMethods(Arrays.asList("GET","POST"));
	UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
	source.registerCorsConfiguration("/**", configuration);
	return source;
}

 Spring Security 내에서 CORS 지원을 통합하는 예제다.

 

@Bean
SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
	http
		// ...
		.cors(cors -> cors.disable());
	return http.build();
}

Spring Security 내에서 CORS 통합을 비활성화하는 예제다.

 

 

 

 

참고 👇

https://www.youtube.com/watch?v=-2TgkKYmJt4 

 

https://docs.spring.io/spring-security/reference/reactive/integrations/cors.html

 

CORS :: Spring Security

Spring Framework provides first class support for CORS. CORS must be processed before Spring Security because the pre-flight request will not contain any cookies (i.e. the JSESSIONID). If the request does not contain any cookies and Spring Security is firs

docs.spring.io

 

반응형

'Web' 카테고리의 다른 글

nGrinder 설치  (0) 2023.01.03
JWT  (0) 2022.12.09
CSRF  (0) 2022.09.15
[Error] Request method 'DELETE' not supported  (0) 2022.06.04
get과 post 차이  (0) 2022.02.18