Spring Security oauth2客户端-Twitter问题

时间:2019-10-14 16:23:15

标签: java spring-boot twitter-oauth spring-security-oauth2

我想将Twitter oAuth2添加到我的应用程序中。之前,我成功添加了Facebook和google-我不必添加提供程序。 当我尝试将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.

这是我的配置:

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

我添加了客户端ID,所以问题出在哪里。并且我希望我正确地将oauth url添加到配置中。

@更新 我发现了问题:)错字在这里:

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

@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

在我致电:http://127.0.0.1:8080/oauth2/authorization/twitter之后,我看到了: enter image description here

2 个答案:

答案 0 :(得分:0)

  • 您的问题是有关在Twitter上使用 oauth2客户端的,这是不可能的。 Twitter不支持涉及用户的Oauth 2.0流。

  • 仅支持应用程序 OAuth 2.0承载令牌

    OAuth 2.0承载令牌是用于通过Twitter API进行身份验证的仅应用程序身份验证方法。由于此方法特定于应用程序,因此它不涉及任何用户

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

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

  • 如下图所示,Oauth 1.0a流中,您的应用程序首先需要与Authorization Server对话以获取请求令牌,并将用户重定向到授权服务器时传递该令牌。这是令牌,twitter抱怨缺少它,因为它是Oauth 1.0a。也就是说,您正在执行步骤B,而没有执行步骤A。

    enter image description here

图表参考

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

答案 1 :(得分:0)

尝试将grant_type更改为client_credentials和/或将oauth令牌URL更改为https://api.twitter.com/oauth2/token

此外,您还可以访问以下一些站点,以通过Spring的社交提供程序api与Twitter进行交互。

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

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