CORS 시큐리티 설정

kwak woojong·2022년 9월 1일
0

코드스테이츠

목록 보기
33/36
post-thumbnail

기능 개발 거의 끝내놓고

백단 서버를 뺀 다음에 프론트랑 통신을 해볼라 했음.

당연하게도 cors 에러가 뜬다.

    private CorsConfigurationSource corsConfig() {
        CorsConfiguration configuration = new CorsConfiguration();

        configuration.addAllowedOriginPattern("*");
        configuration.addAllowedHeader("*");
        configuration.addAllowedMethod("*");
        configuration.addExposedHeader("Authorization");

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

    }

어차피 개발서버니까 다 열어 제끼자

배포할 때만 조율하면 되니까

그리고 시큐리티 필터 체인에서 설정

 http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .cors().configurationSource(corsConfig())
                ...

이러면 일단 연결은 되실거임

configuration.addAllowedOriginPattern("*");

이 놈이 핵심인데, credentials = true, addAllowedOrigin("*") 로 나눠서 많이 쓰는거로 구글링하면 나옴.

실은 addAllowedOrigin("*")이 요새 사용안 함. 패턴으로 바꿔야 한다.

credentials는 혹시 몰라서 걍 true로 박아줘놨음

사전 프로젝트라 단순 개발인데 드릅게 시간 빡빡하다.

profile
https://crazyleader.notion.site/Crazykwak-36c7ffc9d32e4e83b325da26ed8d1728?pvs=4<-- 포트폴리오

0개의 댓글