主頁 > 軟體設計 > 資料權限解決方案

資料權限解決方案

2023-07-13 08:31:21 軟體設計

一、表結構設計

 

 二、實作思路

  1.系統啟動時將字典資料加載到redis作為可選常量池,以及mapper.xml、dao、資料規則資訊加載到redis

  2.用訪問時通過springmvc攔截器對用戶進行攔截獲取token然后通過RSA解密獲取用戶資訊,將用戶資訊,以及請求引數加入本地執行緒

  3.mybatis-plus攔截器對mapper攔截然后決議對應dao層介面的方法,用于加載規則配置資訊

  4、通過策略模式實作讀取不同常量池對規則運算式決議

  5、使用jsqlparser實作sql決議注入

  6、將處理后的sql交給mybatis-plus框架處理

三、核心代碼:

@Slf4j
@Component
public class DpcInterceptor extends JsqlParserSupport implements InnerInterceptor {
ThreadLocal<String> mapperID = new ThreadLocal<String>();
ThreadLocal<String> methodID = new ThreadLocal<String>();

@Autowired
private RedisService redisService;
@Autowired
private Map<String, AbstractExpressionHandler> expressionHandlers;

@Override
public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
if (!InterceptorIgnoreHelper.willIgnoreDataPermission(ms.getId())) {
String[] ids = ms.getId().replace(".", ",").split(",");
mapperID.set(ids[ids.length - 2]);
methodID.set(ids[ids.length - 1]);
PluginUtils.MPBoundSql mpBs = PluginUtils.mpBoundSql(boundSql);
mpBs.sql(this.parserSingle(mpBs.sql(), ms.getId()));
}
}

@Override
protected void processSelect(Select select, int index, String sql, Object obj) {
Authentication authentication = AuthenticationHolder.get();
try {
if (!Objects.isNull(authentication)) {
Long userId = authentication.getUserId();
String json = redisService.getByKey(String.format("DATA:SCOPE:%s:%s:%s", userId, mapperID.get(), methodID.get()));
if (String_.isNotEmpty(json)) {
Expression where = null;
PlainSelect plainSelect = (PlainSelect) select.getSelectBody();
if ((where = plainSelect.getWhere()) == null) {
plainSelect.setWhere(new HexValue("1 = 1"));
}
StringBuffer sbWhere = new StringBuffer();
for (MenuRuleDto menuRule : Json_.toList(json, MenuRuleDto.class)) {
if (1 == menuRule.getType() && 1 == menuRule.getStatus()) {
String sqlWhere = menuRule.getCriteria();
for (String variablePoolName : menuRule.getVariablePool().split(",")) {
String whereSql = expressionHandlers.get(String_.lowerFirst(variablePoolName) + "ExpressionHandler").expression(sqlWhere);
if (whereSql.indexOf("$") < 0 && whereSql.indexOf("::") < 0) {
sbWhere.append(" and (" + whereSql + ") ");
}
}
}
}
plainSelect.setWhere(new AndExpression(new Parenthesis(where),new Column(sbWhere.substring(4).toString())));
}
}
} catch (Exception e) {
log.debug(e.getMessage());
} finally {
methodID.remove();
mapperID.remove();
}
}

