[네트워크] CORS 에러를 테스트해보자

Hocaron·2022년 12월 19일
1

네트워크

목록 보기
3/5

우리를 너무나도 괴롭히는 CORS 에러를 해결했을 때, 테스트는 어떻게 해야할까?

CORS 설정 추가하기

...
        http.httpBasic().disable()
                .cors().configurationSource(corsConfigurationSource())
...
    @Bean
    protected CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOriginPatterns(CORSType.CONFIGURATION.getAllowOrigins());
        configuration.setAllowedHeaders(CORSType.CONFIGURATION.getAllowHeaders());
        configuration.setAllowedMethods(CORSType.CONFIGURATION.getAllowMethods());
        configuration.setMaxAge(CORSType.CONFIGURATION.getMaxAge());
        configuration.setAllowCredentials(true);

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);

        return source;
    }

"When allowCredentials is true, allowedOrigins cannot contain the special value "" since that cannot be set on the "Access-Control-Allow-Origin" response header. To allow credentials to a set of origin ... "
allow credentials true 로 설정하면 allowedOrigin를 "
"로 설정할 수 없다. setAllowedOrigins 대신 setAllowedOriginPatterns 를 사용하자.

적용이 잘 된건지, 테스트하기

https://test-cors.org 에서 프론트에서 서버에 요청하는 것과 같이 cors 테스트를 할 수 있다.

적용한 코드는 여기

https://github.com/depromeet/toks-api/blob/develop/core/src/main/java/com/tdns/toks/core/config/security/AuthSecurityConfig.java

profile
기록을 통한 성장을

0개의 댓글