简体   繁体   English

Spring 安全 oauth2 客户端 - Twitter 的问题

[英]Spring Security oauth2 client - problem with Twitter

I want to add Twitter oAuth2 to my application.我想将 Twitter oAuth2 添加到我的应用程序中。 Earlier I added Facebook and google with success - I didn't have to add provider.早些时候我添加了 Facebook 和谷歌成功 - 我不必添加提供者。 When i try to add twitter data to application.properties file and run server i get error:当我尝试将 twitter 数据添加到application.properties文件并运行服务器时,出现错误:

Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. Message: Error creating bean with name 'securityConfig': Unsatisfied dependency expressed through method 'setContentNegotationStrategy' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration': Unsatisfied dependency expressed through method 'setConfigurers' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.OAuth2ClientConfiguration$OAuth2ClientWebMvcSecurityConfiguration': Unsatisfied dependency expressed through method 'setClientRegistrationRepository' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientRegistrationRepositoryConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.security.oauth2.client-org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Client id must not be empty.

This is my configuration:这是我的配置:

spring.security.oauth2.client.registration.facebook.clientId=<SECRET>
spring.security.oauth2.client.registration.facebook.clientSecret=<SECRET>
spring.security.oauth2.client.registration.facebook.redirect-uri=http://localhost:8080/oauth2/callback/facebook
spring.security.oauth2.client.registration.facebook.scope=public_profile email


spring.security.oauth2.client.registration.twitter.clientId=<SECRET>
spring.security.oauth2.client.registration.twitter.clientSecret=<SECRET>
spring.security.oauth2.client.registration.twitter.redirect-uri=http://localhost:8080/oauth2/callback/twitter
spring.security.oauth2.client.registration.twitter.provider=twitter
spring.security.oauth2.client.registration.twitter.authorization-grant-type=token
spring.security.oauth2.client.provider.twitter.token-uri=https://api.twitter.com/oauth/token
spring.security.oauth2.client.provider.twitter.authorization-uri=https://api.twitter.com/oauth/authorize
spring.security.oauth2.client.provider.twitter.user-info-uri=https://api.twitter.com/oauth/request_token

I add client ID so where is problem.我添加了客户 ID,所以问题出在哪里。 And I hope I correct add oauth urls to configuration.我希望我正确地将 oauth url 添加到配置中。

@Update I found problem:) Typo in here: @Update 我发现了问题:) 这里的错字:

spring.security.oauth2.client.registration.twiter.authorization-grant-type=token

@UPDATE Now i have another problem, this is my configuration: @UPDATE 现在我有另一个问题,这是我的配置:

spring.security.oauth2.client.registration.twitter.client-id=<SECRET>
spring.security.oauth2.client.registration.twitter.clientSecret=<SECRET>
spring.security.oauth2.client.registration.twitter.redirect-uri=http://localhost:8080/oauth2/callback/twitter
spring.security.oauth2.client.registration.twitter.authorization-grant-type=authorization_code
spring.security.oauth2.client.provider.twitter.token-uri=https://api.twitter.com/oauth/access_token
spring.security.oauth2.client.provider.twitter.authorization-uri=https://api.twitter.com/oauth/authorize

And after i call: http://127.0.0.1:8080/oauth2/authorization/twitter i see this:在我打电话后: http://127.0.0.1:8080/oauth2/authorization/twitter我看到了这个: 在此处输入图像描述

  • Your question is about using oauth2 client with Twitter and is not possible.您的问题是关于将oauth2 客户端与 Twitter 一起使用,这是不可能的。 Twitter does not support Oauth 2.0 flows involving user. Twitter 不支持涉及用户的 Oauth 2.0 流。

  • It only supports the application only OAuth 2.0 Bearer Token :它仅支持应用程序OAuth 2.0 Bearer Token

    OAuth 2.0 Bearer Token is the application-only authentication method for authenticating with the Twitter API. OAuth 2.0 Bearer Token是仅应用程序的身份验证方法,用于使用 Twitter API 进行身份验证。 As this method is specific to the application, it does not involve any users由于此方法特定于应用程序,因此不涉及任何用户

    https://developer.twitter.com/en/docs/basics/authentication/oauth-2-0 https://developer.twitter.com/en/docs/basics/authentication/oauth-2-0

  • For flows involving end user, it uses Oauth 1.0a https://developer.twitter.com/en/docs/basics/authentication/oauth-1-0a对于涉及最终用户的流程,它使用 Oauth 1.0a https://developer.twitter.com/en/docs/basics/authentication/

  • As you can see in the diagram below, Oauth 1.0a flow, your application first needs to talk to Authorisation Server to get a request token and pass that token when redirecting the user to authorisation server.如下图所示,Oauth 1.0a 流程中,您的应用程序首先需要与授权服务器对话以获取请求令牌并在将用户重定向到授权服务器时传递该令牌。 And it is the token, that twitter is complaining as missing because it is Oauth 1.0a.这就是令牌,twitter 抱怨缺失,因为它是 Oauth 1.0a。 Ie you are doing step B without step A.即您正在执行步骤 B 而没有步骤 A。

    在此处输入图像描述

Diagram Reference图表参考

https://oauth.net/core/1.0/ https://oauth.net/core/1.0/

Try to change the grant_type as client_credentials and/or changing the oauth token url to https://api.twitter.com/oauth2/token . Try to change the grant_type as client_credentials and/or changing the oauth token url to https://api.twitter.com/oauth2/token .

Additionally, there are some of the below sites which you can go through to interact with Twitter via Spring's social provider api.此外,您可以通过以下一些站点 go 通过 Spring 的社交服务提供商 api 与 Twitter 进行交互。

https://docs.spring.io/spring-social-twitter/docs/current/reference/htmlsingle/ https://docs.spring.io/spring-social-twitter/docs/current/reference/htmlsingle/

https://developer.twitter.com/en/docs/basics/authentication/api-reference/token https://developer.twitter.com/en/docs/basics/authentication/api-reference/token

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

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