【发布时间】: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