我按照Spring Security 5.0官方引用文档和示例代码oauth2login在我的项目中设置 OAuth2/OIDC 身份验证,但它失败了,当我通过 mvn spring-boot:run
启动应用程序时出现以下异常。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientRegistrationRepository'
defined in class path resource [org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2ClientRegistrationRepositoryConfiguration.class]:
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository]:
Factory method 'clientRegistrationRepository' threw exception;
nested exception is java.lang.IllegalArgumentException: authorizationGrantType cannot be null
我使用的是 Spring Boot 提供的默认配置,只是在项目中添加了一些基本依赖项,例如 spring-security-config
、spring-security-oauth2-client
、spring-security-oauth2-jsoe
等
更新:
我找到了原因,对于自定义 OAuth2 提供商,例如 Gitlab,我必须添加授权类型、 redirectUritemplate、scope、clientName等,但OpenID Connect规范有一个配置端点协议(protocol),例如:https://gitlab.com/.well-known/openid-configuration ,是否可以让 Spring Security 自动读取此信息?
更新(5/15/2021):在最新的 Spring Security 5.4 和 Spring Boot 2.4 中,OpenId 配置(.well-known/openid-configuration
)默认情况下会发现,对于大多数 oauth2/oidc 授权服务器,配置一个
issuer_uri
就足够了。
最佳答案
如果使用SpringBoot v2.2.1 RELEASE,请使用redirect-uri而不是redirect-uri-template
关于spring - 在 Spring Security 5 OAuth 客户端和 Spring Boot 2.0 中,authorizationGrantType 不能为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49315552/