自定义 hasPermission 校验规则

自定义一个 Spring Security hasPermission 校验规则:

在 tienchin-framework 模块当中进行自定义,新建 CustomSecurityExpressionRoot.java 自定义 hasPermission 判断逻辑类:

/**
* @author BNTang
* @version 1.0
* @description 自定义 hasPermission 判断逻辑
* @since 2023-08-26
**/
public class CustomSecurityExpressionRoot
extends SecurityExpressionRoot
implements MethodSecurityExpressionOperations { private Object filterObject;
private Object returnObject;
private final AntPathMatcher antPathMatcher = new AntPathMatcher(); /**
* Creates a new instance
*
* @param authentication the {@link Authentication} to use. Cannot be null.
*/
public CustomSecurityExpressionRoot(Authentication authentication) {
super(authentication);
} /**
* 判断当前对象是否具备某一个权限
*
* @param permission 权限
* @return boolean
* @author BNTang
* @since 2023/08/26 08:43:56
*/
public boolean hasPermission(String permission) {
// 获取当前登录用户所具有的权限
// 这里实际上调用到的是 top.it6666.common.core.domain.model.LoginUser.getAuthorities 方法的返回值
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
for (GrantedAuthority authority : authorities) {
if (antPathMatcher.match(authority.getAuthority(), permission)) {
return true;
}
}
return false;
} /**
* 是否具备多个权限中的任意一个权限
*
* @param permissions 权限
* @return boolean
* @author BNTang
* @since 2023/08/26 08:44:52
*/
public boolean hasAnyPermissions(String... permissions) {
if (permissions == null || permissions.length == 0) {
return false;
}
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
for (GrantedAuthority authority : authorities) {
for (String permission : permissions) {
if (antPathMatcher.match(authority.getAuthority(), permission)) {
return true;
}
}
}
return false;
} /**
* 是否具备拥有所有权限
*
* @param permissions 权限
* @return boolean
* @author BNTang
* @since 2023/08/26 08:44:26
*/
public boolean hasAllPermissions(String... permissions) {
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
if (permissions == null || permissions.length == 0) {
return false;
}
for (String permission : permissions) {
boolean flag = false;
for (GrantedAuthority authority : authorities) {
if (antPathMatcher.match(authority.getAuthority(), permission)) {
flag = true;
}
}
if (!flag) {
return false;
}
}
return true;
} @Override
public void setFilterObject(Object filterObject) {
this.filterObject = filterObject;
} @Override
public Object getFilterObject() {
return filterObject;
} @Override
public void setReturnObject(Object returnObject) {
this.returnObject = returnObject;
} @Override
public Object getReturnObject() {
return returnObject;
} @Override
public Object getThis() {
return this;
}
}

新建自定义 hasPermission 判断逻辑处理器类:

/**
* @author BNTang
* @version 1.0
* @description 自定义 hasPermission 判断逻辑处理器
* @since 2023-08-26
**/
public class CustomMethodSecurityExpressionHandler
extends DefaultMethodSecurityExpressionHandler {
@Override
protected MethodSecurityExpressionOperations createSecurityExpressionRoot(Authentication authentication,
MethodInvocation invocation) { CustomSecurityExpressionRoot root = new CustomSecurityExpressionRoot(authentication); root.setTrustResolver(getTrustResolver());
root.setPermissionEvaluator(getPermissionEvaluator());
root.setRoleHierarchy(getRoleHierarchy()); return root;
}
}

注册一下自定义 hasPermission 判断逻辑处理器,更改 ResourcesConfig:

/**
* 自定义 hasPermission 判断逻辑处理器
*
* @return {@code CustomMethodSecurityExpressionHandler }
* @author BNTang
* @since 2023/08/26 08:57:19
*/
@Bean
CustomMethodSecurityExpressionHandler customMethodSecurityExpressionHandler() {
return new CustomMethodSecurityExpressionHandler();
}

