Project/Boilerplate

Spring Boot response로 header가 추가되지 않는 현상

조용우 2025. 3. 6. 04:42

문제:

response.addHeader(JwtProperties.HEADER_AUTHORIZATION,
    JwtProperties.ACCESS_TOKEN_PREFIX + newAccessToken);

스프링 서버에서 응답 헤더에 추가한 헤더가 Front 서버에서 출력했을 때, undefined로 출력됨

 

원인:

configuration.setExposedHeaders(Collections.singletonList("Set-Cookie"));
configuration.setExposedHeaders(Collections.singletonList("Authorization"));
configuration.setExposedHeaders(Collections.singletonList("x-reissue-token"));

 

@Configuration
public class CorsMvcConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry corsRegistry) {
        corsRegistry
            // CORS를 적용할 URL 패턴
            .addMapping("/**")
            // 응답에 노출되는 헤더
            .exposedHeaders("Set-Cookie")
            // 자원 공유를 허락할 origin (프론트)
            .allowCredentials(true)
            .allowedOrigins("http://localhost:3000");
    }
}

 

해결:

configuration.setExposedHeaders(Arrays.asList("Authorization", "Set-Cookie", "x-reissue-token"));
@Override
public void addCorsMappings(CorsRegistry corsRegistry) {
    corsRegistry
        // CORS를 적용할 URL 패턴
        .addMapping("/**")
        // 응답에 노출되는 헤더
        .exposedHeaders("Authorization", "Set-Cookie", "x-reissue-token")
        // 자원 공유를 허락할 origin (프론트)
        .allowCredentials(true)
        .allowedOrigins("http://localhost:3000");
}