【Spring Boot】Spring Boot @EnableOAuth2Sso | 启用 OAuth2 单点登录
文章目录
- 演示工具版本
- Maven 依赖
- 使用 @EnableOAuth2Sso
- OAuth2 配置
- 登出
- 完整示例
- 输出
- 参考文献
- 源码下载
本页将介绍Spring Security
OAuth2
@EnableOAuth2Sso
注解的例子。
@EnableOAuth2Sso
注解可以启用OAuth2
单点登录(SingleSignOn,SSO
)。默认情况下,所有的路径都是需要安全的。
我们可以在Spring Security
的Java
配置中使用WebSecurityConfigurerAdapter
来定制它。我们可以使用application.properties
或application.yml
或以命令行方式配置Spring Security OAuth2
。
这里我们将使用GitHub
创建Spring Boot OAuth2
应用程序。
演示工具版本
- Java 11
- Spring 5.1.7.RELEASE
- Spring Boot 2.1.5.RELEASE
- Maven 3.5.2
Maven 依赖
找到OAuth2
的Maven
依赖。
<dependency><groupId>org.springframework.security.oauth.boot</groupId><artifactId>spring-security-oauth2-autoconfigure</artifactId><version>2.1.5.RELEASE</version>
</dependency>
在Spring Boot
应用程序中,上述依赖关系在类路径上的可用性为我们提供了自动配置OAuth2
的优势。
使用 @EnableOAuth2Sso
要在我们的应用程序中使用@EnableOAuth2Sso
,请在Spring Security
配置中用@Configuration
对其进行注释。
@Configuration
@EnableOAuth2Sso
public class SecurityConfiguration {}
现在所有的URL都是需要安全验证的。我们可以使用WebSecurityConfigurerAdapter
来自定义这一行为。假设我们想使用一些不进行安全验证的URL
,如主页和错误页面等,如下进行配置。
SecurityConfiguration.java
package com.concretepage;
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;@Configuration
@EnableOAuth2Sso
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/", "/error**").permitAll().anyRequest().authenticated().and().logout().logoutUrl("/logout").logoutSuccessUrl("/");}
}
OAuth2 配置
在Spring Boot
应用程序中,我们可以使用application.properties
或application.yml
或以命令行方式配置安全OAuth2
客户端、资源和sso
属性。
在我们的例子中,我们使用的是GitHub OAuth
。
application.yml
security:oauth2:client:clientId: <your_github_clientId>clientSecret: <your_github_clientSecret>accessTokenUri: https://github.com/login/oauth/access_tokenuserAuthorizationUri: https://github.com/login/oauth/authorizeclientAuthenticationScheme: formresource:userInfoUri: https://api.github.com/usersso:login-path: /login
你需要在上述YML
文件中输入你的GitHub
的clientId
和 clientSecret
。
clientId: 这是OAuth
客户端的ID
,OAuth
提供商通过它来识别客户端。
clientSecret: 与资源关联的客户端密钥。
要获得GitHub
的OAuth2
客户端ID
和客户端密钥,请通过该链接。
登出
要注销Spring Security
应用程序,请在Spring Security
Java
配置文件中配置注销URL
,默认为/logout
,然后创建一个表单并以POST
方式提交给注销URL
。用Thymeleaf
查找示例表单。
<form th:action="@{/logout}" method="POST"><input type="submit" value="Logout"/>
</form>
完整示例
这里我们将提供我们的演示程序的完整代码。文章中已经给出了SecurityConfiguration.java
和application.yml
这两个文件。查找其余的代码。
pom.xml
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.5.RELEASE</version><relativePath />
</parent>
<properties><context.path>spring-app</context.path><java.version>11</java.version>
</properties>
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework.security.oauth.boot</groupId><artifactId>spring-security-oauth2-autoconfigure</artifactId><version>2.1.5.RELEASE</version></dependency>
</dependencies>
AppController.java
package com.concretepage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;@Controller
public class AppController {@GetMapping("hello")public ModelAndView welcome() {ModelAndView mav = new ModelAndView();mav.setViewName("welcome");return mav;}@GetMapping("error")public ModelAndView error() {ModelAndView mav = new ModelAndView();return mav;}
}
index.html
<!doctype html>
<html>
<head><title>Spring Security</title>
</head>
<body><h3>Login with <a href="/hello">GitHub</a></h3>
</body>
</html>
welcome.html
<!doctype html>
<html lang="en">
<head><title>Welcome</title>
</head>
<body>Welcome <b th:inline="text" > [[${#httpServletRequest.remoteUser}]] </b> <br/><br/><form th:action="@{/logout}" method="POST"><input type="submit" value="Logout"/></form>
</body>
</html>
error.html
<!doctype html>
<html>
<head><title>Spring Security</title>
</head>
<body><h3>Error</h3><p thif="${param.error}">An error occurred.</p>
</body>
</html>
Main.java
package com.concretepage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class Main {public static void main(String[] args) {SpringApplication.run(Main.class, args);}
}
输出
下载该项目,并在application.yml
文件中输入你的GitHub
clientId
和clientSecret
。
然后使用命令提示符从项目的根文件夹中运行以下命令。
mvn spring-boot:run
访问网址
http://localhost:8080/
点击GitHub
链接进行登录。你将被重定向到GitHub
登录页面。登录成功后,你将被重定向到你的应用程序并看到欢迎页面。
参考文献
【1】OAuth2 Boot
【2】OAuth 2 Developers Guide
源码下载
提取码:mao4
spring-boot-enableoauth2sso.zip
【Spring Boot】Spring Boot @EnableOAuth2Sso | 启用 OAuth2 单点登录相关推荐
- oauth2 单点登录_Spring Security Oauth2和Spring Boot实现单点登录
最近在学习单点登录相关,调查了一下目前流行的单点登录解决方案:cas 和 oauth2,文章主要介绍oauth2 单点登录.希望这篇文章能帮助大家学习相关内容. 我们将使用两个单独的应用程序: 授权服 ...
- Spring Security OAuth2 单点登录和登出
文章目录 1. 单点登录 1.1 使用内存保存客户端和用户信息 1.1.1 认证中心 auth-server 1.1.2 子系统 service-1 1.1.3 测试 1.2 使用数据库保存客户端和用 ...
- Spring Security Oauth2 单点登录案例实现和执行流程剖析
我已经试过了 教程很完美 Spring Security Oauth2 OAuth是一个关于授权的开放网络标准,在全世界得到的广泛的应用,目前是2.0的版本.OAuth2在"客户端" ...
- Spring Security OAuth2 单点登录
1. 概述 在前面的文章中,我们学习了 Spring Security OAuth 的简单使用. <Spring Security OAuth2 入门> <Spring Securi ...
- spring boot 集成 Oracle Access Manager(OAM)单点登录
1.介绍 Oracle Access Manager(OAM)是oracle公司开发的身份认证和资源管理解决方案.结合WebGate和OHS可实现系统间单点登录集成. oracle中间件产品可以非常方 ...
- 爆破专栏丨Spring Security系列教程之实现CAS单点登录上篇-概述
作者:千锋一一哥 前言 从本章节开始,一一哥 会给各位讲解一个很常见也很重要的知识点,就是单点登录!现在的大型分布式项目,基本都会考虑实现单点登录,而且现在网上也有很多单点登录的实现方案.开源项目,但 ...
- java培训爆破专栏之Spring Security系列教程之实现CAS单点登录上篇-概述
作者:千锋一一哥 前言 从本章节开始,一一哥 会给各位讲解一个很常见也很重要的知识点,就是单点登录!现在的大型分布式项目,基本都会考虑实现单点登录,而且现在网上也有很多单点登录的实现方案.开源项目,但 ...
- oauth2 单点登录_六个高Star开源项目,让你更懂OAuth和单点登录
现在大部分的网络应用,登录.注册.密码加密保存.token 管理等功能都是必要的.为了让用户的隐私更能得到保障,使用起来更方便,OAuth 协议和单点登录系统也就应运而生.今天 Gitee 介绍的六款 ...
- Spring Security 框架学习之十二 单点登录原理(部分注解是我原创,其余是转载网上电子书内容,献丑了,写的不对请大家见谅,如有侵权我立即删除)(主要是为了自己学的知识备忘)
下图中的redis通常作为抽离出来的独立的外部session对象数据容器 (上面说的是:如果将共享顶级域名下的各个子系统的sessionId(会话标识信息),session对象数据信息,用户身份认 ...
最新文章
- MyBatis框架添加客户有哪些步骤
- Nova Conductor 与 Versioned Object Model 机制
- 一个手机号码剔重的问题
- Ioc的推荐实现方式
- 科学家公布地球“裸照”
- html表格转换为csv,python实现将html表格转换成CSV文件的方法
- RedisTemplate和StringRedisTemplate使用
- [Node.js] nodejs 连接 mysql数据库
- 右键菜单管理---右键管家
- Linux NGINX 主备,使用keepalived实现主备(以nginx为例)
- 微型计算机系统王其藩,王其藩 系统动力学 附录Vensim模型集
- android 四舍五入函数,巧用WPS移动版组合函数四舍五入保留两位小数
- HBuilder连接不上逍遥Android模拟器
- Django框架(一)
- 程序猿最喜欢说的30句话!看看你有没有说过
- css朗逸保险丝盒机舱,【朗逸保险盒】朗逸保险盒位置图解、拆卸方法_车主指南...
- Revit二次开发_1.过滤器笔记篇
- android 新闻功能列表,Android中通过ListView的实现简单新闻列表
- ROS Error: [rospack] Error: package ‘map_server‘ not found
- zigbee协议栈ADC采集外部电压