복습/개발 일지

헤더에 Authorization 추가

DDunDDang 2023. 4. 23. 13:57

Postman에서 확인했을 땐 login절차에서 발급된 Authorization을 확인할 수 있었는데,,,프론트 분들께서 확인하셨을 때는 헤더에 담기지 않아 Authorization을 불러올 수 없다고 말씀하셔서 띠용했다...

이렇게 되면 헤더에 달린거 아닌가요 ㅠㅠㅠㅠㅠㅠㅠ

구글링 후 알고보니까 내가 잘못 하고 있었다.....

 

헤더에 포함시킨 줄 알았는데 넣지도 않고 바로 보낸것...

 

 

    @Bean
    public CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();

        configuration.setAllowCredentials(true);
        configuration.setAllowedOrigins(Arrays.asList("http://localhost:3000", "http://localhost:3001"));
        configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PATCH", "DELETE"));
        configuration.setAllowedHeaders(Arrays.asList("*"));
        configuration.addExposedHeader("Authorization");  // 추가된 코드: Authorization 을 헤더에 추가

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

        return source;
    }

addExposedHeader 부분을 추가해 줬더니 프론트분들께서 Authorization을 확인할 수 있었다.

 

참고로 setAllowCredentials(true) 로 설정하게 되면

configuration.setAlloweOrigins("*"); 패턴을 사용하지 못하게 된다.

-> 경우를 다 적어줘야한다..