@Slf4j
@Component
public class AuthenticationExpressionHandler extends AbstractExpressionHandler{
@Override
public String expression(String whereSql) {
Authentication currenUser = AuthenticationHolder.get();
if (currenUser != null) {
try {
Map<String,String> params = new HashMap<>();
for (Field declaredField : currenUser.getClass().getDeclaredFields()) {
declaredField.setAccessible(true);
if (declaredField.get(currenUser) != null) {
params.put("current"+ String_.upperFirst(declaredField.getName()),declaredField.get(currenUser).toString());
}
}
StringSubstitutor stringSubstitutor = new StringSubstitutor(params);
return stringSubstitutor.replace(whereSql);
} catch (Exception e) {
log.debug("[AuthenticationExpressionHandler 引數替換失敗]{}",e.getMessage(),e);
}
}
return whereSql;
}
}
/**
* 默認的運算式決議工具類,鑒權資料,請求資料作為引數
*/
@Component
public class DefaultExpressionHandler extends AbstractExpressionHandler{
@Override
public String expression(String whereSql) {
Map<String,String> params = ThreadLocal_.get("params");
StringSubstitutor stringSubstitutor = new StringSubstitutor(params);
return stringSubstitutor.replace(whereSql);
}
}
/**
* 運算式子決議處理介面
*/
public abstract class AbstractExpressionHandler {
public abstract String expression(String whereSql);
}
/**
* 默認的運算式決議工具類,字典資料作為引數
*/
@Component
public class DictionaryExpressionHandler extends AbstractExpressionHandler{

@Autowired
private RedisService redisService;
@Override
public String expression(String whereSql) {
Map<String,String> params = new HashMap<>();
for (String variable : getVariables(whereSql)) {
if (variable.indexOf("::")>0) {
String dictType = variable.substring(0,variable.indexOf("::"));
String dictCode = variable.substring(variable.indexOf("::")+2);
String dictJson = redisService.getByKey(RedisConst.DICT_DATA_KEY + dictType);
List<SysDictData> dictData = https://www.cnblogs.com/Eastern-Towns/p/Json_.toList(dictJson,SysDictData.class);
for (SysDictData dictDatum : dictData) {
if (dictDatum.getDictLabel().equals(dictCode)) {
params.put(variable,dictDatum.getDictValue());
}
}
}
}
StringSubstitutor stringSubstitutor = new StringSubstitutor(params);
return stringSubstitutor.replace(whereSql);
}

public static Set<String> getVariables(String str) {
Set<String> variables = new HashSet<>();
Pattern pattern = Pattern.compile("\\$\\{([^}]+)}");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
variables.add(matcher.group(1));
}
return variables;
}
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/557161.html

標籤:架構設計

上一篇:重溫設計模式 --- 責任鏈模式

下一篇:返回列表

