
在微服务的调用中,往往会涉及到令牌(token)的传递与认证。只有令牌中继才能在调用链中保证用户认证信息的传递。
令牌中继
令牌中继(token Relay) 是比较正式的说法,用白话来说就是token令牌在服务间传递下去以保证资源服务能够正确地对调用方进行鉴权等。
token在调用链流程图

- 如图 web请求在登录授权(以获取token)通过请求网关路由到A服务,A服务以获取token(user信息)
- A服务需要请求B,或者去请求C服务,此时token如何传递?或者B,C中如何接受到user类的信息?
此时A中已经获取token,也可以解析成user等,openfeign也可以将userid传递到B或者C,这种方法固然不是最好的。openfeign中如何在上下游服务中传递token?
令牌token在feign中继
@Slf4j
@Configuration
public class FeignHeadersInterceptor implements RequestInterceptor {
private static final Pattern BEARER_TOKEN_HEADER_PATTERN = Pattern.compile("^Bearer (?<token>[a-zA-Z0-9-._~+/]+=*)$",
Pattern.CASE_INSENSITIVE);
@Override
public void apply(RequestTemplate template) {
final String authorization = HttpHeaders.AUTHORIZATION;
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (Objects.nonNull(requestAttributes)) {
String authorizationHeader = requestAttributes.getRequest().getHeader(HttpHeaders.AUTHORIZATION);
Matcher matcher = BEARER_TOKEN_HEADER_PATTERN.matcher(authorizationHeader);
if (matcher.matches()) {
// 清除token头 避免传染
template.header(authorization);
template.header(authorization, authorizationHeader);
}
}
}
}
A服务代码部分
上图中的A服务(api) 去请求B服务(other)


A服务中的Feign配置
B服务代码

结果演示
通过上图中,用户登录鉴权获取其token
获取token 后去请求 A服务

A服务会通过openfeign 去请求B服务,结果演示图

实例中的请求流程图
