Spring Cloud Zuul API gateway doesn't forward JWT token for stateless sessions

寵の児 提交于 2019-12-04 10:53:58

Zuul considers Authorization header as a sensitive header by default and does not pass it to downstream requests. To override this, you can modify sensitiveHeaders in Zuul configuration either globally (for all routes):

zuul:
  # exclude Authorization from sensitive headers
  sensitiveHeaders: Cookie,Set-Cookie
  ignoredServices: '*'

Or for a specific route:

zuul:
  ignoredServices: '*'
  routes:
    user-service:
      path: /user-service/**
      stripPrefix: false
      serviceId: user-webservice
      # exclude Authorization from sensitive headers
      sensitiveHeaders: Cookie,Set-Cookie

To find more about the problem, check this question:

Authorization header not passed by ZuulProxy starting with Brixton.RC1

I was assuming that with @EnableZuulProxy and @EnableOAuth2Sso, Zuul would take care to forward the bearer token to the downstream services but that is not happening.

I assumed the same thing, but in my (painful) experience, @EnableOAuth2Sso secures all endpoints with SSO and blocks even the requests with a Bearer token from getting to downstream services. I had to change my gateway to disable authentication on the routes that lead to my resources, so that the request with a Bearer token could get through.

Try adding /user-service/** and /task-service/** to your permitAll() matcher:

@Override
public void configure(HttpSecurity http) throws Exception {

    // @formatter:off
    http
        .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        .and()
            .authorizeRequests()
                .antMatchers("/sign-up", "/login", "/task-service/**", "/user-service/**")
                    .permitAll()
            .anyRequest()
                .authenticated()
        .and()
            .csrf()
                .ignoringAntMatchers("/sign-up", "/login")
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
    // @formatter:on
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!