簡體   English   中英

Curl Token 請求到 Spring RestTemplate 轉換

[英]Curl Token request to Spring RestTemplate Conversion

我正在嘗試將 curl 請求轉換為 Spring 的 Resttemplate post 請求,該請求為我提供了一個令牌,但我收到了 403 - 錯誤的請求

我的卷曲請求

curl -d "grant_type=client_credentials&client_id=nu5yzeth9tektzf5egxuntp7&client_secret=uP2Xvr6SCKYgXgxxJsv2QkUG" 
  -H "Content-Type: application/x-www-form-urlencoded" 
  -X POST https://cloudsso.example.com/as/token.oauth2

卷曲響應:

{"access_token":"HVURQ845OPJqs8UpOlef5m2ZCNwR","token_type":"Bearer","expires_in":3599}

現在,這是我實現上述 curl post 請求的 Java 代碼

RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

MultiValueMap<String, String> bodyParamMap = new LinkedMultiValueMap<String, String>();
bodyParamMap.add("grant_type", "client_credentials");
bodyParamMap.add("client_id", "nu5yzeth9tektzf5egxuntp7");
bodyParamMap.add("client_secret", "uP2Xvr6SCKYgXgxxJsv2QkUG");

entity = new HttpEntity<>(reqBodyData, bodyParamMap);

restTemplate.postForEntity(url, entity, TokenDTO.class)

Resttemplate 調用響應:

2019-12-04 11:10:16,483[0;39m [39mDEBUG[0;39m [[34mrestartedMain[0;39m] [33morg.springframework.core.log.CompositeLog[0;39m: Accept=[application/json, application/*+json]
[30m2019-12-04 11:10:16,484[0;39m [39mDEBUG[0;39m [[34mrestartedMain[0;39m] [33morg.springframework.core.log.CompositeLog[0;39m: Writing [{"grant_type":["client_credentials"],"client_id":["nu5yzeth9tektzf5egxuntp7"],"client_secret":["uP2Xvr6SCKYgXgxxJsv2QkUG"]}] with org.springframework.http.converter.StringHttpMessageConverter
[30m2019-12-04 11:10:17,976[0;39m [39mDEBUG[0;39m [[34mrestartedMain[0;39m] [33morg.springframework.core.log.CompositeLog[0;39m: Response 400 BAD_REQUEST
[30m2019-12-04 11:10:17,981[0;39m [34mINFO [0;39m [[34mrestartedMain[0;39m] [33mcom.ibm.ciscoApiIntegration.service.impl.CiscoAPIServiceImpl[0;39m: e::: 400 Bad Request

如何使RestTemplate的上述 curl 請求工作?

注意: https : //cloudsso.example.com/as/token.oauth2這個問題幾乎沒有修改。

如果您查看HttpEntity的文檔,您會發現您使用了錯誤的構造函數。

entity = new HttpEntity<>(reqBodyData, bodyParamMap);

您將要用作正文 ( bodyParamMap ) 的參數作為標頭bodyParamMap (因為第二個參數是用於請求的標頭)。 事實上,您甚至沒有將HttpHeaders用於請求,因為您沒有將它們傳遞給HttpEntity 我不確定reqBodyData是什么,但可能是bodyParamMaptoString

你應該做的是以下

entity = new HttpEntity<>(bodyParamMap, headers);

由於HttpHeaders實現MultiValueMap您可以直接在構造函數中使用它們。 您還希望按bodyParamMap傳遞bodyParamMap

注意:我還建議在HttpHeadersHttpHeaders設置Content-Type ,以便您確定正在發送的內容。

headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

因此總的結果代碼應該看起來像

RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

MultiValueMap<String, String> bodyParamMap = new LinkedMultiValueMap<>();
bodyParamMap.add("grant_type", "client_credentials");
bodyParamMap.add("client_id", "nu5yzeth9tektzf5egxuntp7");
bodyParamMap.add("client_secret", "uP2Xvr6SCKYgXgxxJsv2QkUG");

entity = new HttpEntity<>(bodyParamMap, headers);

restTemplate.postForEntity(url, entity, TokenDTO.class)

注意:您不應該構建單一用途的RestTemplate ,而是希望配置一次並將其注入到您的類中。

正如帖子https://stackoverflow.com/a/49127760/11226302所建議的

您應該將標頭中的請求內容類型設置為; headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

希望這可以幫助!

我剛剛解決了這個問題:

我有這個

// wrong
//private HttpEntity<String> entity;

我把它改成這樣:

private MultiValueMap<String, String> parametersMap;

並改變了它

// not working
entity = new HttpEntity<>(reqBodyData, bodyParamMap);

對此

// working
entity = new HttpEntity<>(bodyParamMap, headers);

現在它工作得很好。

回復:

2019-12-04 11:52:46,503 DEBUG [restartedMain] org.springframework.core.log.CompositeLog: HTTP POST https://cloudsso.example.com/as/token.oauth2
2019-12-04 11:52:46,521 DEBUG [restartedMain] org.springframework.core.log.CompositeLog: Accept=[application/json, application/*+json]
2019-12-04 11:52:46,522 DEBUG [restartedMain] org.springframework.core.log.CompositeLog: Writing [{grant_type=[client_credentials], client_id=[nu5yzeth9tektzf5egxuntp7], client_secret=[uP2Xvr6SCKYgXgxxJsv2QkUG]}] with org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter
2019-12-04 11:52:47,831 DEBUG [restartedMain] org.springframework.core.log.CompositeLog: Response 200 OK

注意:就我而言,添加或禁用此值沒有任何區別,無論如何我都會獲得身份驗證令牌。

//headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM