【问题标题】:Spring Security oauth2 client - problem with TwitterSpring Security oauth2 客户端 - Twitter 的问题
【发布时间】:2020-02-11 07:22:08
【问题描述】:

我想将 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

@更新 现在我有另一个问题,这是我的配置:

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 我看到了这个:

【问题讨论】:

  • Spring Security OAuth Client 基于 Provider 的 OpenID Connect 支持。我怀疑 Twitter 是否支持 OpenID Connect 规范。否则,除了 Google、GitHub、FB 和 Okta 之外,SpringSecurity 人员还会为 Twitter 提供默认配置。
  • @JuniorWithEverything,你可以试试这个令牌 uri 吗? https://api.twitter.com/oauth2/token

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


【解决方案1】:
  • 您的问题是关于在 Twitter 上使用 oauth2 客户端,这是不可能的。 Twitter 不支持涉及用户的 Oauth 2.0 流。

  • 仅支持应用程序OAuth 2.0 Bearer Token

    OAuth 2.0 Bearer Token 是使用 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 流程中,您的应用程序首先需要与授权服务器通信以获取请求令牌,并在将用户重定向到授权服务器时传递该令牌。这是令牌,因为它是 Oauth 1.0a,所以 twitter 抱怨丢失了。即您正在执行步骤 B 而没有步骤 A。

图表参考

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

【讨论】:

    【解决方案2】:

    尝试将 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

    【讨论】:

      猜你喜欢
      • 2012-10-22
      • 2013-06-20
      • 2019-04-06
      • 2017-09-17
      • 2021-03-07
      • 1970-01-01
      • 2019-07-20
      • 2015-11-05
      • 2022-11-08
      相关资源
      最近更新 更多