【发布时间】:2021-04-14 05:21:39
【问题描述】:
现在我想获取访问令牌的值并执行一些逻辑。如何从响应中获取价值?任何人都可以请帮忙。 API的服务代码。
public String getToken(User user) throws JsonMappingException, JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
System.out.println(clientId+clientPass);
String plainCreds = clientId+":"+clientPass;
byte[] plainCredsBytes = plainCreds.getBytes();
byte[] base64CredsBytes = Base64.getEncoder().encode(plainCredsBytes);
String base64Creds = new String(base64CredsBytes);
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds);
HttpEntity<String> request=new HttpEntity<String>(headers);
String uri = url+ user.getUser_id()
+ "&password=" + user.getPassword();
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.POST, request, String.class) ;
JsonNode newNode = mapper.readTree(result.getBody());
ObjectNode node = ((ObjectNode) newNode).put("Authentication", "Successful");
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(node);
}
【问题讨论】:
-
也许这很有用:appsdeveloperblog.com/…
标签: spring spring-boot spring-mvc microservices rest