我有 Eureka 和连接的服务 Zuul:8090、AuthService:[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
}
我向 postman 提出请求。 Request result
首先,我尝试在 Monoliths 中使用 JWT。没有任何问题,可以添加Authorization Token。
为什么缺少授权 header ?
最佳答案
这是因为 Zuul 内置了一个机制——它会自动过滤掉敏感 header,例如 Authorization
和 Cookies
,以保护敏感信息不被转发到下游服务。
这就是为什么您无法获取名为 Authorization
的 header 。
如果您希望下游服务接收它们,只需在 Zuul 配置文件中自行定义过滤器,而不是使用默认值。
zuul:
routes:
users:
path: your url pattern
sensitiveHeaders: //put nothing here!! leave it blank, the filter will be off
url: downstream url
这里是spring官方对敏感 header 的解释:document
关于java - 为什么缺少授权 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53351154/