老师能不能讲讲feign在微服务间如何传递token的?
来源:6-3 JWT改造之网关和服务改造
郭玉成
2019-10-21
如题
2回答
-
额...我们没用feign,所以没研究过,https://blog.csdn.net/u013360850/article/details/81813262 https://www.cnblogs.com/mxmbk/p/9883629.html 百度了两个帖子供参考。
012019-10-21 -
奔跑中的耗子
2020-04-04
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignOauth2RequestInterceptor implements RequestInterceptor {
private final String AUTHORIZATION_HEADER = "Authorization";
private final String BEARER_TOKEN_TYPE = "Bearer";
@Override
public void apply(RequestTemplate requestTemplate) {
SecurityContext securityContext = SecurityContextHolder.getContext();
Authentication authentication = securityContext.getAuthentication();
if (authentication != null && authentication.getDetails() instanceof OAuth2AuthenticationDetails) {
OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) authentication.getDetails();
requestTemplate.header(AUTHORIZATION_HEADER, String.format("%s %s", BEARER_TOKEN_TYPE, details.getTokenValue()));
}
}
}
亲测可用
012020-10-17
相似问题