Java 在微服务之间共享用户id的最佳实践是什么?

Java 在微服务之间共享用户id的最佳实践是什么?,java,spring,spring-boot,spring-security,Java,Spring,Spring Boot,Spring Security,到目前为止,我已经部署了两个Spring Boot微服务: 认证网关 具有业务逻辑的Restful服务 身份验证网关在成功登录后发出jwt令牌。对于下一个请求,它在将请求重定向到业务逻辑之前验证/授权jwt令牌。 从Auth Gateway到其他服务共享用户相关信息的最佳方式是什么 在之间,使用SpringSecurity和Spring Boot编写身份验证网关。在JWT令牌中编码所有必要的详细信息(用户id等) 您在Auth网关中发布令牌,JWT访问令牌有三个部分:头、声明和签名 将所有必要

到目前为止,我已经部署了两个Spring Boot微服务:

  • 认证网关
  • 具有业务逻辑的Restful服务
  • 身份验证网关在成功登录后发出jwt令牌。对于下一个请求,它在将请求重定向到业务逻辑之前验证/授权jwt令牌。
    从Auth Gateway到其他服务共享用户相关信息的最佳方式是什么


    之间,使用SpringSecurity和Spring Boot编写身份验证网关

    在JWT令牌中编码所有必要的详细信息(用户id等)

    您在Auth网关中发布令牌,JWT访问令牌有三个部分:头、声明和签名

    将所有必要的信息放入索赔部分。看


    在JWT中编码所有必要的详细信息(用户id等)token@StanislavL你是说,在登录后用用户id等更新jwt令牌?
    @Component
    public class JwtTokenFactory {  
        private final JwtSettings settings;
    
        @Autowired
        public JwtTokenFactory(JwtSettings settings) {
            this.settings = settings;
        }
    
        /**
         * Factory method for issuing new JWT Tokens.
         * 
         * @param username
         * @param roles
         * @return
         */
        public AccessJwtToken createAccessJwtToken(UserContext userContext) {
            if (StringUtils.isBlank(userContext.getUsername())) 
                throw new IllegalArgumentException("Cannot create JWT Token without username");
    
            if (userContext.getAuthorities() == null || userContext.getAuthorities().isEmpty()) 
                throw new IllegalArgumentException("User doesn't have any privileges");
    
            Claims claims = Jwts.claims().setSubject(userContext.getUsername());
            claims.put("scopes", userContext.getAuthorities().stream().map(s -> s.toString()).collect(Collectors.toList()));
    
            DateTime currentTime = new DateTime();
    
            String token = Jwts.builder()
              .setClaims(claims)
              .setIssuer(settings.getTokenIssuer())
              .setIssuedAt(currentTime.toDate())
              .setExpiration(currentTime.plusMinutes(settings.getTokenExpirationTime()).toDate())
              .signWith(SignatureAlgorithm.HS512, settings.getTokenSigningKey())
            .compact();
    
            return new AccessJwtToken(token, claims);
        }