我在一个应用程序中使用了以下依赖项: 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客户端的应用程序/微服务中使用相同的方法时,该方法被正确地调用和执行。
在Spring Cloud Gateway应用程序中,application.yaml中的安全配置如下所示:
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_usernameSpring Cloud Gateway应用程序是否可以同时作为OAuth2客户端和资源服务器执行,或者我在应用程序的配置方面做错了吗?
发布于 2021-02-25 05:12:35
事实证明,我误解了一些基本的OAuth2概念。当我在Authorization头(隐式流)中发送JWT时,Authorization本身工作正常。不起作用的是当我试图通过浏览器访问资源时的情况。我被重定向到Keycloak的登录页面(授权代码流)。通过Keycloak的登录页面登录后,您收到的不是JWT,而是Keycloak会话ID。Spring Cloud Gateway不能基于Keycloak会话ID执行授权(如果我想使用授权码流,但我使用隐式流,则不知道如何执行授权,所以我现在还可以)。
https://stackoverflow.com/questions/65859586
复制相似问题