2012-11-06 2 views
5

저지 클라이언트가 "origin"헤더를 설정하지 않고 무엇이 누락되어 있는지 궁금합니다. 내가 런타임에 검사 할 때저지 클라이언트가있는 "Origin"및 "Access-Control-Request-Method"헤더 설정

String origin="http://www.localhost.com"; 
ClientResponse response= webResourceBuilder("my/endpoint") 
      .header("origin" , origin) 
      .header("Access-Control-Request-Method", "POST") 
      .header("xorigin", origin) 
      .header("whatever", "test") 
      .accept("application/xml") 
      .get(ClientResponse.class); 

는 서버 측의 요청 헤더, 나는 "xorigin"와 "무엇"헤더가 아니라 "기원"과 "액세스 제어-요청-방법"

을 찾을 수 이 헤더를 어떻게 설정할 수 있습니까?

답변

12

기본 저지 클라이언트는 HttpURLConnection을 사용하여 요청을 서버에 보냅니다. HttpUrlConnection이 요청에서 전송되는 일부 헤더 제한을 참조하십시오

/* 
* Restrict setting of request headers through the public api 
* consistent with JavaScript XMLHttpRequest2 with a few 
* exceptions. Disallowed headers are silently ignored for 
* backwards compatibility reasons rather than throwing a 
* SecurityException. For example, some applets set the 
* Host header since old JREs did not implement HTTP 1.1. 
* Additionally, any header starting with Sec- is 
* disallowed. 
* 
* The following headers are allowed for historical reasons: 
* 
* Accept-Charset, Accept-Encoding, Cookie, Cookie2, Date, 
* Referer, TE, User-Agent, headers beginning with Proxy-. 
* 
* The following headers are allowed in a limited form: 
* 
* Connection: close 
* 
* See http://www.w3.org/TR/XMLHttpRequest2. 
*/ 
private static final boolean allowRestrictedHeaders; 
private static final Set<String> restrictedHeaderSet; 
private static final String[] restrictedHeaders = { 
    /* Restricted by XMLHttpRequest2 */ 
    //"Accept-Charset", 
    //"Accept-Encoding", 
    "Access-Control-Request-Headers", 
    "Access-Control-Request-Method", 
    "Connection", /* close is allowed */ 
    "Content-Length", 
    //"Cookie", 
    //"Cookie2", 
    "Content-Transfer-Encoding", 
    //"Date", 
    //"Expect", 
    "Host", 
    "Keep-Alive", 
    "Origin", 
    // "Referer", 
    // "TE", 
    "Trailer", 
    "Transfer-Encoding", 
    "Upgrade", 
    //"User-Agent", 
    "Via" 
}; 

을 당신은이 상황을 처리하는 방법 두 가지 옵션이 있습니다

  1. 기본 저지 클라이언트를 사용하면 시스템 속성을 설정해야합니다

    -Dsun.net.http.allowRestrictedHeaders=true 
    

    요청에서 제한된 헤더를 제거하지 않습니다.

  2. ApacheHttpClient/ApacheHttpClient4에는 이러한 제한이없는 것으로 보입니다. 단순히 프로젝트에 다음과 같은 종속성 중 하나를 추가

    <dependency> 
        <groupId>com.sun.jersey.contribs</groupId> 
        <artifactId>jersey-apache-client</artifactId> 
        <version>1.15</version> 
    </dependency> 
    

    또는

    <dependency> 
        <groupId>com.sun.jersey.contribs</groupId> 
        <artifactId>jersey-apache-client4</artifactId> 
        <version>1.15</version> 
    </dependency> 
    

    다음 클라이언트를 만드는 등 :

    ApacheHttpClient.create(com.sun.jersey.api.client.config.ClientConfig); 
    

    또는

    ApacheHttpClient4.create(com.sun.jersey.api.client.config.ClientConfig); 
    
+0

나는 방법 # 1에 의해 작동이 얻을 수 있었다. 실제로 # 2가 선호되었지만 http://java.net/jira/browse/JERSEY-jsp에서 지침에 따라 "java.lang.IllegalStateException : Invalid use of SingleClientConnManager : connection still allocated"오류를 지속적으로 받았습니다. 730 – SkP

7

또는 단지는 (당신이 전역 설정으로 설정하지 않는 경우) 헤더를 설정하기 전에 동적으로 속성을 설정 :

System.setProperty("sun.net.http.allowRestrictedHeaders", "true"); 
+0

이것은 저에게 효과적입니다. 요청을하기 위해 HttpURLConnection을 사용하는 Cxf 웹 클라이언트 (3.1.1)에서 같은 문제가 발생했습니다. –

관련 문제