2

我試圖實現使用春季啓動1.5.6.RELEASE春雲Dalston.SR3微服務架構的後端,將通過移動消耗/網絡端點。春雲Zuul API網關不會轉發JWT令牌無狀態會話

API網關應用

@SpringBootApplicatio 
@EnableEurekaClient 
@EnableZuulProxy 
public class GatewayApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(GatewayApplication.class, args); 
    } 
} 

API安全

@Configuration 
@EnableWebSecurity 
@Order(ManagementServerProperties.ACCESS_OVERRIDE_ORDER) 
@EnableOAuth2Sso 
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { 

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

     // @formatter:off 
     http 
      .sessionManagement() 
       .sessionCreationPolicy(SessionCreationPolicy.STATELESS) 
      .and() 
       .authorizeRequests() 
        .antMatchers("/sign-up", "/login") 
         .permitAll() 
       .anyRequest() 
        .authenticated() 
      .and() 
       .csrf() 
        .ignoringAntMatchers("/sign-up", "/login") 
        .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()); 
     // @formatter:on 
    } 
} 

搖籃安全相關的依賴

// Spring OAuth2 security 
    compile("org.springframework.boot:spring-boot-starter-security") 
    compile("org.springframework.security.oauth:spring-security-oauth2") 
    compile("org.springframework.cloud:spring-cloud-starter-oauth2") 
    compile("org.springframework.security:spring-security-jwt") 

Zuul路線

zuul: 
    ignoredServices: '*' 
    routes: 
    user-service: 
     path: /user-service/** 
     stripPrefix: false 
     serviceId: user-webservice 
     sensitiveHeaders: 
    task-service: 
     path: /task-service/** 
     stripPrefix: false 
     serviceId: task-webservice 
     sensitiveHeaders: 
    user: 
     path: /userauth/** 
     stripPrefix: false 
     serviceId: auth-server 
     sensitiveHeaders: 

我能夠得到授權服務器的訪問令牌(無狀態會話 - 沒有JSESSIONID的cookie)

捲曲-D - --request POST -u極致:acmesecret 「http://localhost:8899/userauth/oauth/token?grant_type=password&username= < ...> &密碼= < ...> 「

{ 」的access_token「:」 eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MDQ3ODg4NzgsInVzZXJfbmFtZSI6IjcyMTk2MTk2NDEiLCJhdXRob3JpdGllcyI6WyJST0xFX1BBVElFTlQiXSwianRpIjoiZThhMzBjNmQtZjA2MS00MWEzLWEyZGItYTZiN2ZjYTI5ODk1IiwiY2xpZW50X2lkIjoiYWNtZSIsInNjb3BlIjpbIm9wZW5pZCJdfQ.AhF_kqfsRYM1t1HVT ........

我可以使用訪問令牌從授權服務器或其他資源請求數據

捲曲-D - --request GET -H 「授權:承載 eyJhbGciOiJSUzI1 ......」 http://localhost:8899/userauth/me

{ 「權威」:[{ 「權威」: 「ROLE_P .........}

捲曲-D - --request GET -H」 授權:承載 eyJhbGciOiJSUzI1NiIsInR5 ... ....「http://localhost:8081/user-service/

[{「名字」:「阿尼爾」 .....}]

然而,對於通過API網關路由相同的請求時,它未能在網關本身和被過濾爲AnonymousAuthenticationToken。

捲曲-D - --request GET -H 「授權:承載 eyJhbGciOiJSUzI1 ....」 http://localhost:8765/user-service/

HTTP/1.1 302設置Cookie: XSRF-TOKEN = b5a1c34e-e83c-47EA -86a6-13a237c027d4;路徑= /地點: http://localhost:8765/login

我是假設與@EnableZuulProxy@EnableOAuth2Sso,Zuul會照顧轉發承載令牌到下游服務,但情況並非如此。我已經有一個工作示例,使用HTTP會話和瀏覽器重定向來獲取API網關傳遞令牌 - https://github.com/anilallewar/microservices-basics-spring-boot

但我很努力讓它與無狀態會話一起工作,任何指針可能缺少Zuul API網關方面?

+0

你有這個工作?我陷入了同樣的問題。 你能否擺脫一些光線? – rohit

回答

1

Zuul默認將授權標頭視爲敏感標頭,並且不會將其傳遞給下游請求。要覆蓋這一點,你可以在Zuul配置包括全局的(適用於所有航線)修改sensitiveHeaders

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

或特定路線:

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

要了解更多有關問題,檢查這個問題:

Authorization header not passed by ZuulProxy starting with Brixton.RC1

+0

如果您查看原始問題,則敏感標頭已設置爲空(sensitiveHeaders:)。這絕對不是問題。 –

0

我是假設與@EnableZuulProxy和@Ena bleuuth2Sso,Zuul會小心地將不記名令牌轉發給下游服務,但這不會發生。

我認爲同樣的事情,但在我的(痛苦)的經驗,@ EnableOAuth2Sso確保與SSO和塊甚至從獲取到下游業務的承載令牌的請求的所有端點。我不得不改變我的網關以禁用導致我的資源的路由的身份驗證,以便帶有令牌令牌的請求可以通過。

嘗試增加/user-service/**/task-service/**permitAll()匹配:

@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 
}