更改 LoginUser,完善一下 LoginUser 当中的 getAuthorities 方法:

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
if (permissions != null && !permissions.isEmpty()) {
return permissions.stream()
.map(SimpleGrantedAuthority::new)
.collect(Collectors.toList());
}
return Collections.emptyList();
}

编写查询接口

更改 ChannelController:

/**
* <p>
* 渠道管理表 前端控制器
* </p>
*
* @author BNTang
* @since 2023-08-22
*/
@RestController
@RequestMapping("/tienchin/channel")
public class ChannelController extends BaseController { @Resource
private IChannelService iChannelService; @PreAuthorize("hasPermission('tienchin:channel:list')")
@GetMapping("/list")
TableDataInfo list() {
startPage();
return getDataTable(iChannelService.selectChannelList());
}
}

更改 IChannelService:

/**
* <p>
* 渠道管理表 服务类
* </p>
*
* @author BNTang
* @since 2023-08-22
*/
public interface IChannelService
extends IService<Channel> { /**
* 查询渠道列表
*
* @return {@code List<Channel> }
* @author BNTang
* @since 2023/08/26 09:32:57
*/
List<Channel> selectChannelList();
}

更改 ChannelServiceImpl:

/**
* <p>
* 渠道管理表 服务实现类
* </p>
*
* @author BNTang
* @since 2023-08-22
*/
@Service
public class ChannelServiceImpl
extends ServiceImpl<ChannelMapper, Channel>
implements IChannelService { @Resource
private ChannelMapper channelMapper; @Override
public List<Channel> selectChannelList() {
return channelMapper.selectChannelList();
}
}

更改 ChannelMapper:

/**
* <p>
* 渠道管理表 Mapper 接口
* </p>
*
* @author BNTang
* @since 2023-08-22
*/
public interface ChannelMapper extends BaseMapper<Channel> { /**
* 查询渠道列表
*
* @return {@code List<Channel> }
* @author BNTang
* @since 2023/08/26 09:33:46
*/
List<Channel> selectChannelList();
}

更改 ChannelMapper.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="top.it6666.channel.mapper.ChannelMapper">
<select id="selectChannelList" resultType="top.it6666.channel.domain.Channel">
SELECT channel_id,
channel_name,
status,
remark,
type,
create_by,
update_by,
create_time,
update_time,
del_flag
FROM tienchin_channel
WHERE del_flag = 0
</select>
</mapper>

全局将 @ss.hasPermi 替换为 hasPermission