標籤雲
其他(162494) Python(38274) JavaScript(25532) Java(18294) C(15241) 區塊鏈(8275) C#(7972) AI(7469) 爪哇(7425) MySQL(7296) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5876) 数组(5741) R(5409) Linux(5347) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4616) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2439) ASP.NET(2404) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) HtmlCss(2002) .NET技术(1987) 功能(1967) Web開發(1951) C++(1942) python-3.x(1918) 弹簧靴(1913) xml(1889) PostgreSQL(1883) .NETCore(1863) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 面試突擊第一季,第二季,第三季

    第一季必考 https://www.bilibili.com/video/BV1FE411y79Y?from=search&seid=15921726601957489746 第二季分布式 https://www.bilibili.com/video/BV13f4y127ee/?spm_id_fro ......

    uj5u.com 2020-09-10 05:35:24 more
  • 第三單元作業總結

    1.前言 這應該是本學期最后一次寫作業總結了吧。總體來說,對作業的節奏也差不多掌握了,作業做起來的效率也更高了。雖然和之前的作業一樣,作業中都要用到新的知識,但是相比之前,更加懂得了如何利用工具以及資料。雖然之間卡過殼,但總體而言,這幾次作業還算完成的比較好。 2.作業程序總結 相比前兩個單元,此單 ......

    uj5u.com 2020-09-10 05:35:41 more
  • 北航OO(2020)第四單元博客作業暨課程總結博客

    北航OO(2020)第四單元博客作業暨課程總結博客 本單元作業的架構設計 在本單元中,由于UML圖具有比較清晰的樹形結構,因此我對其中需要進行查詢操作的元素進行了包裝,在樹的父節點中存盤所有孩子的參考。考慮到性能問題,我采用了快取機制,一次查詢后盡可能快取已經遍歷過的資訊,以減少遍歷次數。 本單元我 ......

    uj5u.com 2020-09-10 05:35:48 more
  • BUAA_OO_第四單元

    一、UML決議器設計 ? 先看下題目:第四單元實作一個基于JDK 8帶有效性檢查的UML(Unified Modeling Language)類圖,順序圖,狀態圖分析器 MyUmlInteraction,實際上我們要建立一個有向圖模型,UML中的物件(元素)可能與同級元素連接,也可與低級元素相連形成 ......

    uj5u.com 2020-09-10 05:35:54 more
  • 6.1邏輯運算子

    邏輯運算子 1. && 短路與 運算式1 && 運算式2 01.運算式1為true并且運算式2也為true 整體回傳為true 02.運算式1為false,將不會執行運算式2 整體回傳為false 03.只要有一個運算式為false 整體回傳為false 2. || 短路或 運算式1 || 運算式2 ......

    uj5u.com 2020-09-10 05:35:56 more
  • BUAAOO 第四單元 & 課程總結

    1. 第四單元:StarUml檔案決議 本單元采用了圖模型決議UML。 UML檔案可以抽象為圖、子圖、邊的邏輯結構。 在實作中,圖的節點包括類、介面、屬性,子圖包括狀態圖、順序圖等。 采用了三次遍歷UML元素的方法建圖,第一遍遍歷建點,第二、三次遍歷設定屬性、連邊,實作圖物件的初始化。這里借鑒了一些 ......

    uj5u.com 2020-09-10 05:36:06 more
  • 談談我對C# 多型的理解

    面向物件三要素:封裝、繼承、多型。 封裝和繼承,這兩個比較好理解,但要理解多型的話,可就稍微有點難度了。今天,我們就來講講多型的理解。 我們應該經常會看到面試題目:請談談對多型的理解。 其實呢,多型非常簡單,就一句話:呼叫同一種方法產生了不同的結果。 具體實作方式有三種。 一、多載 多載很簡單。 p ......

    uj5u.com 2020-09-10 05:36:09 more
  • Python 資料驅動工具:DDT

    背景 python 的unittest 沒有自帶資料驅動功能。 所以如果使用unittest,同時又想使用資料驅動,那么就可以使用DDT來完成。 DDT是 “Data-Driven Tests”的縮寫。 資料:http://ddt.readthedocs.io/en/latest/ 使用方法 dd. ......

    uj5u.com 2020-09-10 05:36:13 more
  • Python里面的xlrd模塊詳解

    那我就一下面積個問題對xlrd模塊進行學習一下: 1.什么是xlrd模塊? 2.為什么使用xlrd模塊? 3.怎樣使用xlrd模塊? 1.什么是xlrd模塊? ?python操作excel主要用到xlrd和xlwt這兩個庫,即xlrd是讀excel,xlwt是寫excel的庫。 今天就先來說一下xl ......

    uj5u.com 2020-09-10 05:36:28 more
  • 當我們創建HashMap時,底層到底做了什么?

    jdk1.7中的底層實作程序(底層基于陣列+鏈表) 在我們new HashMap()時,底層創建了默認長度為16的一維陣列Entry[ ] table。當我們呼叫map.put(key1,value1)方法向HashMap里添加資料的時候: 首先,呼叫key1所在類的hashCode()計算key1 ......

    uj5u.com 2020-09-10 05:36:38 more
