spring cloud 如何生成有效但不可用Authorization,请写出完整代码
时间: 2023-02-18 13:07:20 浏览: 61
可以使用以下代码:
String authorization = Base64.getEncoder().encodeToString("username:password".getBytes());
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Basic " + authorization);
HttpEntity<String> entity = new HttpEntity<>("parameters", headers);
ResponseEntity<String> response = restTemplate.exchange("url", HttpMethod.GET, entity, String.class);
相关问题
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,请写出完整代码
可以使用以下代码生成不可用的Authorization:
String authorization = "";
SecureRandom secureRandom = new SecureRandom();
byte[] bytes = new byte[20];
secureRandom.nextBytes(bytes);
authorization = Base64.getEncoder().encodeToString(bytes);