- Spring Boot 教程
- Spring Boot - 首页
- Spring Boot - 简介
- Spring Boot - 快速入门
- Spring Boot - 启动
- Spring Tool Suite
- Spring Boot - Tomcat 部署
- Spring Boot - 构建系统
- Spring Boot - 代码结构
- Spring Bean & 依赖注入
- Spring Boot - 运行器
- Spring Boot - 启动器
- Spring Boot - 应用属性
- Spring Boot - 配置
- Spring Boot - 注解
- Spring Boot - 日志
- 构建 RESTful Web 服务
- Spring Boot - 异常处理
- Spring Boot - 拦截器
- Spring Boot - Servlet 过滤器
- Spring Boot - Tomcat 端口号
- Spring Boot - Rest 模板
- Spring Boot - 文件处理
- Spring Boot - 服务组件
- Spring Boot - Thymeleaf
- 使用 RESTful Web 服务
- Spring Boot - CORS 支持
- Spring Boot - 国际化
- Spring Boot - 定时任务
- Spring Boot - 启用 HTTPS
- Spring Boot - Eureka 服务器
- 使用 Eureka 注册服务
- 网关代理服务器和路由
- Spring Cloud 配置服务器
- Spring Cloud 配置客户端
- Spring Boot - Actuator
- Spring Boot - Admin 服务器
- Spring Boot - Admin 客户端
- Spring Boot - 启用 Swagger2
- Spring Boot - 使用 SpringDoc OpenAPI
- Spring Boot - 创建 Docker 镜像
- 追踪微服务日志
- Spring Boot - Flyway 数据库
- Spring Boot - 发送邮件
- Spring Boot - Hystrix
- Spring Boot - Web Socket
- Spring Boot - 批处理服务
- Spring Boot - Apache Kafka
- Spring Boot - Twilio
- Spring Boot - 单元测试用例
- Rest Controller 单元测试
- Spring Boot - 数据库处理
- 保护 Web 应用
- Spring Boot - 使用 JWT 的 OAuth2
- Spring Boot - Google Cloud Platform
- Spring Boot - Google OAuth2 登录
- Spring Boot 资源
- Spring Boot - 快速指南
- Spring Boot - 有用资源
- Spring Boot - 讨论
Spring Boot - Google OAuth2 登录
本章我们将学习如何使用 Gradle 构建的 Spring Boot 应用添加 Google OAuth2 登录。
首先,在你的构建配置文件中添加 Spring Boot OAuth2 安全依赖项,如下所示。
buildscript {
ext {
springBootVersion = '3.3.4'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
group = 'com.tutorialspoint.projects'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 21
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter')
testCompile('org.springframework.boot:spring-boot-starter-test')
compile('org.springframework.security.oauth:spring-security-oauth2-client')
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
现在,添加 HTTP 端点,以便在 Spring Boot 主应用程序类文件中通过 Spring Boot 身份验证后从 Google 读取用户主体,如下所示:
package com.tutorialspoint.projects.googleservice;
import java.security.Principal;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class GoogleserviceApplication {
public static void main(String[] args) {
SpringApplication.run(GoogleserviceApplication.class, args);
}
@GetMapping(value = "/user")
public Principal user(Principal principal) {
return principal;
}
}
现在,编写一个配置文件来启用 Web 安全的 OAuth2SSO,并删除 index.html 文件的认证,如下所示:
package com.tutorialspoint.projects.googleservice;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {
@Bean
protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(
request -> request
.requestMatchers("/").permitAll()
.requestMatchers("/home").permitAll()
.anyRequest().authenticated()
)
.formLogin(form -> form.loginPage("/login")
.permitAll())
.logout(config -> config
.logoutSuccessUrl("/")
.permitAll())
.oauth2Client(Customizer.withDefaults())
.oauth2Login(Customizer.withDefaults())
.build();
}
}
接下来,在静态资源下添加 index.html 文件,并添加链接以重定向到用户 HTTP 端点以读取 Google 用户主体,如下所示:
<!DOCTYPE html>
<html>
<head>
<meta charset = "ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href = "user">Click here to Google Login</a>
</body>
</html>
注意 - 在 Google Cloud 控制台中,启用 Gmail 服务、分析服务和 Google+ 服务 API。
然后,转到“凭据”部分并创建一个凭据,然后选择 OAuth 客户端 ID。
接下来,在 OAuth2 同意屏幕中提供产品名称。
接下来,选择“Web 应用程序”作为应用程序类型,提供授权的 JavaScript 源和授权的重定向 URI。
现在,你的 OAuth2 客户端 ID 和客户端密钥已创建。
接下来,在你的应用程序属性文件中添加客户端 ID 和客户端密钥。
spring.security.oauth2.client.registration.google.clientId = <CLIENT_ID> spring.security.oauth2.client.registration.google.client-secret= <CLIENT_SECRET> spring.security.oauth2.client.registration.google.authorization-grant-type=client_credentials
现在,你可以创建一个可执行的 JAR 文件,并使用以下 Gradle 命令运行 Spring Boot 应用程序。
对于 Gradle,你可以使用如下所示的命令:
gradle clean build
“BUILD SUCCESSFUL”之后,你可以在 build/libs 目录下找到 JAR 文件。
使用命令 java –jar <JARFILE> 运行 JAR 文件,应用程序将在 Tomcat 端口 8080 上启动。
现在访问 URL https://:8080/ 并单击 Google 登录链接。
它将重定向到 Google 登录屏幕,并提供 Gmail 登录详细信息。
如果登录成功,我们将收到 Gmail 用户的主体对象。