Spring 启动 Google Oauth2 所有请求 return a 401 Unauthorized

Spring Boot Google Oauth2 all requests return a 401 Unauthorized

我有一个非常简单的 Spring 启动应用程序,我希望 所有 页面通过 Google Oauth2 进行身份验证。我跟着Spring Oauth2 tuotrial and looked at the code under the /simple implementation。 (我的 application.yml 文件是为 Google 而不是 FB 设置的)

对我的应用程序的任何请求 returns 401 未经授权的响应,然后转到 localhost:8080/login...(Spring 安全自动生成的登录页面,设置为Google 开发者控制台中的重定向 URI)

我已经查看了所有其他试图回答此问题的问题,但 none 对我有所帮助。

我的申请class:

@SpringBootApplication
@EnableOAuth2Sso
@RestController
public class ControlApplication  extends WebSecurityConfigurerAdapter {

    public static void main(String[] args) {
        SpringApplication.run(ControlApplication.class, args);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.antMatcher("/**").authorizeRequests().anyRequest().authenticated().and().formLogin().defaultSuccessUrl("/", false);
    }

}

还有我的application.yml:

security:
  oauth2:
    client:
      clientId: [MyClientID]
      clientSecret: [MyClientSecret]
      accessTokenUri: https://accounts.google.com/o/auth2/token
      userAuthorizationUri: https://accounts.google.com/o/oauth2/auth?hd=xyz.com
      redirectUri: http://localhost:8080
      clientAuthenticationScheme: form
      tokenName: oauth_token
      authenticationScheme: query
      scope:
        - email
        - profile
    resource:
      userInfoUri: https://www.googleapis.com/oauth2/v3/userinfo
      preferTokenInfo: true

已解决。 (终于!)问题似乎是调用 Google Auth API 时 acessTokenUri 的配置错误。工作配置:

  accessTokenUri: https://www.googleapis.com/oauth2/v4/token
  userAuthorizationUri: https://accounts.google.com/o/oauth2/v2/auth?hd=xyz.com
  clientAuthenticationScheme: form
  scope:
    - openid
    - email
    - profile
resource:
  userInfoUri: https://www.googleapis.com/oauth2/v3/userinfo
  preferTokenInfo: true