欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis-plus通過(guò)添加攔截器實(shí)現(xiàn)簡(jiǎn)單數(shù)據(jù)權(quán)限

 更新時(shí)間:2023年08月30日 09:20:25   作者:野風(fēng)r  
系統(tǒng)需要根據(jù)用戶所屬的公司,來(lái)做一下數(shù)據(jù)權(quán)限控制,具體一點(diǎn),就是通過(guò)表中的company_id進(jìn)行權(quán)限控制,項(xiàng)目使用的是mybatis-plus,所以通過(guò)添加攔截器的方式,修改查詢sql,實(shí)現(xiàn)數(shù)據(jù)權(quán)限,本文就通過(guò)代碼給大家詳細(xì)的講解一下,需要的朋友可以參考下

1 配置文件中的配置

# 數(shù)據(jù)權(quán)限配置  
data-permission:  
  # 不再數(shù)據(jù)權(quán)限的表,目前主要是公司信息表,和一些關(guān)聯(lián)表  
  not-control-tables: role_menu,user_company,user_role  
  # 權(quán)限控制表,即基于哪個(gè)表的數(shù)據(jù)來(lái)做權(quán)限區(qū)分,目前是公司信息表  
  base-table: company_info  
  # 特殊的uri,不進(jìn)行數(shù)據(jù)權(quán)限控制  
  not-control-uri: /checkCompany-post

2 在權(quán)限處理時(shí),將請(qǐng)求 uri 放入到內(nèi)存中

/**  
 * 自定義權(quán)限處理  
 */  
@Component  
@Slf4j  
public class CustomAuthorizationManager implements AuthorizationManager<RequestAuthorizationContext> {  
    @Override  
    public AuthorizationDecision check(  
            Supplier<Authentication> authentication,  
            RequestAuthorizationContext requestAuthorizationContext  
    ) {  
	   // …… 
        HttpServletRequest request = requestAuthorizationContext.getRequest();  
        String method = request.getMethod();  
        String path = request.getRequestURI();  
        // 將當(dāng)前的請(qǐng)求的信息,放入到user中,用戶后面的數(shù)據(jù)權(quán)限  
        LoginUser loginUser = (LoginUser) authentication.get().getPrincipal();  
        loginUser.setUri(path + "-" + method.toLowerCase());  
	  // ……
    }
}

另外,用戶在的登錄系統(tǒng)之后,有一個(gè)選擇公司的動(dòng)作,這時(shí)將用戶選擇的公司信息放入緩存中:

// ……
// 緩存用戶選擇的公司  
RBucket<String> bucket = redissonClient.getBucket(OPERATION_COMPANY + loginUserId);  
bucket.set(companyId, Duration.ofHours(2));
// ……

3 攔截器中的配置

import cn.hutool.core.util.ObjectUtil;  
import cn.hutool.core.util.StrUtil;  
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;  
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;  
import lombok.Data;  
import lombok.extern.slf4j.Slf4j;  
import net.sf.jsqlparser.JSQLParserException;  
import net.sf.jsqlparser.expression.Expression;  
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;  
import net.sf.jsqlparser.parser.CCJSqlParserUtil;  
import net.sf.jsqlparser.statement.select.PlainSelect;  
import net.sf.jsqlparser.statement.select.Select;  
import org.apache.ibatis.executor.Executor;  
import org.apache.ibatis.mapping.BoundSql;  
import org.apache.ibatis.mapping.MappedStatement;  
import org.apache.ibatis.session.ResultHandler;  
import org.apache.ibatis.session.RowBounds;  
import org.redisson.api.RBucket;  
import org.redisson.api.RedissonClient;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.beans.factory.annotation.Value;  
import org.springframework.security.core.Authentication;  
import org.springframework.security.core.context.SecurityContextHolder;  
import org.springframework.stereotype.Component;  
import java.sql.SQLException;  
import java.util.Arrays;  
import java.util.List;  
/**  
 * 數(shù)據(jù)權(quán)限控制  
 */  
@Data  
@Component  
@Slf4j  
public class DataPermissionInterceptor implements InnerInterceptor {  
    @Autowired  
    private RedissonClient redissonClient;  
    @Value("${data-permission.not-control-tables}")  
    public String notControlTables;  
    @Value("${data-permission.base-table}")  
    public String baseTable;  
    @Value("${data-permission.not-control-uri}")  
    public String notControlUri;  
    @Override  
    public boolean willDoQuery(  
            Executor executor,  
            MappedStatement ms,  
            Object parameter,  
            RowBounds rowBounds,  
            ResultHandler resultHandler,  
            BoundSql boundSql  
    ) throws SQLException {  
        return InnerInterceptor.super.willDoQuery(executor, ms, parameter, rowBounds, resultHandler, boundSql);  
    }  
    @Override  
    public void beforeQuery(  
            Executor executor,  
            MappedStatement ms,  
            Object parameter,  
            RowBounds rowBounds,  
            ResultHandler resultHandler,  
            BoundSql boundSql  
    ) throws SQLException {  
        log.debug("數(shù)據(jù)權(quán)限處理……");  
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();  
        if (ObjectUtil.isNull(authentication)) {  
            log.debug("數(shù)據(jù)權(quán)限處理, 未登錄!");  
            return;  
        }  
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();  
        LoginUser loginUser = (LoginUser) principal;  
        String username = loginUser.getUsername();  
        // 如果是系統(tǒng)管理員,不需做作權(quán)限處理  
        if (SYSTEM_ADMINISTRATOR_ACCOUNT.equals(username)) {  
            log.debug("數(shù)據(jù)權(quán)限處理,當(dāng)前為管理員,不需要處理數(shù)據(jù)權(quán)限。");  
            return;  
        }  
        String uri = loginUser.getUri();  
        log.debug("數(shù)據(jù)權(quán)限處理,當(dāng)前uri為:{}", uri);  
        if (notControlUri.contains(uri)) {  
            log.debug("數(shù)據(jù)權(quán)限處理,當(dāng)前uri,不需要處理數(shù)據(jù)權(quán)限。");  
            return;  
        }  
        String sql = boundSql.getSql();  
        Select select;  
        try {  
            select = (Select) CCJSqlParserUtil.parse(sql);  
        } catch (JSQLParserException e) {  
            throw new RuntimeException(e);  
        }  
        // 系統(tǒng)自動(dòng)生成的sql,一般都是單表查詢,所以這里暫時(shí)不考慮復(fù)雜的情況  
        PlainSelect plainSelect = (PlainSelect) select.getSelectBody();  
        net.sf.jsqlparser.schema.Table table = (net.sf.jsqlparser.schema.Table) plainSelect.getFromItem();  
        String tableName = table.getName();  
        // 排除一些不需要控制的表  
        List<String> notControlTablesList = Arrays.asList(notControlTables.split(","));  
        if (notControlTablesList.contains(tableName.toLowerCase())) {  
            log.debug("數(shù)據(jù)權(quán)限處理,當(dāng)前表不做權(quán)限控制,table is {}", tableName);  
            return;  
        }  
        String userId = loginUser.getUser().getPkId();  
        RBucket<String> bucket = redissonClient.getBucket(OPERATION_COMPANY + userId);  
        String companyId = bucket.get();  
        if (StrUtil.isBlank(companyId)) {  
            throw new BaseException("公司id不存在!");  
        }  
        // 處理SQL語(yǔ)句  
        // 基礎(chǔ)表,根據(jù)主鍵進(jìn)行控制  
        log.debug("數(shù)據(jù)權(quán)限處理,處理之前的sql為: {}", sql);  
        Expression where = plainSelect.getWhere();  
        Expression envCondition;  
        try {  
            if (baseTable.equals(tableName.toLowerCase())) {  
                envCondition = CCJSqlParserUtil.parseCondExpression("PK_ID = " + companyId);  
            } else {  
                envCondition = CCJSqlParserUtil.parseCondExpression("COMPANY_ID = " + companyId);  
            }  
        } catch (JSQLParserException e) {  
            throw new RuntimeException(e);  
        }  
        if (where == null) {  
            plainSelect.setWhere(envCondition);  
        } else {  
            AndExpression andExpression = new AndExpression(where, envCondition);  
            plainSelect.setWhere(andExpression);  
        }  
        sql = plainSelect.toString();  
        log.debug("數(shù)據(jù)權(quán)限處理,處理之后的sql為: {}", sql);  
        PluginUtils.MPBoundSql mpBs = PluginUtils.mpBoundSql(boundSql);  
        mpBs.sql(sql);  
    }  
}

4 啟用插件

  
@Configuration  
@MapperScan("xxx.xxx.xx.mapper")  
public class MybatisPlusConfig {  
    @Autowired  
    private DataPermissionInterceptor dataPermissionInterceptor;  
    /**  
     * 新的分頁(yè)插件,一緩和二緩遵循mybatis的規(guī)則,需要設(shè)置 MybatisConfiguration#useDeprecatedExecutor = false 避免緩存出現(xiàn)問(wèn)題(該屬性會(huì)在舊插件移除后一同移除)  
     */    @Bean  
    public MybatisPlusInterceptor mybatisPlusInterceptor() {  
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();  
        interceptor.addInnerInterceptor(dataPermissionInterceptor);  
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));  
        return interceptor;  
    }  
}

