在使用RestTemplate請求服務(wù)端,獲取授權(quán)碼時, 會經(jīng)過兩次的 HTTP 302 Found 跳轉(zhuǎn),開發(fā)者在實(shí)現(xiàn)時需要允許客戶端跟隨跳轉(zhuǎn)。
解決辦法來自Stack Overflow的一個問答:Spring RestTemplate redirect 302
相關(guān)代碼:
final RestTemplate restTemplate = new RestTemplate();
final HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
final HttpClient httpClient = HttpClientBuilder.create()
.setRedirectStrategy(new LaxRedirectStrategy())
.build();
factory.setHttpClient(httpClient);
restTemplate.setRequestFactory(factory);
其中用到的HttpComponentsClientHttpRequestFactory、HttpClientBuilder、HttpClient等類,可以用maven來引入:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>