使用 Spring Boot 进行 OAuth2 身份验证后无法正确重定向

Unable to properly redirect after OAuth2 authentication using Spring Boot

我正在尝试构建一个基本的 Spring 引导应用程序,它将允许我将验证用户的工作委托给外部 OAuth2 提供商,例如 Facebook。

我有两个主要问题:

  1. 身份验证过程完成后,我得到:

GET request for "https://graph.facebook.com/me" resulted in 200 (OK)

我还得到以下信息:

Reading [interface java.util.Map] as "application/json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@2a7ce52b] Delegating to org.springframework.security.web.authentication.session.ChangeSessionIdAuthenticationStrategy@4f061f45 SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.

好像解析和存储返回的凭据有问题。

  1. 有时我会收到 401 错误:

Authentication request failed: org.springframework.security.authentication.BadCredentialsException: Could not obtain access token

这是一个偶然的问题,我无法追查到问题所在。当改用 Google OAuth2 时也会发生这种情况。

我基本上使用了@EnableOAuth2Sso 注释,并使用 application.yml 文件配置了凭据:

security:
  oauth2:
    client:
      clientId: 123
      clientSecret: bla
      accessTokenUri: https://graph.facebook.com/oauth/access_token
      userAuthorizationUri: https://www.facebook.com/dialog/oauth
      tokenName: oauth_token
      authenticationScheme: query
      clientAuthenticationScheme: form
    resource:
      userInfoUri: https://graph.facebook.com/me
      preferTokenInfo: false

非常感谢任何帮助:)

最后我设法解决了这个问题,spring-boot-starter-tomcat maven 依赖项被标记为 <scope>provided</scope>,因此干扰了正确初始化嵌入式中的 spring 上下文对象的能力tomcat.