springboot 크로스 도 메 인 CORS 문제 해결

도 메 인 방문 문제 및 해결
Access to XMLHttpRequest at ‘http://localhost:88/api/sys/login’ from origin ‘http://localhost:8001’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
  • ,같은 프로 토 콜,같은 도 메 인 이름,같은 포트 는 같은 도 메 인 이 라 고 불 립 니 다.브 라 우 저 는 도 메 인 간 요청 을 허용 하지 않 습 니 다(localhost:이 컴퓨터 127.0.0.0.1:이 컴퓨터).방문 경로'/hello'이전의 이 문 자 는 변경 되면 도 메 인 간 요청 으로 간 주 됩 니 다.
  • https://localhost:8080/hello 방문 하 다.https://localhost:80/hello
  • https://localhost:8080/hello 방문 하 다.https://127.0.0.1:8080/hello
  • https://localhost:8080/hello 방문 하 다.http://localhost:8080/hello
  • 등등
  • 도 메 인 간 요청 해결
    문제 1,도 메 인 을 뛰 어 넘 는 데 성공 하지 못 했 습 니 다springboot解决跨域CORS问题_第1张图片XMLHttpRequest 에 대한 액세스 at'http://localhost:88/api/sys/login’ from origin ‘http://localhost:8001’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. POST http://localhost:88/api/sys/login net::ERR_FAILED Uncaught (in promise) Error: Network Error at createError (createError.js?16d0:16) at XMLHttpRequest.handleError (xhr.js?ec6c:87)
  • 해결 방법:
  • 1.도 메 인 간 문서 설명https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS
  • 2.게 이 트 웨 이에 서 크로스 도 메 인 요청 프로필 을 작성 하고 게 이 트 웨 이에 서 크로스 도 메 인 요청 을 통일 한 다음 에 응답 합 니 다
  • 3.CorsWebFilter 류 를 사용 해 야 합 니 다.이 종 류 는 도 메 인 정 보 를 설정 하 는 클래스 로 설정 파일 에 bean 을 추가 할 수 있 습 니 다.
  • CorsWebFilter 구조 함 수 는 무 참 구조 가 없 기 때문에 새로운 CorsConfigurationSourcespringboot解决跨域CORS问题_第2张图片
  • CorsConfigurationSource 는 하나의 인터페이스,ctrl+h 로 구현 클래스 를 보면 UrlBasedCorsConfigurationSource 류 만 이 인 터 페 이 스 를 실현 했다 는 것 을 알 수 있 습 니 다


  • springboot解决跨域CORS问题_第3张图片 springboot解决跨域CORS问题_第4张图片
  • UrlBasedCorsConfigurationSource 는 세 가지 방법 이 있 습 니 다.registerCorsConfiguration 방법 을 사용 하여 크로스 도 메 인 설정 정 보 를 등록 해 야 하기 때문에 new CorsConfiguration

  • springboot解决跨域CORS问题_第5张图片
  • corsConfiguration 은 설정 정보 입 니 다.http 의 요청/응답 정 보 를 위해 필요 에 따라 설정 할 수 있 습 니 다springboot解决跨域CORS问题_第6张图片
  • 크로스 도 메 인 코드 작성
  • import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.cors.CorsConfiguration;
    import org.springframework.web.cors.reactive.CorsConfigurationSource;
    import org.springframework.web.cors.reactive.CorsWebFilter;
    import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
    
    @Configuration
    public class CorsConfig {
         
        @Bean
        public CorsWebFilter corsWebFilter(){
         
            //  CorsWebFilter      CotsConfigurationSorcery  
            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
            //      
            CorsConfiguration corsConfiguration = new CorsConfiguration();
            //      ,
            //           
            corsConfiguration.addAllowedHeader("*");
            //            
            corsConfiguration.addAllowedMethod("*");
            //           
            corsConfiguration.addAllowedOrigin("*");
            corsConfiguration.setAllowCredentials(true);
            //      
            source.registerCorsConfiguration("/**",corsConfiguration);
            //  source  
            return new CorsWebFilter(source);
        }
    }
    
  • 문제 해결
  • 문제 2.도 메 인 처리 과정 에서 다 중 도 메 인 처리 코드 를 썼 습 니 다.이런 문 제 는 도 메 인 처리 코드 중 하 나 를 삭제 하면 됩 니 다
  • springboot解决跨域CORS问题_第7张图片

    좋은 웹페이지 즐겨찾기