使用linkedin的Spring OAuth登录

Posted

技术标签:

【中文标题】使用linkedin的Spring OAuth登录【英文标题】:Spring OAuth login with linkedin 【发布时间】:2019-11-19 18:50:57 【问题描述】:

我想在我的项目中实现使用linkedin 登录。我尝试实现 github 并且它可以工作,但是使用linkedin 我的代码无法编译。

我使用https://developer.okta.com/blog/2019/05/15/spring-boot-login-options#spring-boot-oauth-20-login-using-github-and-single-sign-on 这个实现了我的登录系统。

我创建了 /securePage,当我想访问此页面时,它会将我重定向到 github 登录并授权应用程序,然后它重定向到我的 localhost:8080/securedPage。但是当我想用linkedin做这个时它没有用。它告诉我为linkedin 添加提供程序ID(原因:java.lang.IllegalStateException:必须为客户端注册'linkedin'指定提供程序ID)。当我添加它时,它告诉我: 引起:java.lang.IllegalStateException:未知的提供者 ID 'linkedin'。

我的 properties.yml 文件:

spring:
    security:
        oauth2:
            client:
                registration:
                    linkedin:
                        provider: linkedin
                        client-id: MY_CLIENT_ID
                        client-secret: MY_CLIENT_SECRET
                        user-authorization-uri: https://www.linkedin.com/oauth/v2/authorization
                        access-token-uri: https://www.linkedin.com/oauth/v2/accessToken
#                    github:
#                        client-id: MY_CLIENT_ID
#                        client-secret: MY_CLIENT_SECRET

这就是我在 application.yml 添加 'provider:linkedin' 时得到的结果

Caused by: java.lang.IllegalStateException: Unknown provider ID 'linkedin'
    at org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientPropertiesRegistrationAdapter.getBuilder(OAuth2ClientPropertiesRegistrationAdapter.java:95) ~[spring-boot-autoconfigure-2.1.6.RELEASE.jar:2.0.1.RELEASE]
    at org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientPropertiesRegistrationAdapter.getClientRegistration(OAuth2ClientPropertiesRegistrationAdapter.java:61) ~[spring-boot-autoconfigure-2.1.6.RELEASE.jar:2.0.1.RELEASE]
    at org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientPropertiesRegistrationAdapter.lambda$getClientRegistrations$0(OAuth2ClientPropertiesRegistrationAdapter.java:53) ~[spring-boot-autoconfigure-2.1.6.RELEASE.jar:2.0.1.RELEASE]
    at java.util.HashMap.forEach(HashMap.java:1289) ~[na:1.8.0_201]
    at org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientPropertiesRegistrationAdapter.getClientRegistrations(OAuth2ClientPropertiesRegistrationAdapter.java:52) ~[spring-boot-autoconfigure-2.1.6.RELEASE.jar:2.0.1.RELEASE]
    at org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientRegistrationRepositoryConfiguration.clientRegistrationRepository(OAuth2ClientRegistrationRepositoryConfiguration.java:55) ~[spring-boot-autoconfigure-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientRegistrationRepositoryConfiguration$$EnhancerBySpringCGLIB$$cfaed288.CGLIB$clientRegistrationRepository$0(<generated>) ~[spring-boot-autoconfigure-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientRegistrationRepositoryConfiguration$$EnhancerBySpringCGLIB$$cfaed288$$FastClassBySpringCGLIB$$673de55c.invoke(<generated>) ~[spring-boot-autoconfigure-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientRegistrationRepositoryConfiguration$$EnhancerBySpringCGLIB$$cfaed288.clientRegistrationRepository(<generated>) ~[spring-boot-autoconfigure-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_201]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_201]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_201]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_201]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    ... 118 common frames omitted

这是我没有添加'provider:linkedin'的时候

Caused by: java.lang.IllegalStateException: Provider ID must be specified for client registration 'linkedin'
    at org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientPropertiesRegistrationAdapter.getBuilder(OAuth2ClientPropertiesRegistrationAdapter.java:95) ~[spring-boot-autoconfigure-2.1.6.RELEASE.jar:2.0.1.RELEASE]
    at org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientPropertiesRegistrationAdapter.getClientRegistration(OAuth2ClientPropertiesRegistrationAdapter.java:61) ~[spring-boot-autoconfigure-2.1.6.RELEASE.jar:2.0.1.RELEASE]
    at org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientPropertiesRegistrationAdapter.lambda$getClientRegistrations$0(OAuth2ClientPropertiesRegistrationAdapter.java:53) ~[spring-boot-autoconfigure-2.1.6.RELEASE.jar:2.0.1.RELEASE]
    at java.util.HashMap.forEach(HashMap.java:1289) ~[na:1.8.0_201]
    at org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientPropertiesRegistrationAdapter.getClientRegistrations(OAuth2ClientPropertiesRegistrationAdapter.java:52) ~[spring-boot-autoconfigure-2.1.6.RELEASE.jar:2.0.1.RELEASE]
    at org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientRegistrationRepositoryConfiguration.clientRegistrationRepository(OAuth2ClientRegistrationRepositoryConfiguration.java:55) ~[spring-boot-autoconfigure-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientRegistrationRepositoryConfiguration$$EnhancerBySpringCGLIB$$6ad24949.CGLIB$clientRegistrationRepository$0(<generated>) ~[spring-boot-autoconfigure-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientRegistrationRepositoryConfiguration$$EnhancerBySpringCGLIB$$6ad24949$$FastClassBySpringCGLIB$$1e63fd0a.invoke(<generated>) ~[spring-boot-autoconfigure-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientRegistrationRepositoryConfiguration$$EnhancerBySpringCGLIB$$6ad24949.clientRegistrationRepository(<generated>) ~[spring-boot-autoconfigure-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_201]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_201]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_201]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_201]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    ... 148 common frames omitted

谁能帮帮我。

【问题讨论】:

【参考方案1】:

假设您使用的是最新版本的 spring-security。看起来是配置问题。 我认为该文件应如下所示:

spring:  
  ...
  security:  
    oauth2:  
      client:  
        registration:  
          github:  
            client-id: << Client ID >>  
            client-secret: << Secret >>  
          linkedin:  
            client-id: << Client ID >>  
            client-secret: <<  Secret >> 
            ...

        provider:  
          linkedin:  
            << provider related configuration values >>

在 Spring 的 github 上查看此问题:https://github.com/spring-projects/spring-security/issues/5983

然后您也可以查看此示例(问题线程中的链接):https://github.com/jgrandja/oauth2login-demo/tree/linkedin

希望对你有帮助!

【讨论】:

是的,我正在使用最新版本的 spring-security。我在该示例的 application.yml 中做了同样的事情,并且它有效。谢谢

以上是关于使用linkedin的Spring OAuth登录的主要内容,如果未能解决你的问题,请参考以下文章

如何让 Linkedin OAuth 在 Spring Boot 中工作

适用于 ios OAuth2、API V2 的 LinkedIn 登录。如何打开已安装的 LinkedIn 应用程序

Linkedin Oauth Javascript 授权“哦哦!”

LinkedIn OAuth2 /oauth/v2/accessToken 响应未返回“token_type”

Spring Boot/Angular整合Keycloak实现单点登录

Android中LinkedIn的Oauth 2.0授权