首页 > 其他分享 >TienChin 渠道管理-查看渠道接口

TienChin 渠道管理-查看渠道接口

时间:2023-08-29 23:56:18浏览次数:26  
标签:return 08 since 接口 渠道 2023 TienChin public permissions

自定义 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

image-20230829234307171

标签:return,08,since,接口,渠道,2023,TienChin,public,permissions
From: https://www.cnblogs.com/BNTang/p/17666157.html

相关文章

  • restful规范和django源码写接口
    一、restful规范1、restful规范是什么,如何来的?一种定义WebAPI接口的设计风格,尤其适用于前后端分离的应用模式中的规范RoyFielding的博士论文提出的2、以后写接口,大致都要遵循如下规范-1数据的安全保障-》url链接一般都采用https协议进行传输--》它比http安全......
  • for循环中调用接口
    有一个需求项,循环调用接口注意不要使用forEach,使用forof或者forin方法1 方法2 自执行函数 ......
  • 1-7可编程并行接口 8255 实验
    COM_8255EQU0273H ;8255控制口PA_8255EQU0270HPB_8255EQU0271HPC_8255EQU0272H_STACKSEGMENTSTACKDW100DUP(?)_STACKENDSDATASEGMENTWORDPUBLIC'DATA'DATAENDSCODESEGMENTSTARTPROCNEARASSUMECS:CODE,DS:DATA,SS:_STACK......
  • Python+Flask设置接口开机自启动
    Windows系统适用创建一个批处理文件(例如 start_flask_api.bat),内容如下:@echooffcd/dC:\path\to\your\flask\app//你要启动程序的路径pythonapp.py//你要启动的程序将批处理文件添加到Windows的启动项中:按下Win+R键打开"运行"对话框,输入 shell:startup 并按回车......
  • App开放接口API安全性—Token签名sign的设计与实现
    在app开放接口API的设计中,避免不了的就是安全性问题。 一、https协议对于一些敏感的API接口,需要使用https协议。https是在http超文本传输协议加入SSL层,它在网络间通信是加密的,所以需要加密证书。 二、签名设计原理:用户登录后向服务器提供用户认证信息(如账户和密码),服务器认证完后......
  • 具有 I 2C 和 SMBus 接口的 TMP75CQDRQ1、TMP75CQDGKRQ1、TMP75BQDGKRQ1 汽车类温度传
    一、器件介绍TMP75-Q1器件属于数字温度传感器,是负温度系数(NTC)和正温度系数(PTC)热敏电阻的理想替代产品。该器件无需校准或外部组件信号调节即可提供典型值为±1°C的精度。器件温度传感器为高度线性化产品,无需复杂计算或查表即可得知温度。片上12位模数转换器(ADC)......
  • JAVA接口
    接口:是一种引用数据类型由interfacec定义只能包含常量和抽象方法不能被实例话接口需要被实现、继承,实现类/派生类:必须重写接口中的所有抽象方法一个类可以实现多个接口,用逗号分隔。接口可以继承接口 接口中成员的访问权限,默认就是public的,也之能是publi......
  • 要调用API接口获取商品数据,首先需要了解该API的文档和规范
    要调用API接口获取商品数据,首先需要了解该API的文档和规范。大多数API都需要使用API密钥进行身份验证,因此您需要先注册API提供商,并从他们那里获取API密钥。以下是一些通用的步骤:注册API提供商并获取API密钥在开始使用任何API之前,您需要先注册API提供商,并从他们那里获取API密钥。请......
  • Python+Flask接口实现简单的ToKen功能
    话不多说,上代码fromflaskimportrequest,jsonifyfromfunctoolsimportwrapsclassTokenRequired:@classmethoddeftoken_required(cls,f):@wraps(f)defdecorated_function(*args,**kwargs):#获取请求头部中的key字段......
  • 05,实现Runnable接口
    packageThreadDemo;//创建线程方式二:实现Runnable接口。推荐,因为单继承多实现特性publicclassTest05implementsRunnable{@Overridepublicvoidrun(){for(inti=0;i<2000;i++){System.out.println("这是run()线程---"+i);......