Spring boot --- Spring Oauth(三)
本节将学习 spring security oauth 实现单点登录
概述
首先我们来了解什么是单点登录。看下面两张图就明白了。


很明显,单点登录最重要解决的就是登录和注销的功能,今天的例子,可以用来这样的界面来验证我们实现的单点登录是否成功。

一个是是否登录后可以跳到子服务器,另一个是退出登录,是否需要重新登录才可以再次访问页面。
代码实现
以下并不是完整的demo 代码,而是编写过程中需要注意注意的地方
单点登录的客户端需要添加@EnableOauthSso的注解 :
@EnableOAuth2Sso
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class UiSecurityConfig extends WebSecurityConfigurerAdapter { @Override
public void configure(HttpSecurity http) throws Exception {
http.antMatcher("/**")
.authorizeRequests()
.antMatchers("/", "/login**","/logout","/static**").permitAll()
.anyRequest()
.authenticated().and()
.logout().logoutUrl("/logout").logoutSuccessUrl("http://localhost:8080/logout")
.and().csrf().disable().cors();
} }
从上面的配置,我们也看到了指定了登录和注销的 url ,我们看到注销的地址是跨域的,所以要注销地址的服务器需要设置 CORS
认证服务器端
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
.... @Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("http://localhost:8081"));
configuration.setAllowedMethods(Arrays.asList("GET","POST"));
configuration.setAllowedHeaders(Arrays.asList("x-requested-with"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
} @Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/oauth/**","/login/**", "/client/exit","/actuator**").permitAll()
.anyRequest().authenticated() // 其他地址的访问均需验证权限
.and()
.logout().deleteCookies("remove").invalidateHttpSession(false)
.and()
.formLogin()
.loginPage("/login").and().csrf().disable().cors();
}
}
完整代码详见 :代码
总结
逻辑实现过程中注意看项目输出的log日志,方便理解整个流程,再一个需要看文档
参考资料
- https://www.cnblogs.com/cjsblog/p/10548022.html
- https://www.cnblogs.com/Benjious/p/10588466.html
- https://docs.spring.io/spring-security/site/docs/5.2.0.BUILD-SNAPSHOT/reference/htmlsingle/#cors
Spring boot --- Spring Oauth(三)的更多相关文章
- spring boot 笔记--第三章
spring boot 笔记 第三章,使用Spring boot 构建系统: 强烈建议支持依赖管理的构建系统,Maven或Gradle 依赖管理: Spring Boot的每版本都会提供它支持的依赖列 ...
- Spring Boot 项目学习 (三) Spring Boot + Redis 搭建
0 引言 本文主要介绍 Spring Boot 中 Redis 的配置和基本使用. 1 配置 Redis 1. 修改pom.xml,添加Redis依赖 <!-- Spring Boot Redi ...
- Spring Boot 2.X(三):使用 Spring MVC + MyBatis + Thymeleaf 开发 web 应用
前言 Spring MVC 是构建在 Servlet API 上的原生框架,并从一开始就包含在 Spring 框架中.本文主要通过简述 Spring MVC 的架构及分析,并用 Spring Boot ...
- 使用Spring Boot和OAuth构建安全的SPA
最近一段时间都在闭关学习,过程还是有点艰辛的,幸运的是还有优锐课老师带着,少走了很多弯路.很久也没有更新文章了,这篇想和大家分享的是,了解如何在使用Spring Boot入门程序的同时使用Spring ...
- spring Boot+spring Cloud实现微服务详细教程第二篇
上一篇文章已经说明了一下,关于spring boot创建maven项目的简单步骤,相信很多熟悉Maven+Eclipse作为开发常用工具的朋友们都一目了然,这篇文章主要讲解一下,构建spring bo ...
- spring Boot+spring Cloud实现微服务详细教程第一篇
前些天项目组的大佬跟我聊,说项目组想从之前的架构上剥离出来公用的模块做微服务的开发,恰好去年的5/6月份在上家公司学习了国内开源的dubbo+zookeeper实现的微服务的架构.自己平时对微服务的设 ...
- [权限管理系统(四)]-spring boot +spring security短信认证+redis整合
[权限管理系统]spring boot +spring security短信认证+redis整合 现在主流的登录方式主要有 3 种:账号密码登录.短信验证码登录和第三方授权登录,前面一节Sprin ...
- 基于Spring Boot+Spring Security+JWT+Vue前后端分离的开源项目
一.前言 最近整合Spring Boot+Spring Security+JWT+Vue 完成了一套前后端分离的基础项目,这里把它开源出来分享给有需要的小伙伴们 功能很简单,单点登录,前后端动态权限配 ...
- 新书上线:《Spring Boot+Spring Cloud+Vue+Element项目实战:手把手教你开发权限管理系统》,欢迎大家买回去垫椅子垫桌脚
新书上线 大家好,笔者的新书<Spring Boot+Spring Cloud+Vue+Element项目实战:手把手教你开发权限管理系统>已上线,此书内容充实.材质优良,乃家中必备垫桌脚 ...
- Java面试题(Spring Boot/Spring Cloud篇)
Spring Boot/Spring Cloud 104.什么是 spring boot? SpringBoot是一个框架,一种全新的编程规范,他的产生简化了框架的使用,所谓简化是指简化了Spring ...
随机推荐
- 基于RBAC设计的通用权限管理框架
RoadFlow拥有基于RBAC设计的通用权限管理框架.不仅可以基于角色组进行菜单授权,还可以根据组织架构中部门.岗位.人员等进行细分的权限管理分配. 如果一个人有重复菜单权限,则将自动合并. 系统资 ...
- sqlserver插入之字符串+数字
declare @i int,@a varchar(10)set @i = 0set @a='hiro--'+LTRIM(@i)while @i < 500begin insert into h ...
- C#多线程编程系列(三)- 线程同步
目录 1.1 简介 1.2 执行基本原子操作 1.3 使用Mutex类 1.4 使用SemaphoreSlim类 1.5 使用AutoResetEvent类 1.6 使用ManualResetEven ...
- day 59 pymysql
PyMySQL介绍 PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb. PYmysql安装 pip install pymys ...
- Day 9 函数的初识1
def my_len(): l1 = [1,2,3,5,6] print(111) print(222) return print(333)print(my_len()) 一.函数的定义1.遇到ret ...
- Android-------------fragment的学习
一.fragment的静态使用 1.使用步骤: a.继承fragment 重写onCreatevVew()的回调方法 b.设置Fragment的布局 c.在Activity中声明Fragme ...
- class字节码结构(零:补充:class结构,常量池,字节码指令)
JVM高级特性与实践(五):实例探究Class类文件 及 常量池 JVM高级特性与实践(六):Class类文件的结构(访问标志,索引.字段表.方法表.属性表集合) JVM高级特性与实践(七):九大类字 ...
- jmeter-server中启动后端口总是不断在变化
1.首先找到这个文件打开: 2.修改两个地方如图: 第一个:server_port=xxxx 第二个:server.rmi.localport=xxxx 3.重启jmeter-server,这是在li ...
- error: failed to push some refs to 'https://github.com/username/python.git'
解决error: failed to push some refs to 'https://github.com/bluepen/python.git' 当我们在使用git工具上传我们自己的代码时,可 ...
- Mac下的paths.d目录神奇用法
首先,这个方法是通过PG的做法学到的,且这个方法只能在Mac下用,在Linux下还真没有这个方法. 这个paths.d的作用很简单,就是在里面创建一个文件,然后写上需要在全局命令行下用到的命令,直接配 ...