TienChin 渠道管理-查看渠道接口
1.TienChin 开篇-运行 RuoYiVue2.TienChin 代码格式化-项目结构大改造3.TienChin 项目改造完善&项目结构分析4.TienChin 验证码响应结果分析&验证码生成接口分析5.TienChin 运行 RuoYi-Vue36.TienChin-系统功能介绍7.TienChin 新建业务菜单8.TienChin 创建菜单页面9.TienChin 引入 MyBatisPlus10.TienChin 渠道管理-表创建11.TienChin 渠道管理-渠道类型12.TienChin 渠道管理-工程创建
13.TienChin 渠道管理-查看渠道接口
14.TienChin 渠道管理-前端展示渠道信息15.TienChin 渠道管理-配置字典常量16.TienChin 渠道管理-字典原理分析17.TienChin 渠道管理-权限分配18.TienChin 渠道管理-添加渠道19.TienChin 渠道管理-配置校验失败信息20.TienChin 渠道管理-添加渠道页面开发21.TienChin 渠道管理-更新渠道接口开发22.TienChin 渠道管理-删除渠道23.TienChin 渠道管理-渠道搜索24.TienChin 渠道管理-渠道导出25.TienChin 渠道管理-渠道导入26.TienChin 渠道管理-渠道页面完善27.TienChin 活动管理-准备工作28.TienChin 活动管理-工程创建29.TienChin 活动管理-活动列表展示30.TienChin 活动管理-活动状态完善31.TienChin 活动管理-添加活动页面32.TienChin 活动管理-添加活动接口33.TienChin 活动管理-设置活动的默认状态34.TienChin 活动管理-完成添加活动35.TienChin 活动管理-修改活动接口36.TienChin 活动管理-修改活动37.TienChin 活动管理-删除活动38.TienChin 活动管理-搜索活动39.TienChin 活动管理-活动导出40.TienChin-课程管理-数据表创建41.TienChin-课程管理-创建工程42.TienChin-课程管理-配置课程字典43.TienChin-课程管理-展示课程列表44.TienChin-课程管理-添加课程接口45.TienChin-课程管理-添加课程页面46.TienChin-课程管理-课程更新接口47.TienChin-课程管理-删除课程48.TienChin-课程管理-课程搜索49.TienChin-课程管理-课程导出自定义 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>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具