授权在oauth2客户端+资源服务器的网关中不起作用

hrysbysz  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(415)

我在一个应用程序中使用以下依赖项:spring cloud gateway、spring boot oauth2 client、spring boot oauth2 resource server。
我使用以下安全配置:

@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();
}

当我执行调用时,我被正确地建议登录,这样做很好。但只有身份验证有效,而不是授权。当我使用调试器时,我可以看到 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

SpringCloudGateway应用程序不可能同时作为oauth2客户机和资源服务器运行吗?或者我在配置应用程序时犯了错误吗?

v2g6jxz6

v2g6jxz61#

原来我误解了一些基本的oauth2概念。当我在授权头(隐式流)中发送jwt时,授权本身工作正常。当我试图通过浏览器访问一个资源时,没有起作用。我被重定向到keydrope(授权代码流)的登录页面。在您通过keydove的登录页面登录之后,您不会收到jwt,而是一个keydove会话id。spring cloud gateway不能基于keydove会话id执行授权(不知道如果我想使用授权代码流,但我使用的是隐式流,所以我现在还可以)。

相关问题