[WIL]5주차 개발일지

민지·2022년 6월 13일
0

WIL

목록 보기
4/4

CORS

  • Croos-Origin Resource Sharing, 교차 출처 리소스 공유
  • front와 back의 로컬 환경이 다르기 때문에 SOP 문제 발생
  • Spring Serve에서 출처가 다른 자원이 접근할 수 있도록 권한을 주는 작업

CORS 해결 방법

WebMvcConfigurer addCorsMapiings 구현

@RequiredArgsConstructor
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

   private final LoginUserIdArgumentResolver loginUserIdArgumentResolver;

   @Override
   public void addCorsMappings(CorsRegistry registry) {
       registry.addMapping("/**")
           .allowedOrigins("http://localhost:3000")
           .allowedMethods("OPTIONS", "GET", "POST", "PUT", "DELETE");
   }
}
  • WebMvcConfigurer 인터페이스가 가지고 있는 addCorsMappings 메소드를 오버라이딩 한 후에 위와 같이 http://localhost:3000에 대해서 접근할 수 있는 권한

    @RequiredArgsConstructor
    @EnableWebSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    
       @Override
       protected void configure(HttpSecurity http) throws Exception {
           http
               .authorizeRequests()
               .mvcMatchers(HttpMethod.OPTIONS, "/**").permitAll() // Preflight Request 허용해주기
               .antMatchers("/api/v1/**").hasAnyAuthority(USER.name());
       }
    }
profile
매일 매일 기록하기

0개의 댓글