【问题标题】:Authorization not working in Gateway with OAuth2 Client + Resource Server使用 OAuth2 客户端 + 资源服务器的网关中的授权不起作用
【发布时间】:2021-04-27 18:31:36
【问题描述】:

我在一个应用程序中使用以下依赖项:Spring-Cloud-Gateway、Spring Boot OAuth2 客户端、Spring Boot OAuth2 资源服务器。

我使用以下安全配置:

@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http, ReactiveClientRegistrationRepository clientRegistrationRepository) {
        
        http.oauth2Login();

        http.logout(logout -> logout.logoutSuccessHandler(
                new OidcClientInitiatedServerLogoutSuccessHandler(clientRegistrationRepository)));


        http.authorizeExchange()
                .pathMatchers("/actuator/health").permitAll()
                .pathMatchers("/auth/realms/ahearo/protocol/openid-connect/token").permitAll()
                .pathMatchers("/v3/api-docs").permitAll()
                .anyExchange().authenticated()
                .and()
                .oauth2ResourceServer()
                .jwt()
                .jwtAuthenticationConverter(userJwtAuthenticationConverter());

         http.csrf().disable().formLogin().disable().httpBasic().disable();
        return http.build();
}

@Bean
public UserJwtAuthenticationConverter userJwtAuthenticationConverter() {
    return new UserJwtAuthenticationConverter();
}

当我执行呼叫时,我被正确地建议登录,这工作正常。但它只是 Authentication 起作用,而不是 Authorization。当我使用调试器时,我可以看到永远不会调用 userJwtAuthenticationConverter() 方法来使用 JWT 之外的角色。

当我在另一个只是 OAuth2 资源服务器而不是 OAuth2 客户端的应用程序/微服务中使用相同的方法时,该方法被正确调用和执行。

application.yaml 中的安全配置在 Spring Cloud Gateway 应用程序中如下所示:

security:
    oauth2:
      resourceserver:
        jwt:
          issuer-uri: http://localhost/auth/realms/example-realm
          jwk-set-uri: http://localhost/auth/realms/example-realm/protocol/openid-connect/certs
      client:
        registration:
          keycloak:
            client-id: 'example-proxy-client'
            client-secret: 'xxx'
            authorizationGrantType: authorization_code
            redirect-uri: '{baseUrl}/login/oauth2/code/{registrationId}'
            scope: openid,profile,email
        provider:
          keycloak:
            issuer-uri: http://localhost/auth/realms/example-realm
            user-name-attribute: preferred_username

Spring Cloud Gateway 应用程序是否可以同时作为 OAuth2 客户端和资源服务器执行,或者我在应用程序的配置方面犯了错误?

【问题讨论】:

  • 当我添加像pathMatchers("/test/**).hasAnyRole("ADMIN")" 这样的授权行时,我在登录后收到了 HTTP 403,尽管用户具有所需的角色。在调试器中,我可以看到没有调用 UserJwtAuthenticationConverter 中的任何方法。

标签: spring spring-boot spring-security oauth-2.0 spring-cloud


【解决方案1】:

原来我误解了一些基本的 OAuth2 概念。 当我在授权标头(隐式流)中发送 JWT 时,授权本身工作正常。当我尝试通过浏览器访问资源时,情况不起作用。 我被重定向到 Keycloak (Authorization Code Flow) 的登录页面。通过 Keycloak 的登录页面登录后,您不会收到 JWT,而是收到 Keycloak 会话 ID。 Spring Cloud Gateway 无法基于 Keycloak 会话 ID 执行授权(如果我想使用授权代码流但我正在使用隐式流,我不知道这将如何工作,所以我现在很好)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-21
    • 2014-07-09
    • 2017-08-26
    • 2019-04-17
    • 2022-08-03
    • 2019-10-27
    • 2017-11-10
    • 2018-06-23
    相关资源
    最近更新 更多