2017-01-17 29 views
5

私はSpringのRestTemplateを使用してクロスオリジンリクエストを作成しようとしています。通信は2つのSpring-boot webappの間で行われ、両方ともlocalhostで実行されますが、異なるポートで実行されます。私は何をすることです:RestTemplateがOriginヘッダーを渡さない

HttpHeaders httpHeaders = new HttpHeaders(); 
httpHeaders.setOrigin("http://localhost:8083"); 
httpHeaders.add("Authorization", token); 

HttpEntity<Void> httpEntity = new HttpEntity<>(httpHeaders); 

ParameterizedTypeReference<List<MyObj>> beanType = new ParameterizedTypeReference<List<MyObj>>() {}; 
ResponseEntity<List<MyObj>> list = restTemplate.exchange(serviceURL, HttpMethod.GET, httpEntity, beanType); 

呼び出しが実行され、「承認」ヘッダがうまく渡されますが、関係なく、私がしようとするもの、受信側には「起源」ヘッダーがありません。 他のツール(SoapUI、RestClient Chromeプラグインなど)を使用して、simillarリクエストを作成すると、ヘッダーが渡されます。 RestTemplateを使用した場合、原点ヘッダが渡されていないのはなぜ

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) 
     throws IOException, ServletException { 
    HttpServletRequest request = (HttpServletRequest) req; 
    HttpServletResponse response = (HttpServletResponse) res; 
    Enumeration<String> headerNames = request.getHeaderNames(); 
    while (headerNames.hasMoreElements()) { 
     String headerName = headerNames.nextElement(); 
     log.info(headerName + ": " + request.getHeader(headerName)); 
    } 
} 

:私はとは、javax.servlet.Filterの実装を使用しています受信側のすべてのヘッダーを印刷するには

答えて

7

私は1世紀をかけて修正したのと同じ問題がありました。

根本的な原因は、RestTemplateドキュメントから

(注)この行です:デフォルトでRestTemplateは、HTTP接続を確立するために、標準のJDK施設に依存しています。

あなたはJavaで HttpUrlConnectionクラスのソースコードをチェックする場合は、コードのブロックの下に見つけることができます

、およびヘッダOriginは変更を禁止する制限されたヘッダーの1つである:

/* 
* 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" 
}; 

簡単ですただJVM引数

-Dsun.net.http.allowRestrictedHeaders=true 

を設定するには、この問題を解決するか、あなたのコード内の行を追加します。

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

制限を抑制する。

0

同じ問題がありました。私は、この問題を持っていないことを知っているapache httpクライアントは、 '原産地'とのリクエストを送信します:http://hc.apache.org/

HttpOptions httpOptions = new 
HttpOptions(url) 
httpOptions.setHeader("Origin", "test") 
httpOptions.setHeader("Content-Type", "application/json") 
BasicHttpClientConnectionManager manager = new 
BasicHttpClientConnectionManager() 
HttpClient client = new MinimalHttpClient(manager) 
client.execute(httpOptions) 
関連する問題