繁体   English   中英

如何通过身份验证请求 Feign JWT Bearer Java Spring?

[英]How to request Feign with Authentication JWT Bearer Java Spring?

我在 java spring 服务核心和服务商店中使用两个服务,我想使用服务核心中的方法从服务核心插入数据,但在服务核心中我需要承载令牌,我如何将身份验证承载令牌从服务商店发送到服务核心?

实现RequestInterceptor来设置 Authentication 来请求 header。

    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,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM