Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
480 views
in Technique[技术] by (71.8m points)

spring - Authorization not working in Gateway with OAuth2 Client + Resource Server

I am using the following dependencies in one application: Spring-Cloud-Gateway, Spring Boot OAuth2 Client, Spring Boot OAuth2 Resource Server.

I use the following security config:

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

When I execute calls I am correctly advised to login which works fine. But it's just the Authentication that works, not the Authorization. When I use the debugger I can see that the userJwtAuthenticationConverter() method is never called to use roles out of the JWT.

When I use the same method in another application/microservice which is just a OAuth2 Resource Server, but not a OAuth2 Client the method is correctly called and executed.

The security config in the application.yaml looks like the following in the Spring Cloud Gateway application:

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

Isn't it possible for the Spring Cloud Gateway application to perform as a OAuth2 Client and Resource Server at the same time or am I doing a mistake regarding the configuring of the application?

question from:https://stackoverflow.com/questions/65859586/authorization-not-working-in-gateway-with-oauth2-client-resource-server

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...