使用Spring Boot来实现Google登录认证

有位热心的人已经在下面的文章中总结了Spring Boot(Spring Social)的登录协作。

使用Spring Social和Spring Boot实现SNS登录

以下是我在尝试使用Google登录认证时遇到困难的备忘录。

获取OAuth认证信息并设置属性

    • spring.social.google.app-id: Developer Console の「API とサービス」で取得した OAuth クライアント ID

 

    spring.social.google.app-secret: 上記で取得したクライアントシークレット

此外,我们将”已经批准的重定向URI”设置为”http://localhost:8080/auth/google”。

属性通常可以在application.yml等文件中指定,但是由于可能会误公开的问题,(我自己)选择在STS的执行设置中指定。

我会使用 Spring Social Google 的社区版吗?

在Gradle等依赖关系中,需要指定com.github.spring-social组的内容。
对于Gradle:

    // https://mvnrepository.com/artifact/com.github.spring-social/spring-social-google
    compile group: 'com.github.spring-social', name: 'spring-social-google', version: '1.1.3'
    // ログイン連携に必要
    // https://mvnrepository.com/artifact/org.springframework.social/spring-social-security
    compile group: 'org.springframework.social', name: 'spring-social-security' // , version: '1.1.4.RELEASE'

Spring Social 本家的 spring-social-google (v.1.0.0.RELEASE) 已经过时,不适用于当前的 Spring 版本。1

导入 GoogleAutoConfiguration。

@SpringBootApplication
@Import(GoogleAutoConfiguration.class)
public class SpringSocialSboxApplication {

在Spring Boot中,spring-social-google似乎是外部资源,不会被默认自动配置。
这是当前最令人困惑的地方。

[错误] 在STS3.9.0中,会在VM参数的末尾插入“-noverify”。

作为工作记录,即使与该主题没有关系。

如果在STS3.9.0中以”Spring Boot App”的形式启动,”VM arguments”的末尾将会添加”-noverify”,而不加空格。

因此,如果没有在末尾添加”-noverify -Ddummy=”等内容,属性将无法正确设置。

最初の説明では、appSecretが無効であり、Googleにブロックされているため、私はしばらく困惑しました。今回は、二番目のトライで問題が発生しました。

示例代码

如果启用 @EnableGoogle,就会抛出 ClassNotFoundError 导致程序崩溃。
bannerAds