其他分享
首页 > 其他分享> > 在Spring Security 5 OAuth Client和Spring Boot 2.0中,authorizationGrantType不能为null

在Spring Security 5 OAuth Client和Spring Boot 2.0中,authorizationGrantType不能为null

作者:互联网

我按照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,我必须添加grant类型,redirectUritemplate,scope,clientName等,但OpenID Connect规范有一个配置端点协议,例如:https://gitlab.com/.well-known/openid-configuration,是否有可能使Spring成为可能安全性自动读取这些信息?

解决方法:

要详细说明上面的OP更新,您需要在application.yaml中包含以解决原始错误的属性如下所示,在本例中为Azure AD(请注意,这仅适用于Spring Security 5,而不是Spring Security OAuth2 2. x的功能是being merged直接进入Spring Security 5):

spring:
  security:
    oauth2:
      client:
        registration:
          microsoft:
            client-id: a935ba7b-6aa4-4b0c-9e84-04f9acaa477b
            client-secret: redacted
            authorization-grant-type: authorization_code
            redirect-uri-template: '{baseUrl}/login/oauth2/code/{registrationId}'
            scope: User.Read
            client-name: Microsoft
            client-alias: microsoft
        provider:
          microsoft:
            authorization-uri: https://login.microsoftonline.com/common/oauth2/authorize?resource=https://graph.microsoft.com/
            token-uri: https://login.microsoftonline.com/common/oauth2/token
            user-info-uri: https://graph.microsoft.com/v1.0/me
            user-name-attribute: sub
            jwk-set-uri: https://login.microsoftonline.com/common/discovery/keys

标签:spring,oauth-2-0,spring-security,spring-boot-2,openid-connect
来源: https://codeday.me/bug/20190527/1162404.html