spring cloud 如何生成不可用Authorization,请写出完整代码
时间: 2023-02-18 07:07:20 浏览: 46
可以使用以下代码生成不可用的Authorization:
String authorization = "";
SecureRandom secureRandom = new SecureRandom();
byte[] bytes = new byte[20];
secureRandom.nextBytes(bytes);
authorization = Base64.getEncoder().encodeToString(bytes);
相关问题
spring cloud 如何生成过期不可用Authorization,请写出完整代码
可以使用以下代码来生成过期不可用的Authorization:
String expiredAuth = "";
try {
// 获取当前时间
Date now = new Date();
// 设置过期时间,比当前时间早1分钟
Date expired = new Date(now.getTime() - 60000);
// 使用Spring Cloud提供的API创建过期不可用的Authorization
expiredAuth = Jwt.create(expired).sign(Algorithm.HMAC256("secret"));
} catch (Exception e) {
e.printStackTrace();
}
spring cloud 如何生成假Authorization,请写出完整代码
可以使用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;