如何解决如何使用身份验证 JWT Bearer Java Spring 请求 Feign?
我在java spring服务核心和服务商店中使用了两个服务,我想使用服务核心中的方法从服务核心插入数据,但在服务核心中我需要承载令牌,如何从服务商店发送身份验证承载令牌服务核心?
解决方法
实现 RequestInterceptor
以将身份验证设置为请求标头。
public void apply(RequestTemplate template) {
if (RequestContextHolder.getRequestAttributes() != null && RequestContextHolder.getRequestAttributes() instanceof ServletRequestAttributes) {
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
String authorization = request.getHeader("Authorization");
if (StringUtils.isNotBlank(authorization)) {
template.header("Authorization",new String[]{authorization});
}
}
}
,
当然,您需要一种使用 client-credentials
授权类型从众所周知的 OAuth 端点获取服务令牌的方法
如果您想在每个集成的基础上执行此操作,可能是因为您使用不同的方法与不同的服务进行集成,您可以执行以下操作:
@RequestMapping(method = [RequestMethod.GET],value = ["/id/{id}"],produces = [MediaType.APPLICATION_JSON_VALUE],consumes = [MediaType.APPLICATION_JSON_VALUE])
operator fun get(
@RequestHeader("Authorization") bearerToken: String = "Bearer mybiglongencodedtoken....",@PathVariable("id") code: String,
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。