最新发布
  • 資料權限解決方案

    一、表結構設計 二、實作思路 1.系統啟動時將字典資料加載到redis作為可選常量池,以及mapper.xml、dao、資料規則資訊加載到redis 2.用訪問時通過springmvc攔截器對用戶進行攔截獲取token然后通過RSA解密獲取用戶資訊,將用戶資訊,以及請求引數加入本地執行緒 3.myba ......

    uj5u.com 2023-07-13 08:31:21 more
  • 重溫設計模式 --- 責任鏈模式

    ## 引言 **責任鏈模式**是一種行為型設計模式,它通過一條由多個處理器組成的鏈來處理請求,每個處理器都有機會處理請求,如果一個處理器不能處理該請求,它會將請求傳遞給下一個處理器,直到請求被處理為止。 在實際應用中,責任鏈模式常用于處理請求的分發、事件處理等場景,它的優點在于可以動態地添加、洗掉處 ......

    uj5u.com 2023-07-12 08:50:29 more
  • 模板模式

    #### 模板模式的定義 其定義如下: Define the skeleton of an algorithm in an operation,deferring some steps to subclasses.Template Method lets subclasses redefine ce ......

    uj5u.com 2023-07-12 08:50:23 more
  • 服務端應用多級快取架構方案

    ## 一:場景 20w的QPS的場景下,服務端架構應如何設計? ## 二:常規解決方案 可使用分布式快取來抗,比如redis集群,6主6從,主提供讀寫,從作為備,不提供讀寫服務。1臺平均抗3w并發,還可以抗住,如果QPS達到100w,通過增加redis集群中的機器數量,可以擴展快取的容量和并發讀寫能 ......

    uj5u.com 2023-07-12 08:50:19 more
  • 重溫設計模式 --- 責任鏈模式

    ## 引言 **責任鏈模式**是一種行為型設計模式,它通過一條由多個處理器組成的鏈來處理請求,每個處理器都有機會處理請求,如果一個處理器不能處理該請求,它會將請求傳遞給下一個處理器,直到請求被處理為止。 在實際應用中,責任鏈模式常用于處理請求的分發、事件處理等場景,它的優點在于可以動態地添加、洗掉處 ......

    uj5u.com 2023-07-12 08:50:07 more
  • 模板模式

    #### 模板模式的定義 其定義如下: Define the skeleton of an algorithm in an operation,deferring some steps to subclasses.Template Method lets subclasses redefine ce ......

    uj5u.com 2023-07-12 08:50:01 more
  • 服務端應用多級快取架構方案

    ## 一:場景 20w的QPS的場景下,服務端架構應如何設計? ## 二:常規解決方案 可使用分布式快取來抗,比如redis集群,6主6從,主提供讀寫,從作為備,不提供讀寫服務。1臺平均抗3w并發,還可以抗住,如果QPS達到100w,通過增加redis集群中的機器數量,可以擴展快取的容量和并發讀寫能 ......

    uj5u.com 2023-07-12 08:49:37 more
  • 重溫設計模式 --- 原型模式

    ## 引言 **原型模式**是一種創建型設計模式,它允許我們創建一個物件的副本,而不需要顯式地使用建構式來創建一個新的物件。這種模式通常用于創建那些具有復雜創建程序或需要大量資源的物件。 在原型模式中,我們首先定義一個原型介面,該介面包含一個克隆方法。然后我們創建一個原型物件,該物件實作了原型介面 ......

    uj5u.com 2023-07-11 08:13:06 more
  • 重溫設計模式 --- 原型模式

    ## 引言 **原型模式**是一種創建型設計模式,它允許我們創建一個物件的副本,而不需要顯式地使用建構式來創建一個新的物件。這種模式通常用于創建那些具有復雜創建程序或需要大量資源的物件。 在原型模式中,我們首先定義一個原型介面,該介面包含一個克隆方法。然后我們創建一個原型物件,該物件實作了原型介面 ......

    uj5u.com 2023-07-11 08:12:52 more
  • 讀發布!設計與部署穩定的分布式系統(第2版)筆記22_實體層之日志

    ![](https://img2023.cnblogs.com/blog/3076680/202307/3076680-20230704152811386-132747394.png) # 1. 記錄日志 ## 1.1. 傳統的日志檔案仍然是最可靠和最靈活的資訊載體 ## 1.2. 日志檔案反映應用 ......

    uj5u.com 2023-07-10 08:11:00 more