到此這篇關(guān)于Mybatis-plus通過(guò)添加攔截器實(shí)現(xiàn)簡(jiǎn)單數(shù)據(jù)權(quán)限的文章就介紹到這了,更多相關(guān)Mybatis-plus實(shí)現(xiàn)簡(jiǎn)單數(shù)據(jù)權(quán)限內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java中的值傳遞以及引用傳遞和數(shù)組傳遞詳解

    Java中的值傳遞以及引用傳遞和數(shù)組傳遞詳解

    這篇文章主要介紹了Java中的值傳遞以及引用傳遞和數(shù)組傳遞詳解,Java不允許程序員選擇按值傳遞還是按引用傳遞各個(gè)參數(shù),就對(duì)象而言,不是將對(duì)象本身傳遞給方法,而是將對(duì)象的的引用或者說(shuō)對(duì)象的首地址傳遞給方法,引用本身是按值傳遞的,需要的朋友可以參考下
    2023-07-07
  • 詳解SpringBoot如何自定義一個(gè)Starter

    詳解SpringBoot如何自定義一個(gè)Starter

    小伙伴們?cè)?jīng)可能都經(jīng)歷過(guò)整天寫著CURD的業(yè)務(wù),都沒(méi)寫過(guò)一些組件相關(guān)的東西,這篇文章記錄一下SpringBoot如何自定義一個(gè)Starter。原理和理論就不用多說(shuō)了,可以在網(wǎng)上找到很多關(guān)于該方面的資料,這里主要分享如何自定義
    2022-11-11
  • java poi設(shè)置生成的word的圖片為上下型環(huán)繞以及其位置的實(shí)現(xiàn)

    java poi設(shè)置生成的word的圖片為上下型環(huán)繞以及其位置的實(shí)現(xiàn)

    這篇文章主要介紹了java poi設(shè)置生成的word的圖片為上下型環(huán)繞以及其位置的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • idea 開(kāi)發(fā)神器之idea插件匯總

    idea 開(kāi)發(fā)神器之idea插件匯總

    這篇文章主要介紹了idea 開(kāi)發(fā)神器之idea插件匯總,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • Java之a(chǎn)pi網(wǎng)關(guān)斷言及過(guò)濾器案例講解

    Java之a(chǎn)pi網(wǎng)關(guān)斷言及過(guò)濾器案例講解

    這篇文章主要介紹了Java之a(chǎn)pi網(wǎng)關(guān)斷言及過(guò)濾器案例講解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Java中高效的判斷數(shù)組中某個(gè)元素是否存在詳解

    Java中高效的判斷數(shù)組中某個(gè)元素是否存在詳解

    相信大家在操作Java的時(shí)候,經(jīng)常會(huì)要檢查一個(gè)數(shù)組(無(wú)序)是否包含一個(gè)特定的值?這是一個(gè)在Java中經(jīng)常用到的并且非常有用的操作。同時(shí),這個(gè)問(wèn)題在Stack Overflow中也是一個(gè)非常熱門的問(wèn)題。本文將分析幾種常見(jiàn)用法及其時(shí)間成本,有需要的朋友們可以參考借鑒。
    2016-11-11
  • JavaWeb實(shí)現(xiàn)簡(jiǎn)單查詢商品功能

    JavaWeb實(shí)現(xiàn)簡(jiǎn)單查詢商品功能

    這篇文章主要為大家詳細(xì)介紹了JavaWeb實(shí)現(xiàn)簡(jiǎn)單查詢商品功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • 5分鐘快速搭建SpringBoot3?+?MyBatis-Plus工程/項(xiàng)目的實(shí)現(xiàn)示例

    5分鐘快速搭建SpringBoot3?+?MyBatis-Plus工程/項(xiàng)目的實(shí)現(xiàn)示例

    本文主要介紹了使用IntelliJ?IDEA創(chuàng)建Spring?Boot工程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2025-01-01
  • java 兩階段終止線程的正確做法

    java 兩階段終止線程的正確做法

    這篇文章主要給大家分享了java 兩階段終止線程的正確做法,文章列舉出錯(cuò)誤的做法與正確做法做對(duì)比,具有一定的參考價(jià)值,需要的小伙伴可以參考一下,希望對(duì)你有所幫助
    2021-12-12
  • 實(shí)例解析Java的Jackson庫(kù)中的數(shù)據(jù)綁定

    實(shí)例解析Java的Jackson庫(kù)中的數(shù)據(jù)綁定

    這篇文章主要介紹了Java的Jackson庫(kù)中的數(shù)據(jù)綁定,這里分為通常的簡(jiǎn)單數(shù)據(jù)綁定與全數(shù)據(jù)綁定兩種情況來(lái)講,需要的朋友可以參考下
    2016-01-01

最新評(píng)論