首页 > 解决方案 > 没有找到类 org.springframework.security.oauth2.client.authentication.OAuth2AuthorizationCodeAuthenticationToken 的提供者

问题描述

我创建了三个应用程序,分别为“spring cloud gateway(8081)”、“spring oauth2 auth server(8094)”和“spring oauth2 resource server(8097)”。

当我想请求资源服务器时,首先我需要请求 gw,它转发到 oauth 服务器并在那里登录(oauth 也有 spring 安全层)。登录成功后,它会重定向到后面的 gw 服务器,例如http://localhost:8081/login/oauth2/code/gateway?code=6ldKVF&state=0WvvWdTs8G_XchSTQKqgokua_XDVQziqVZ_VXLMqIS0%3D url。然后,屏幕发生错误。

当我在身份验证服务器上成功登录时,网关服务器控制台中有一个跟踪日志:

2020-01-17 17:52:11.825 TRACE 11336 --- [ctor-http-nio-4] oshttp.codec.json.Jackson2JsonDecoder :[21762c89] Decoded [{access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsib2F1dGgyLXJlc291cmNlIl0sInVzZXJfbmFtZSI6ImRnIiwic2NvcGUiOlsiY3VzdG9tX21vZCJdLCJleHAiOjE1NzkyNzI3NDEsImF1dGhvcml0aWVzIjpbIlJPTEVfQURNSU4iXSwianRpIjoiOWUzYzQ2YTQtMDJiZi00MTgwLTg1ZTktMGJhOTM0MjBhYjg4IiwiY2xpZW50X2lkIjoiZmlyc3QtY2xpZW50In0.xdWGm420tvp2Rzq0AyCgOTcDuKvP-V6JFd76KmJJf7o, token_type= bearer, refresh_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsib2F1dGgyLXJlc291cmNlIl0sInVzZXJfbmFtZSI6ImRnIiwic2NvcGUiOlsiY3VzdG9tX21vZCJdLCJhdGkiOiI5ZTNjNDZhNC0wMmJmLTQxODAtODVlOS0wYmE5MzQyMGFiODgiLCJleHAiOjE1NzkyNzI3NTEsImF1dGhvcml0aWVzIjpbIlJPTEVfQURNSU4iXSwianRpIjoiYzEyNWExM2ItMmMzYS00ZGM0LWJjODgtZDc4ZDk1ZTljNzQ5IiwiY2xpZW50X2lkIjoiZmlyc3QtY2xpZW50In0.KhRIy7wOH2IsswDZ_AIXVFdtu6JZqtiLBZGZIypeNRw, expires_in=9, scope=custom_mod, jti=9e3c46a4-02bf-4180-85e9-0ba93420ab88}]

当我解码以访问令牌 jwt 时,结果如下。

{
"aud": [
"oauth2-resource"
],
"user_name": "dg",
"scope": [
"custom_mod"
],
"exp": 1579272741,
"authorities": [
"ROLE_ADMIN"
],
"jti": "9e3c46a4-02bf-4180-85e9-0ba93420ab88",
"client_id": "first-client"
}

我想,我缺少一些要在网关服务器或 oauth 服务器中实现的部分,但我找不到。因为当我尝试使用 okta 而不是我的自定义身份验证服务器时,没有错误。

网关应用程序.properties

server.port=8081
spring.security.oauth2.client.registration.gateway.client-id=first-client
spring.security.oauth2.client.registration.gateway.client-secret=noonewilleverguess
spring.security.oauth2.client.registration.gateway.authorization-grant-type=authorization_code
spring.security.oauth2.client.registration.gateway.redirect-uri={baseUrl}/login/oauth2/code/{registrationId}

spring.security.oauth2.client.provider.gateway.authorization-uri=http://localhost:8094/oauth/authorize
spring.security.oauth2.client.provider.gateway.token-uri=http://localhost:8094/oauth/token?scope=custom_mod
spring.security.oauth2.client.provider.gateway.user-info-uri=http://localhost:8094/userinfo
spring.security.oauth2.client.provider.gateway.user-name-attribute=name

logging.level.root=trace

这是屏幕错误消息:

Fri Jan 17 17:28:11 EET 2020 [83c168ec] 出现意外错误(类型=内部服务器错误,状态=500)。没有找到类 org.springframework.security.oauth2.client.authentication.OAuth2AuthorizationCodeAuthenticationToken 的提供者

这是控制台错误消息:

2020-01-17 17:40:27.204 错误 7180 --- [ctor-http-nio-2] awreAbstractErrorWebExceptionHandler : [93951d85] HTTP GET "/login/oauth2/code/gateway 的 500 服务器错误?code=n35lEN&state=fZ94ZsZySnUwaG1vS32cX4sXUrwR988 %3D"

java.lang.IllegalStateException:在 org.springframework.security.web.server.authentication.AuthenticationWebFilter.lambda$authenticate$5(AuthenticationWebFilter.java:118) 找不到类 org.springframework.security.oauth2.client.authentication.OAuth2AuthorizationCodeAuthenticationToken 的提供程序~[spring-security-web-5.2.1.RELEASE.jar:5.2.1.RELEASE] 抑制:reactor.core.publisher.FluxOnAssembly$OnAssemblyException:在以下站点观察到错误:|_ checkpoint ⇢ org.springframework.security.oauth2.client.web.server.authentication.OAuth2LoginAuthenticationWebFilter [DefaultWebFilterChain] |_ checkpoint ⇢ org.springframework.security.oauth2.client.web.server.OAuth2AuthorizationRequestRedirectWebFilter [DefaultWebFilterChain] |_ checkpoint ⇢ org.springframework。 security.oauth2.client.web.server。OAuth2AuthorizationRequestRedirectWebFilter [DefaultWebFilterChain] |_ checkpoint ⇢ org.springframework.security.web.server.context.ReactorContextWebFilter [DefaultWebFilterChain] |_ checkpoint ⇢ org.springframework.security.web.server.csrf.CsrfWebFilter [DefaultWebFilterChain] |_ checkpoint ⇢ org. springframework.security.web.server.header.HttpHeaderWriterWebFilter [DefaultWebFilterChain] |_ checkpoint ⇢ org.springframework.security.config.web.server.ServerHttpSecurity$ServerWebExchangeReactorContextWebFilter [DefaultWebFilterChain] |_ checkpoint ⇢ org.springframework.security.web.server。 WebFilterChainProxy [DefaultWebFilterChain] |_ checkpoint ⇢ HTTP GET "/login/oauth2/code/gateway?code=n35lEN&state=fZ94ZsZySnUwaG1vS32cX4sXU9KJ6aRm58twQMrR9sQ%3D" [ExceptionHandlingWebHandler] 堆栈跟踪:在 org.springframework.security.web.server.authentication.AuthenticationWebFilter.lambda$authenticate$5(AuthenticationWebFilter.java:118) ~[spring-security-web-5.2.1.RELEASE.jar:5.2.1.RELEASE] 在 reactor.core。 publisher.MonoDefer.subscribe(MonoDefer.java:44) ~[reactor-core-3.3.1.RELEASE.jar:3.3.1.RELEASE] 在 reactor.core.publisher.Mono.subscribe(Mono.java:4105) ~ [reactor-core-3.3.1.RELEASE.jar:3.3.1.RELEASE] 在 reactor.core.publisher.FluxSwitchIfEmpty$SwitchIfEmptySubscriber.onComplete(FluxSwitchIfEmpty.java:75) ~[reactor-core-3.3.1.RELEASE。罐子:3.3.1.RELEASE]RELEASE] 在 reactor.core.publisher.Mono.subscribe(Mono.java:4105) ~[reactor-core-3.3.1.RELEASE.jar:3.3.1.RELEASE] 在 reactor.core.publisher.FluxSwitchIfEmpty$SwitchIfEmptySubscriber。 onComplete(FluxSwitchIfEmpty.java:75) ~[reactor-core-3.3.1.RELEASE.jar:3.3.1.RELEASE]RELEASE] 在 reactor.core.publisher.Mono.subscribe(Mono.java:4105) ~[reactor-core-3.3.1.RELEASE.jar:3.3.1.RELEASE] 在 reactor.core.publisher.FluxSwitchIfEmpty$SwitchIfEmptySubscriber。 onComplete(FluxSwitchIfEmpty.java:75) ~[reactor-core-3.3.1.RELEASE.jar:3.3.1.RELEASE]

编辑:缩短以了解问题。

标签: spring-securityspring-cloudspring-security-oauth2spring-oauth2spring-cloud-gateway

解决方案


自从发布这个问题以来已经有一段时间了。但是,您有可能在没有相应 jose 客户端的情况下导入 spring security oauth2 客户端。

所以你最好添加

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>

推荐阅读