简体   繁体   English

使用Olinkedin进行Spring OAuth登录

[英]Spring OAuth login with linkedin

I want to implement login with linkedin to my project. 我想使用我的项目的linkedin实现登录。 I tried implementing github and it work but with linkedin my code doesn't compile. 我尝试实现github,但它可以正常工作,但是无法使用我的代码进行编译。

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

I created /securePage and when I wanted to go to this page it redirected me to github login and authorize app, after that it redirected to me localhost:8080/securedPage. 我创建了/ securePage,当我想转到此页面时,它将我重定向到github登录并授权应用程序,之后将其重定向到我localhost:8080 / securedPage。 But when I wanted to do this with linkedin it didn't work. 但是,当我想使用linkedin进行操作时,它不起作用。 It told me to add provider id for linkedin (Caused by: java.lang.IllegalStateException: Provider ID must be specified for client registration 'linkedin'). 它告诉我添加linkedin的提供者ID(原因:java.lang.IllegalStateException:必须为客户端注册“ linkedin”指定提供者ID)。 When I add it, then it told me this: Caused by: java.lang.IllegalStateException: Unknown provider ID 'linkedin'. 当我添加它时,它告诉我:原因:java.lang.IllegalStateException:未知的提供程序ID“ linkedin”。

My properties.yml file: 我的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

This is what it gives me when I add 'provider: linkedin' to application.yml 这就是我在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

And this is when I didn't add 'provider: linkedin' 这是我没有添加'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

Can anyone help me with this please. 谁能帮我这个忙。

Assuming your are using the latest version of spring-security. 假设您使用的是最新版本的spring-security。 It looks like a configuration issue. 看起来像是配置问题。
I think the file should look like : 我认为该文件应如下所示:

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 >>

See this issue on Spring's github : https://github.com/spring-projects/spring-security/issues/5983 在Spring的github上看到这个问题: https : //github.com/spring-projects/spring-security/issues/5983

Then you can also check this sample (link in the issue thread) : https://github.com/jgrandja/oauth2login-demo/tree/linkedin 然后,您还可以检查此示例(问题线程中的链接): https : //github.com/jgrandja/oauth2login-demo/tree/linkedin

Hope it helps ! 希望能帮助到你 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM