简体   繁体   English

Curl Token 请求到 Spring RestTemplate 转换

[英]Curl Token request to Spring RestTemplate Conversion

I am trying to convert a curl request, which is giving me a token into Spring's Resttemplate post request, but I am getting 403 - bad request我正在尝试将 curl 请求转换为 Spring 的 Resttemplate post 请求,该请求为我提供了一个令牌,但我收到了 403 - 错误的请求

My Curl Request我的卷曲请求

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

Curl Response:卷曲响应:

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

Now, Here is my Java code to implement above curl post request现在,这是我实现上述 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 call response: 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

How can make the above curl request in RestTemplate working?如何使RestTemplate的上述 curl 请求工作?

Note: https://cloudsso.example.com/as/token.oauth2 it's little modified for the question.注意: https : //cloudsso.example.com/as/token.oauth2这个问题几乎没有修改。

If you take a look at the documentation for HttpEntity you will see that you are using the wrong constructor.如果您查看HttpEntity的文档,您会发现您使用了错误的构造函数。

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

You are passing the arguments you want to use as the body ( bodyParamMap ) as headers (as the second argument is the headers to be used for the request).您将要用作正文 ( bodyParamMap ) 的参数作为标头bodyParamMap (因为第二个参数是用于请求的标头)。 In fact you aren't even using the HttpHeaders for the request as you aren't passing them to the HttpEntity .事实上,您甚至没有将HttpHeaders用于请求,因为您没有将它们传递给HttpEntity I'm not sure what the reqBodyData is but probably a toString of the bodyParamMap .我不确定reqBodyData是什么,但可能是bodyParamMaptoString

What you should have been doing is the following你应该做的是以下

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

As HttpHeaders impements MultiValueMap you can directly use them in the constructor.由于HttpHeaders实现MultiValueMap您可以直接在构造函数中使用它们。 You also want to pass the bodyParamMap as is.您还希望按bodyParamMap传递bodyParamMap

NOTE: I would also suggest to explictly set the Content-Type on the HttpHeaders so you are sure what is being send.注意:我还建议在HttpHeadersHttpHeaders设置Content-Type ,以便您确定正在发送的内容。

headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

Thus the total resulting code should look like因此总的结果代码应该看起来像

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)

NOTE: You shouldn't be constructing a single use RestTemplate but you rather want to configure that once and inject that into your class.注意:您不应该构建单一用途的RestTemplate ,而是希望配置一次并将其注入到您的类中。

As suggested by post https://stackoverflow.com/a/49127760/11226302正如帖子https://stackoverflow.com/a/49127760/11226302所建议的

You should set the request content type in header as;您应该将标头中的请求内容类型设置为; headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

Hope this helps!希望这可以帮助!

I have just solved the issue:我刚刚解决了这个问题:

I had this in this我有这个

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

I changed it to this:我把它改成这样:

private MultiValueMap<String, String> parametersMap;

and changed it并改变了它

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

to this对此

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

Now it's working perfectly fine.现在它工作得很好。

response:回复:

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

Note: in my case, adding or disabling this value, isn't making any difference, i am getting the auth token anyway.注意:就我而言,添加或禁用此值没有任何区别,无论如何我都会获得身份验证令牌。

//headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM