mapper權(quán)限攔截/自定義注解指定mapper中的具體方法,拿表的別名,或可立即分頁,自定義異常,全局異常處理

package com.csw.mybatisSpringboot.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class InterceptRunner implements ApplicationRunner {

    @Autowired
    private List<SqlSessionFactory> sqlSessionFactoryList;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        DataPermissionInterceptor mybatisInterceptor = new DataPermissionInterceptor();
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
            org.apache.ibatis.session.Configuration configuration = sqlSessionFactory.getConfiguration();
            configuration.addInterceptor(mybatisInterceptor);
        }
    }
}

package com.csw.mybatisSpringboot.config.quanXian;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface SqlLimit {
    /**
     * 需要攔截權(quán)限的表名
     */
    String tableName() default "";

    /**
     * 是否要立即分頁,true 自動(dòng)拼接limit,否用分頁插件或者自定義分頁工具,[默認(rèn)否]
     */
    boolean isLimit() default false;

}


package com.csw.mybatisSpringboot.config.quanXian;

import cn.hutool.core.bean.BeanUtil;
import com.csw.mybatisSpringboot.config.PageDto;
import com.csw.mybatisSpringboot.config.exception.BusinessException;
import com.csw.mybatisSpringboot.entity.User;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.FromItem;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectBody;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
@Intercepts({
        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
//        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),
})
@Slf4j
public class DataPermissionInterceptor implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        MappedStatement statement = (MappedStatement) invocation.getArgs()[0];
        Object parameter = invocation.getArgs()[1];
        BoundSql boundSql = statement.getBoundSql(parameter);
        String originalSql = boundSql.getSql();
        Object parameterObject = boundSql.getParameterObject();

        SqlLimit sqlLimit = isLimit(statement);
        if (sqlLimit == null) {
            return invocation.proceed();
        }

        RequestAttributes req = RequestContextHolder.getRequestAttributes();
        if (req == null) {
            return invocation.proceed();
        }

        //處理request
        HttpServletRequest request = ((ServletRequestAttributes) req).getRequest();

        //從request里面拿到用戶信息
       User userVo = null;
        try {
            userVo = UserUtils.getUserInfo(request);
        } catch (Exception e) {
            //從token里面解析失敗
            throw new BusinessException("用戶未登錄");
        }

       //拿到表的名稱或者別名
        String tableAlias = getTableAliasString(originalSql, sqlLimit.tableName());
        //拿到修改后的sql
        String sql = addTenantCondition(originalSql, userVo, tableAlias, parameter, sqlLimit.isLimit());


        log.info("原SQL:{}, 數(shù)據(jù)權(quán)限替換后的SQL:{}", originalSql, sql);
        BoundSql newBoundSql = new BoundSql(statement.getConfiguration(), sql, boundSql.getParameterMappings(), parameterObject);
        MappedStatement newStatement = copyFromMappedStatement(statement, new BoundSqlSqlSource(newBoundSql));
        invocation.getArgs()[0] = newStatement;
        return invocation.proceed();
    }

    /**
     * 獲取表的別名
     *
     * @param originalSql
     * @param sqlLimit
     * @return
     * @throws JSQLParserException
     */
    private static String getTableAliasString(String originalSql, String tableAlias) throws JSQLParserException {
        Statement statement = CCJSqlParserUtil.parse(originalSql);

        if (statement instanceof Select) {
            Select select = (Select) statement;
            SelectBody selectBody = select.getSelectBody();
            if (selectBody instanceof PlainSelect) {
                PlainSelect plainSelect = (PlainSelect) selectBody;
                FromItem fromItem = plainSelect.getFromItem();
                if (fromItem instanceof Table) {
                    Table table = (Table) fromItem;
                    String tableName = table.getName();
                    Alias alias = table.getAlias();
                    if (tableAlias.equals(tableName) && alias != null) {
                        tableAlias = alias.getName();
                        return tableAlias;
                    }
                    System.out.println("Table Name: " + tableName);
                }
            }
        }
        return tableAlias;
    }


    /**
     * 重新拼接SQL
     */
    private String addTenantCondition(String originalSql, User user, String alias, Object parameter, boolean isLimit) {
        StringBuilder sb = new StringBuilder(originalSql);
       int index = sb.toString().toLowerCase().indexOf("where");

        String fieldSubSystemId = getField(alias, "sub_system_id");
        String fieldOrgId = getField(alias, "org_id");
        String fieldCreateId = getField(alias, "create_id");

        if (user.getUserType().equals("1")) {//超級(jí)管理員-查看所有
            return originalSql;
        } else if (user.getUserType().equals("2")) {//子系統(tǒng)管理員-查看本系統(tǒng)所有
            benSystemAll(user, index, sb, fieldSubSystemId);
            return sb.toString();
        } else if (user.getUserType().equals("3")) {//用戶按照數(shù)據(jù)權(quán)限
            if (user.getDataAuthId().equals("1001") || user.getDataAuthId() == null) {//本系統(tǒng)下所有
                benSystemAll(user, index, sb, fieldSubSystemId);
            } else if (user.getDataAuthId().equals("1002")) {
                benSystemAll(user, index, sb, fieldSubSystemId);
                sb.insert(index + 5, " " + fieldOrgId + " = " + user.getOrgId() + " and ");
            } else if (user.getDataAuthId().equals("1003")) {
                benSystemAll(user, index, sb, fieldSubSystemId);
                //模擬查詢出用戶部門及以下部門
                List listOrg = new ArrayList();
                listOrg.add(1);
                listOrg.add(2);
                listOrg.add(3);
                String string = listOrg.toString();
                string = string.substring(1, string.length() - 1);
                sb.insert(index + 5, " " + fieldOrgId + " in (" + string + ") and ");
            } else if (user.getDataAuthId().equals("1004")) {
                benSystemAll(user, index, sb, fieldSubSystemId);
                sb.insert(index + 5, " " + fieldCreateId + " = " + user.getCreateId() + " and ");
            }
        }

        if (isLimit == true) {//如果需要用limit分頁的就傳true,要不然就傳false,在外面用工具類分
            //拿到分頁參數(shù),如果數(shù)據(jù)庫用的是limit風(fēng)格
            Map map = BeanUtil.beanToMap(parameter);
            PageDto dto = BeanUtil.copyProperties(map.get("dto"), PageDto.class);
            //第一頁在數(shù)據(jù)庫里面是0
            sb.append(" limit ").append(dto.getPageNo() - 1 + "," + dto.getPageSize());
        }

        return sb.toString();

    }

    private static void benSystemAll(User user, int index, StringBuilder sb, String fieldSubSystemId) {
        if (index < 0) {
            sb.append(" where ").append(fieldSubSystemId).append(" = ").append(user.getSubSystemId());
        } else {
            sb.insert(index + 5, " " + fieldSubSystemId + " = " + user.getSubSystemId() + " and ");
        }
    }

    private static String getField(String alias, String field) {
        if (StringUtils.isNoneBlank(alias)) {
            field = alias + "." + field;
        }
        return field;
    }

    private MappedStatement copyFromMappedStatement(MappedStatement ms, SqlSource newSqlSource) {
        MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), newSqlSource, ms.getSqlCommandType());
        builder.resource(ms.getResource());
        builder.fetchSize(ms.getFetchSize());
        builder.statementType(ms.getStatementType());
        builder.keyGenerator(ms.getKeyGenerator());
        builder.timeout(ms.getTimeout());
        builder.parameterMap(ms.getParameterMap());
        builder.resultMaps(ms.getResultMaps());
        builder.cache(ms.getCache());
        builder.useCache(ms.isUseCache());
        return builder.build();
    }


    /**
     * 通過注解判斷是否需要限制數(shù)據(jù)
     *
     * @return
     */
    private SqlLimit isLimit(MappedStatement mappedStatement) {
        SqlLimit sqlLimit = null;
        try {
            String id = mappedStatement.getId();
            String className = id.substring(0, id.lastIndexOf("."));
            String methodName = id.substring(id.lastIndexOf(".") + 1, id.length());
            final Class<?> cls = Class.forName(className);
            final Method[] method = cls.getMethods();
            for (Method me : method) {
                if (me.getName().equals(methodName) && me.isAnnotationPresent(SqlLimit.class)) {
                    sqlLimit = me.getAnnotation(SqlLimit.class);
                    return sqlLimit;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sqlLimit;
    }


    public static class BoundSqlSqlSource implements SqlSource {

        private final BoundSql boundSql;

        public BoundSqlSqlSource(BoundSql boundSql) {
            this.boundSql = boundSql;
        }

        @Override
        public BoundSql getBoundSql(Object parameterObject) {
            return boundSql;
        }
    }
}
package com.csw.mybatisSpringboot.config;

import lombok.Data;


@Data
//@ApiModel("分頁實(shí)體")
public class PageDto {

    //@ApiModelProperty("當(dāng)前頁碼")
    private Integer pageNo = 1;

    //@ApiModelProperty("每頁顯示條數(shù)")
    private Integer pageSize = 10;

}


package com.csw.mybatisSpringboot.dto;

import com.csw.mybatisSpringboot.config.PageDto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel("列表查詢?nèi)雲(yún)?)
public class UserListDto extends PageDto {
    @ApiModelProperty("名字")
    private String name;

}

package com.csw.mybatisSpringboot.config;

import lombok.Data;

import java.util.List;

/**
 * 分頁查詢信息傳遞類
 *
 * @param <T>
 */

@Data
public class PageResult<T> {
    private List<T> items;
    private Long total;
    private Long totalPage;
    private Integer pageNo;
    private Integer pageSize;
}

package com.csw.mybatisSpringboot.config.page;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

public class PageResultUtil {

    public static <T> PageResult<T> getPageResult(Page<T> param) {
        PageResult<T> result = new PageResult<>();
        if (param != null) {
            result.setTotalPage(param.getPages());
            result.setTotal(param.getTotal());
            result.setItems(param.getRecords());
            result.setPageNo((int) param.getCurrent());
            result.setPageSize((int) param.getSize());
        }
        return result;
    }

}

如果使用mapper內(nèi)置方法的話可以使用重寫覆蓋即可
【int index = sb.toString().toLowerCase().indexOf("where");】修改了上面where的定位

service

 @Override
    public PageResult<User> selectAllByName(UserListDto dto) {
        Page page = new Page<>(dto.getPageNo(), dto.getPageSize());
        Page<User> userList = baseMapper.selectAllByName(page, dto);

        PageResult<User> pageResult = PageResultUtil.getPageResult(userList);
        return pageResult;
    }

mapper

 @SqlLimit(tableName = "user")
    Page<User> selectAllByName(Page page, @Param("dto") UserListDto dto);


【以上為來自大佬的總結(jié)和進(jìn)一步優(yōu)化】
mybatis攔截器實(shí)現(xiàn)數(shù)據(jù)權(quán)限_mybatis數(shù)據(jù)權(quán)限-CSDN博客  https://blog.csdn.net/m0_71777195/article/details/131139654

java(springboot) mybatis 數(shù)據(jù)權(quán)限詳細(xì)實(shí)現(xiàn)(圖文) - 知乎  https://zhuanlan.zhihu.com/p/516113586?utm_id=0

異??催@個(gè)http://www.itdecent.cn/p/d395aef20c15

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容