2017-12-20 2 views
1

CORS 헤더를 처음 사용하고 스프링 부트로 구현했습니다. 요청 본문을 수락하는 POST 서비스에서 CORS 헤더를 사용하도록 설정하고 있습니다.

실제로 실행되고 200을 반환하는 프리 플라이트 요청이 처음 만들어졌지만 실제 게시물 요청이 호출되면 응답 본문 "잘못된 CORS 요청"과 함께 항상 403이 반환됩니다.

나는 거의 모든 봄 워드 프로세서와 모든 google/stackoverflow 토론을 읽었지만 누락 된 부분을 찾지 못했습니다 .. 안녕하세요 ..스프링 부트 CORS 헤더

아래에서 저는 클래스 상단과 메소드 상단에 crossOrigin을 추가하여 테스트했습니다 운이 없다. POST 방법에 대한

@CrossOrigin(origins = "https://domain/", allowCredentials = "false") 
@RequestMapping(value = ApplicationConstants.URI_PATH) 
class MainController { 
     @RequestMapping(value = '/postMethod', method = RequestMethod.POST) 
     Map<String, Object> postMethod(HttpServletResponse servletResponse, 
     @RequestBody(required = false) AccessToken requestedConsumerInfo) {...} 


- 프리 플라이트 요청이 호출되고 결과는 200 만 주 POST 호출이 반환 403 옵션

전화입니다 : 상태 코드 POST 200

Response headers (616 B) 
Access-Control-Allow-Credentials true 
Access-Control-Allow-Headers content-type 
Access-Control-Allow-Methods POST 
Access-Control-Allow-Origin https://domain 
Allow GET, HEAD, POST, PUT, DELETE, OPTIONS, PATCH 
Cache-Control max-age=0, private, no-cache, …roxy-revalidate, no-transform 
Connection close 
Content-Length 0 
Date Wed, 20 Dec 2017 17:57:14 GMT 
Pragma no-cache 
Server nginx/1.9.1 
Strict-Transport-Security max-age=63072000; includeSubdomains; 
Vary Origin,User-Agent 
X-Frame-Options SAMEORIGIN 
X-XSS-Protection 1; mode=block 

Request headers (512 B) 
Accept text/html,application/xhtml+xm…plication/xml;q=0.9,*/*;q=0.8 
Accept-Encoding gzip, deflate, br 
Accept-Language en-US,en;q=0.5 
Access-Control-Request-Headers content-type 
Access-Control-Request-Method POST 
Connection keep-alive 
Host domain 
Origin https://domain 
User-Agent Mozilla/5.0 (Windows NT 6.1; W…) Gecko/20100101 Firefox/57.0 

전화 : 상태 코드 403

Response headers (364 B) 
Cache-Control max-age=0, private, no-cache, …roxy-revalidate, no-transform 
Connection close 
Content-Length 20 
Date Wed, 20 Dec 2017 17:57:14 GMT 
Pragma no-cache 
Server nginx/1.9.1 
Strict-Transport-Security max-age=63072000; includeSubdomains; 
Vary User-Agent 
X-Frame-Options SAMEORIGIN 
X-XSS-Protection 1; mode=block 

Request headers (2.507 KB) 
Accept application/json, text/plain, */* 
Accept-Encoding gzip, deflate, br 
Accept-Language en-US,en;q=0.5 
Connection keep-alive 
Content-Length 102 
Content-Type application/json 
Cookie rxVisitor=1513720811976ARCUHEC…B4SL3K63V8|6952d9a33183e7bc|1 
Host domain 
Origin https://domain 
Referer https://domain/home/account/register 
User-Agent Mozilla/5.0 (Windows NT 6.1; W…) Gecko/20100101 Firefox/57.0 

이것이 작동하지 않았기 때문에 글로벌 구성 만 추가하고 위의 코드와 함께 행운을 빕니다. 프리 플라이트 옵션 요청에

@Bean 
    public WebMvcConfigurer corsConfigurer() { 
     return new WebMvcConfigurerAdapter() { 
      @Override 
      void addCorsMappings(CorsRegistry registry) { 
       super.addCorsMappings(registry); 
       registry.addMapping(ApplicationConstants.MEMBER_URL_PATH) 
         .allowedOrigins("https://domain/") 
         .allowedMethods(HttpMethod.GET.toString(), 
            HttpMethod.POST.toString(), HttpMethod.PUT.toString()); 
      } 
     } 
    } 

답변

2

, 응답해야 서버의 모든 (이미이 일을하고 보이는 것처럼) 다음 실제 POST 요청에

Access-Control-Allow-Origin, Access-Control-Allow-Methods, Access-Control-Allow-Headers, Access-Control-Allow-Credentials (if cookies are passed)

, 당신 ' 적어도 Access-Control-Allow-OriginAccess-Control-Allow-Credentials을 반환해야합니다. POST 응답을 위해 현재 반환하지 않습니다.

+0

그러나 "현재 POST 응답을 보내지 않았습니다."에 필요한 구성은 무엇입니까? –

+0

사실 당신은 나에게 "Access Control-Allow-Credentials (쿠키가 통과 된 경우)"라는 좋은 힌트를주었습니다. 쿠키를 설정하고 문제를 일으키는 컨트롤러에서 allowCredentials = "false"를 지정했습니다. 당신의 도움을 주셔서 감사합니다! –