TienChin 渠道管理-查看渠道接口的更多相关文章

  1. ERP渠道管理添加验证和查询(二十二)

    添加联系人的后台代码: protected void btnSubmit_Click(object sender, EventArgs e) { BioErpCrmManageChannel chan ...

  2. 电商管理后台 API 接口文档

    1. 电商管理后台 API 接口文档 1.1. API V1 接口说明 接口基准地址:http://127.0.0.1:8888/api/private/v1/ 服务端已开启 CORS 跨域支持 AP ...

  3. Vue之状态管理(vuex)与接口调用

    Vue之状态管理(vuex)与接口调用 一,介绍与需求 1.1,介绍 1,状态管理(vuex) Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态 ...

  4. RookeyFrame Bug 表单管理 -> 查看表单 ->编辑字段页面 JS报错

    表单管理 -> 查看表单 ->编辑字段页面 小bug onchange里面直接就是方法,修改:去掉外面的function(){},直接把方法体写在onchange里面就可以了. 后台方法: ...

  5. Linux进程管理——查看内存的工具

    Linux进程管理——查看内存的工具 一查看内存的工具vmstat vmstat命令:虚拟内存信息vmstat [options] [delay [count]]vmstat 2 5 [root@ce ...

  6. 【Spring】Spring的事务管理 - 1、Spring事务管理概述(数据库事务、Spring事务管理的核心接口)

    Spring事务管理概述 文章目录 Spring事务管理概述 数据库事务 什么是Spring的事务管理? Spring对事务管理的支持 Spring事务管理的核心接口 Platform Transac ...

  7. 谈谈如何用eoLinker管理各类API接口及分享API接口管理小技巧教程

    在前后端分离的开发模式下,前后端往往需要接口文档来进行交互.我的上一篇随笔中已经写到用传统的文档写接口时,由于需求经常变动,接口文档也会随之变动.一开始,某接口信息已经写入文档,但后期因为需求变动,发 ...

  8. sql server作业管理查看/进程管理查看命令

    一.作业管理 (1) select * from msdb.dbo.sysjobhistory   可以查看作业的历史记录 (2) select * from msdb.dbo.sysjobs   查 ...

  9. ASP.NET Web API 2系列(三):查看WebAPI接口的详细说明及测试接口

    引言 前边两篇博客介绍了Web API的基本框架以及路由配置,这篇博客主要解决在前后端分离项目中,为前端人员提供详细接口说明的问题,主要是通过修改WebApi HelpPage相关代码和添加WebAp ...

  10. 033.SAP上查看IDOC接口,PI接口查不到的日志记录,可能在IDOC接口日志里面

    01. SAP系统发料之后,数据没有传输到条码系统,同事也没有任何bc01的日志,这是就要考虑是不是在IDOC接口了,输入事务代码WE02或者WE05 02.双击查看内容 03.点开就能看到详细内容了 ...

随机推荐

  1. 7z压缩测试

    注意: CompressionLevel 选择

  2. linux day1:VMware虚拟机配置 CentOS系统配置

    目录 运维岗位说明 计算机的种类 服务器的种类 服务器的品牌 服务器内部组成 缓存和缓冲 服务器磁盘阵列 raid5 linux系统发展史 去IOE运动 虚拟化技术 虚拟化软件下载 VMware安装 ...

  3. 从阿里云全球实时传输网络GRTN出发,浅谈QOE优化实践

    直播已深入每家每户,以淘宝的直播为例,在粉丝与主播的连麦互动中如何实现无感合屏或切屏?阿里云GRTN核心网技术负责人肖凯,在LVS2022上海站为我们分享了GRTN核心网的运作机制.运用方面以及QOE ...

  4. AtCoder ARC 115 E - LEQ and NEQ (延迟标记线段树 or 笛卡尔积 + DP维护)

    问题链接:Here 长度为 \(N\) 的数列 \(A_1,-,A_N\) .回答满足以下条件的长度 \(N\) 的数列 \(X_1,-,X_N\) 的个数除以 \(998244353\) 的余数. ...

  5. 【每日一题】3.数学考试 (前缀和,线性DP)

    题目链接:Here 思路:区间求和问题可以想到一个常用算法.前缀和.区间 \([l,r]\) 的和可以用 \(sum_r - sum_l\) 方便求出 由于区间长度 \(k\) 已知,所以我们可以直接 ...

  6. Spark 数据倾斜及其解决方案

    本文首发于 vivo互联网技术 微信公众号 https://mp.weixin.qq.com/s/lqMu6lfk-Ny1ZHYruEeBdA 作者简介:郑志彬,毕业于华南理工大学计算机科学与技术(双 ...

  7. <vue 路由 8、keep-alive的使用>

    一.     知识点 1.什么是keep-alive? keep-alive是Vue.js的一个内置组件. 包裹动态组件时,会缓存不活动的组件实例,而不是销毁它们.它自身不会渲染一个 DOM 元素,也 ...

  8. git或gitee 提交代码到远程仓库

    本文为博主原创,未经允许不得转载: 1. 选中远程仓库,并fork 指定的项目到自己的私仓: fork 之后,打开我的仓库便能看到刚刚fork 的项目. 2. clone 项目代码到自己电脑的本地仓库 ...

  9. P5729 【深基5.例7】工艺品制作

    1.题目介绍 [深基5.例7]工艺品制作 题目描述 现有一个长宽高分别为 \(w,x,h\) 组成的实心玻璃立方体,可以认为是由 \(1\times1\times1\) 的数个小方块组成的,每个小方块 ...

  10. bcc的简单学习

    bcc的简单学习 安装 # 安装部分依赖项目 yum install cmake llvm -y dnf install -y bison cmake ethtool flex git iperf3 ...