spring cloud 如何生成假Authorization,请写出完整代码
时间: 2023-02-18 21:13:51 浏览: 89
可以使用Spring Security OAuth2框架来实现,可以参考以下代码:
String clientId = "clientId";
String clientSecret = "clientSecret";
String tokenUri = "tokenUri";
// 设置请求参数
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("grant_type", "client_credentials");
params.add("client_id", clientId);
params.add("client_secret", clientSecret);
// 设置请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
// 发送请求
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(params, headers);
ResponseEntity<String> response = restTemplate.postForEntity(tokenUri, request, String.class);
// 获取token
String accessToken = response.getBody();
// 生成假Authorization
String fakeAuthorization = "Bearer " + accessToken;
阅读全文