spring cloud 如何生成不可用Authorization,请写出完整代码
时间: 2023-02-18 20:07:20 浏览: 86
可以使用以下代码生成不可用的Authorization:
String authorization = "";
SecureRandom secureRandom = new SecureRandom();
byte[] bytes = new byte[20];
secureRandom.nextBytes(bytes);
authorization = Base64.getEncoder().encodeToString(bytes);
相关问题
spring cloud 如何生成过期不可用Authorization,请写出完整代码
可以使用Spring Security OAuth2框架来实现,完整代码如下:
@Configuration
public class OAuth2Config {
@Bean
public TokenStore tokenStore() {
return new InMemoryTokenStore();
}
@Bean
public DefaultTokenServices tokenServices() {
DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
defaultTokenServices.setTokenStore(tokenStore());
defaultTokenServices.setSupportRefreshToken(true);
defaultTokenServices.setAccessTokenValiditySeconds(60);
return defaultTokenServices;
}
@Bean
public TokenEnhancer tokenEnhancer() {
return new TokenEnhancer() {
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
((DefaultOAuth2AccessToken) accessToken).setExpiration(new Date(System.currentTimeMillis() + 60 * 1000));
return accessToken;
}
};
}
}
spring cloud 如何生成有效但不可用Authorization,请写出完整代码
可以使用以下代码:
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);
阅读全文