The problem encountered is that when the client authentication mode of feign and oauth2 is used for permission access control between services, the accesstoken of the authentication server cannot be obtained. The general process is that microservice A accesses the authentication server to obtain the authentication token, and then puts the token into the feign interceptor apply method as the request header to access the protected resource microservice b I don't know why I can't get an authenticated token
This is a similar problem I looked up
Enabling OAuth2 with Feign for scheduled cross-service tasks
@Override
public void apply(RequestTemplate template) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null){
String tokenValue = null;
if (auth.getDetails() instanceof OAuth2AuthenticationDetails) {
OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) auth.getDetails();
tokenValue = details.getTokenValue();
} else if (auth.getDetails() instanceof InternalOAuth2Details) {
InternalOAuth2Details details = (InternalOAuth2Details) auth.getDetails();
tokenValue = details.getTokenValue();
}
if (tokenValue == null) {
log.warn("Current token value is null");
return;
}
template.header("Authorization", "Bearer " + tokenValue);
}
}
InternalOAuth2Details out of date unusable