首页 > 解决方案 > 如何将用户凭据从 zuul 发送到 Spring Boot 微服务中的微服务模块

问题描述

我有三个微服务模块 auth-server、zuul-server 和 product-server。我想将 auth-server 授权的用户凭据从 zuul-server 发送到 product-server 并在proudct-server 中获取?

我试图在 zuul 服务器标头中设置令牌并从产品服务器获取它。但是我在产品服务器中获得了 null。

if (username != null) { 
   @SuppressWarnings("unchecked") List<String> authorities = (List<String>) claims.get("authorities"); 
   UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken( username, null, authorities.stream().map( SimpleGrantedAuthority::new ).collect(Collectors.toList()));
   SecurityContextHolder.getContext().setAuthentication(auth);
}

我想要这个安全上下文到另一个微服务模块,所以我可以从中获取用户凭据。

标签: javaspringsecurityjwtmicroservices

解决方案


推荐阅读