spring-cloud-security
说在前面
- 劳资有代码洁癖,就追求用最少的代码,最整齐的代码完成功能和达到要求的性能,原来的项目是的认证和授权是集成在单个业务模块里的,这样就导致增加一个工程就搬一次认证和授权的代码,很不爽,又不想为了认证和授权单独写一个服务。有机会接手一个全新的项目,打算把网关和认证授权集成在一个工程,但是原来的认证和授权是spring-boot-security,是基于servlet的web容器的,而spring-gateway是基于webflux的,没有servlet,好,那就搞spring-cloud-security吧,他也是基于webflux的。
- 此篇暂时只有spring-cloud-security,spring-cloud-gateway的代码后期补上,(已经补上了,spring-cloud-gateway看我的另一篇博客,整合代码连接在最后)。
- 看了网上好多的帖子,有基于 AuthenticationManager做认证的,有集成
ServerSecurityContextRepository 然后认证和授权的,有单独定义一个webFilter 来做认证和授权的,还有的认证不通过也会走到鉴权流程,总之感觉不是我想要的
- 我想要的,认证不通过返回 401,认证通过了但是鉴权不通过返回 403,认证和授权都通过了正确返回。
架构
- 前后端分离的工程,登录不用spring-cloud-security默认的表单,用restful风格的post请求,登录成功后创建认证和授权信息(代码里就是 UsernamePasswordAuthenticationToken),然后之后所有的请求都带 access_token 请求头过来进行认证和授权。
- 缓存用redis_cluster,数据库就mysql。
表结构
工程代码里的sql文件夹里,简单的写入一条用户,一个role(admin),和相关的关联关系。
先看效果
登录,成功返回token
带上登录返回的token,请求/demo,正确返回
胡乱修改token,/demo请求不能通过认证
把token恢复正确,胡乱改权限,/demo请求返回没有授权
我的思路
- 认证
package com.faith.securitybaseonwebflux.security;import com.faith.securitybaseonwebflux.security.authorization.FunpayAuthorizeExchangeSpecCustomizer;
import com.faith.securitybaseonwebflux.security.contextrepository.FunpaySecurityContextRepository;
import com.faith.securitybaseonwebflux.security.handler.FunpayAccessDeniedHandler;
import com.faith.securitybaseonwebflux.security.handler.FunpayAuthenticationEntryPointHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.server.SecurityWebFilterChain;/*** @author Leeko* @date 2022/3/30**/
@Configuration
@EnableWebFluxSecurity
public class FluxSecurityConfig {@AutowiredFunpaySecurityContextRepository funpaySecurityContextRepository;@AutowiredFunpayAccessDeniedHandler funpayAccessDeniedHandler;@AutowiredFunpayAuthenticationEntryPointHandler funpayAuthenticationEntryPointHandler;@AutowiredFunpayAuthorizeExchangeSpecCustomizer funpayAuthorizeExchangeSpecCustomizer;public static final String[] AUTH_WHITELIST = new String[]{"/login", "/logout"};@Beanpublic SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {http.csrf().disable();http.httpBasic().disable();http.securityContextRepository(funpaySecurityContextRepository);http.authorizeExchange(funpayAuthorizeExchangeSpecCustomizer).exceptionHandling().authenticationEntryPoint(funpayAuthenticationEntryPointHandler).and().exceptionHandling().accessDeniedHandler(funpayAccessDeniedHandler);return http.build();}/*** BCrypt密码编码** @return*/@Beanpublic BCryptPasswordEncoder bcryptPasswordEncoder() {return new BCryptPasswordEncoder();}}
方法 securityWebFilterChain 的最后一行 http.build(),什么也不配置的时候,点进去跟一下代码,发现他默认的过滤器:
发现第三个WebFilter在认证之前,而这个ReactorContextWebFilter 点进去看源码是修改上下文
ReactiveSecurityContextHolder的,那应该可以在这一步创建认证信息,然后也放入上下文,后来试了可以。
- 授权
我不想在配置的方法里写那么长的代码,发现在 http.build 里有属性 AuthorizaExchangeSpec,build方法里用它来配置,那就单独写一个授权指定类 FunpayAuthorizeExchangeSpecCustomizer
总之,就开始什么都不配置,跟源码进去看人家怎么配的,看哪些地方可以自定义,就试之无他。
疑问
- spring-boot-security 的时候可以在方法上 @PreAuthorize来指定权限,为什么在spring-cloud-security不这么用,我记得spring-boot-security的对方法的鉴权是基于拦截器的,而拦截器是servlet的东西,webflux没有这一套,所以。。。懒得跟代码去确定了,欢迎小伙伴指正。
代码
GitHub - faithgreen332/faith-spring-cloud-gateway-security at master
整合认证和鉴权的网关,如果不想要认证和鉴权,就把 security 文件夹删掉,去掉 pom 里的 security 依赖就行了
有大毛病
其中权限的配置:
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.stereotype.Component;/*** 握翘啊,这里是启动的时候就配置好了,不能动态配啊,想先登录,拿到用户的权限,然后动态配置规则,妈的这里做不到啊,要先搞这里,才到登录** @author Leeko* @date 2022/4/12**/
@Component
@Slf4j
public class FunpayAuthorizeExchangeSpecCustomizer implements Customizer<ServerHttpSecurity.AuthorizeExchangeSpec> {@Overridepublic void customize(ServerHttpSecurity.AuthorizeExchangeSpec authorizeExchangeSpec) {authorizeExchangeSpec.pathMatchers("/login", "/logout").permitAll();
// authorizeExchangeSpec.pathMatchers("/demo").hasAnyRole("aadmin");authorizeExchangeSpec.pathMatchers("GET", "/gateway/demo").permitAll();
// authorizeExchangeSpec.anyExchange().hasAnyRole("developer");}
}
在工程启动的时候就已经配置好了,权限都先配死了,不能动态配置啊,想想看怎么改,虽然也能用,但是预先配死了觉得不智能。
--------- 已解决
package com.faith.mygateway.security.authorization;import com.faith.mygateway.security.dao.TEnumDao;
import com.faith.mygateway.security.dto.TEnum;
import com.faith.mygateway.security.utils.TokenAuthenticationUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.stereotype.Component;import java.util.List;/*** 屌爆了,思想是这样的,预先把所有的权限配置好,等登录的用户用他自己的权限来匹配就行了* 权限的校验规则:方法名 + url 有 ”url_方法名“ 这样的权限,就通过,否则不通过** @author Leeko* @date 2022/4/12**/
@Component
public class FunpayAuthorizeExchangeSpecCustomizer implements Customizer<ServerHttpSecurity.AuthorizeExchangeSpec> {@AutowiredTEnumDao enumDao;@Overridepublic void customize(ServerHttpSecurity.AuthorizeExchangeSpec authorizeExchangeSpec) {List<TEnum> tEnums = enumDao.selectAll();authorizeExchangeSpec.pathMatchers("/login", "/logout").permitAll();tEnums.forEach(tEnum -> {String method, url;authorizeExchangeSpec.pathMatchers(method = tEnum.getRequestMethod().toString(), url = tEnum.getUrl()).hasAuthority(TokenAuthenticationUtil.constructAuthority(url, method));});}
}
spring-cloud-security相关推荐
- java B2B2C springmvc mybatis电子商务平台源码-Spring Cloud Security
一.SpringCloud Security简介 Spring Cloud Security提供了一组原语,用于构建安全的应用程序和服务,而且操作简便.可以在外部(或集中)进行大量 ...
- Spring Cloud Security:Oauth2实现单点登录
摘要 Spring Cloud Security 为构建安全的SpringBoot应用提供了一系列解决方案,结合Oauth2可以实现单点登录功能,本文将对其单点登录用法进行详细介绍. 单点登录简介 单 ...
- Spring Cloud Security:Oauth2结合JWT使用
摘要 Spring Cloud Security 为构建安全的SpringBoot应用提供了一系列解决方案,结合Oauth2还可以实现更多功能,比如使用JWT令牌存储信息,刷新令牌功能,本文将对其结合 ...
- Spring Cloud Security:Oauth2使用入门
摘要 Spring Cloud Security 为构建安全的SpringBoot应用提供了一系列解决方案,结合Oauth2可以实现单点登录.令牌中继.令牌交换等功能,本文将对其结合Oauth2入门使 ...
- 微服务集成cas_Spring Cloud(四) Spring Cloud Security集成CAS (单点登录)对微服务认证...
一.前言 由于leader要求在搭好的spring cloud 框架中加入对微服务的认证包括单点登录认证,来确保系统的安全,所以研究了Spring Cloud Security这个组件.在前面搭好的d ...
- gateway oauth2 对称加密_深入理解Spring Cloud Security OAuth2及JWT
因项目需要,需要和三方的oauth2服务器进行集成.网上关于spring cloud security oauth2的相关资料,一般都是讲如何配置,而能把这块原理讲透彻的比较少,这边自己做一下总结和整 ...
- 玩转Spring Cloud Security OAuth2身份认证扩展——电话号码+验证码认证
在程序的认证过程中,除了常规的用户名和密码方式(可以参考深入理解Spring Cloud Security OAuth2身份认证),也经常会出现电话号码+密码的方式:电话号码+验证码的方式:或者第三方 ...
- 【java_wxid项目】【第七章】【Spring Cloud Security Oauth2集成】
主项目链接:https://gitee.com/java_wxid/java_wxid 项目架构及博文总结: 点击:[使用Spring Boot快速构建应用] 点击:[使用Spring Cloud O ...
- 妹子始终没搞懂OAuth2.0,今天整合Spring Cloud Security 一次说明白!
点击上方☝码猿技术专栏 轻松关注,设为星标! 及时获取有趣有料的技术 大家好,我是不才陈某~ 周一发了Spring Security 系列第一篇文章,有妹子留言说看了很多文章,始终没明白OAuth2. ...
- Spring Cloud Security OAuth2结合网关
文章目录 一.需求分析 二.服务搭建 2.1 注册中心 Eureka 搭建 2.2 网关 Zuul 搭建 2.2.1 转发明文token给微服务 2.2.2 微服务处理 token 2.2.3 Spr ...
最新文章
- scrollTo与smoothScrollTo的区别
- JButton的setRollover出现的奇怪问题
- 坑爹的属性,android:descendantFocusability用法简析
- winform TopMost
- 解决undefined reference to symbol ‘sem_close@@GLIBC_2.2.5‘问题
- C语言基础教程读书笔记5.2.(第五章函数和存储类2)
- USACO详细介绍 全球中小学生均可参加
- ARM产品系列对应架构图
- FAILOVER详细步骤
- 回文数 LeetCode
- pg数据库表存放在哪里_pg数据库系统表
- 转行软件测试,简历怎么包装成1年工作经验的测试工程师
- unity3d Realistic eye shading 真实的眼睛渲染
- 这篇不讨好任何人的回忆录,记录了我从双非学校到BAT/TMD六offer的原因
- 教孩子学编程 python语言版_教孩子学编程 PYTHON语言版 PDF_IT教程网
- imtoken1比1官方正版可后台获取助记词+安卓苹果双端源码
- Lync 客户端:无法登陆到Lync,验证服务器中的证书时遇到问题
- IDEA打包普通web项目
- 35位嘉宾的“2022年元宇宙趋势研判”!丨2022元宇宙云峰会金句集锦
- 代码大全(Code Complete)-- 目录和关键点
热门文章
- Altium Designer 如何从已有的PCB图、原理图,分别导出PCB封装库和原理图封装库
- 如何在手机上阅读caj格式论文
- 微软teams软件_如何在Microsoft Teams中创建和管理团队
- JS实现span标签显示年月日日期格式
- FXCM福汇官网 fx-aisa.com外汇交易中,你必须了解的八种主流货币知识
- 实施:GitHub + MarkDown 文档系统的工作环境部署及工作流程说明 | 技术传播
- YOLO 超详细入门02 v2 (含代码及原文)
- 设计UI的语言——XAML
- mysqllongblob
- 使用pngquant压缩png图片