如何请求 Feign with Authentication JWT Bearer Java Spring?

How to request Feign with Authentication JWT Bearer Java Spring?

I use two service in java spring service core and service shop, i want to insert data from service core with method in service core, but in service core i need to bearer token, how to i send auth bearer token from service shop to service core?

正在实施 RequestInterceptor 以将身份验证设置为请求 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,