首页 > 解决方案 > 为什么缺少授权标头?

问题描述

我有 Eureka 和连接的服务Zuul:8090AuthService:[any_port]

我向 Zuul 发送../login请求,他发送给 AuthSercice。然后 AuthSerice 放入 Header JWT Authentication。

@Override
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException {
    String token = Jwts.builder()
            .setSubject( ((User) authResult.getPrincipal()).getUsername())
            .setExpiration(new Date(System.currentTimeMillis() + EXPIRATION_TIME))
            .signWith(SignatureAlgorithm.HS512, SECRET)
            .compact();

    response.addHeader("Authorization", "Bearer "+ token); // this is missing
    response.addHeader("Authorization2", "Bearer " + token); // ok
}

我确实要求邮递员。请求结果

首先,我尝试在 Monoliths 中使用 JWT。没有任何问题,可以添加授权令牌。

为什么缺少授权标头?

标签: javaauthenticationjwtmicroservices

解决方案


这是因为 Zuul 中内置的机制——它会自动过滤掉敏感的标头,例如AuthorizationCookies,以保护敏感信息不被转发到下游服务。这就是为什么您无法获得带有名称的标题Authorization

如果您希望您的下游服务无论如何都接收它们,只需在您的 Zuul 配置文件中自己定义过滤器,而不是使用默认值。

 zuul:
  routes:
    users:
      path: your url pattern
      sensitiveHeaders: //put nothing here!! leave it blank, the filter will be off
      url: downstream url

这是spring官方对敏感标题的解释:document


推荐阅读