JWT Zuul Feign 传递userId

前言

聚合了资源到 zuul 之后。导致审核功能,无法记录操作用户,产生了需要传递userId的需求。又不能吧JWT 直接传给服务。

路由端

private Authentication getAuthentication(HttpServletRequest request) {

        String token = request.getHeader("Authorization");
        if (token != null && token.startsWith(AUTHENTICATION_PREFIX)) {
            // parse the token.超时会抛 ExpiredJwtException
            Jws claimsJws = Jwts.parser()
                    .setSigningKey(secret)
                    .parseClaimsJws(token.replace(AUTHENTICATION_PREFIX, ""));

            String subject = claimsJws.getBody().getSubject();
            if (subject != null) {

                Long userId = Long.parseLong(subject);
                UnifyResponse user =  userClient.findById(userId);
                List authorities=toAuthorities(user.getContent().getAuthorities());
                /**
                 * 传递userId 给其它服务
                 */
                RequestContext ctx = RequestContext.getCurrentContext();
                ctx.getZuulRequestHeaders().put("userId",userId.toString());
                return new DashboardJwtAuthenticationToken(subject, null, authorities);
            }
            return null;
        }
        return null;
    }

服务端

 @ApiOperation(value = "审核-拒绝")
    @PostMapping(value = {"/promotions/{id}/review/refuse"})
    @PutMapping(value = {"/promotions/{id}/review/refuse"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header",name="userId",dataType="int",value="用户id,由路由通过jwt进行填充",defaultValue = "0" ,example = "0")
    })
    public UnifyResponse refuse(@RequestHeader(required = false,value = "userId") Long userId,@PathVariable String id, @RequestBody PromotionReviewRequest promotionReviewRequest,HttpServletRequest httpServletRequest){

     }

你可能感兴趣的:(JWT Zuul Feign 传递userId)