Skip to content

Commit db0b66f

Browse files
committed
add TableColumnAliasContext
1 parent ac3e405 commit db0b66f

File tree

5 files changed

+123
-17
lines changed

5 files changed

+123
-17
lines changed

springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/enhancer/DataPermissionSQLEnhancer.java

+56-5
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
import com.codingapi.springboot.authorization.handler.Condition;
55
import com.codingapi.springboot.authorization.handler.RowHandler;
66
import lombok.Getter;
7+
import net.sf.jsqlparser.expression.Alias;
78
import net.sf.jsqlparser.expression.Expression;
89
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
910
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
11+
import net.sf.jsqlparser.schema.Column;
1012
import net.sf.jsqlparser.schema.Table;
1113
import net.sf.jsqlparser.statement.Statement;
12-
import net.sf.jsqlparser.statement.select.FromItem;
13-
import net.sf.jsqlparser.statement.select.Join;
14-
import net.sf.jsqlparser.statement.select.PlainSelect;
15-
import net.sf.jsqlparser.statement.select.Select;
14+
import net.sf.jsqlparser.statement.select.*;
1615

1716
import java.sql.SQLException;
1817
import java.util.HashMap;
18+
import java.util.List;
1919
import java.util.Map;
2020

2121
/**
@@ -29,12 +29,15 @@ public class DataPermissionSQLEnhancer {
2929
@Getter
3030
private final Map<String, String> tableAlias;
3131

32+
private final TableColumnAliasContext aliasContext;
33+
3234
// 构造函数
3335
public DataPermissionSQLEnhancer(String sql, RowHandler rowHandler) {
3436
// 如何sql中存在? 则在?后面添加空格
3537
this.sql = sql.replaceAll("\\?", " ? ");
3638
this.rowHandler = rowHandler;
3739
this.tableAlias = new HashMap<>();
40+
this.aliasContext = new TableColumnAliasContext();
3841
}
3942

4043
// 获取增强后的SQL
@@ -44,8 +47,9 @@ public String getNewSQL() throws SQLException {
4447
if (statement instanceof Select) {
4548
Select select = (Select) statement;
4649
PlainSelect plainSelect = select.getPlainSelect();
47-
4850
this.enhanceDataPermissionInSelect(plainSelect);
51+
this.appendColumnAlias(plainSelect.getSelectItems());
52+
aliasContext.print();
4953
System.out.println(tableAlias);
5054
return statement.toString();
5155
}
@@ -63,11 +67,13 @@ private void enhanceDataPermissionInSelect(PlainSelect plainSelect) throws Excep
6367
// FROM 项是表
6468
if (fromItem instanceof Table) {
6569
Table table = (Table) fromItem;
70+
this.appendTableAlias(fromItem);
6671
this.injectDataPermissionCondition(plainSelect, table, plainSelect.getWhere());
6772
}
6873

6974
// FROM是子查询
7075
if (fromItem instanceof Select) {
76+
this.appendTableAlias(fromItem);
7177
PlainSelect subPlainSelect = ((Select) fromItem).getPlainSelect();
7278
this.enhanceDataPermissionInSelect(subPlainSelect);
7379
}
@@ -77,15 +83,60 @@ private void enhanceDataPermissionInSelect(PlainSelect plainSelect) throws Excep
7783
for (Join join : plainSelect.getJoins()) {
7884
if (join.getRightItem() instanceof Select) {
7985
PlainSelect subPlainSelect = ((Select) join.getRightItem()).getPlainSelect();
86+
this.appendTableAlias(join.getRightItem());
8087
this.enhanceDataPermissionInSelect(subPlainSelect);
8188
}
8289
if (join.getRightItem() instanceof Table) {
90+
this.appendTableAlias(join.getRightItem());
8391
injectDataPermissionCondition(plainSelect, (Table) join.getRightItem(), plainSelect.getWhere());
8492
}
8593
}
8694
}
8795
}
8896

97+
98+
private void appendTableAlias(FromItem fromItem) {
99+
if (fromItem instanceof Table) {
100+
Table table = (Table) fromItem;
101+
Alias alias = table.getAlias();
102+
String aliasName = alias.getName();
103+
aliasContext.add(aliasName, table.getName());
104+
}
105+
if (fromItem instanceof Select) {
106+
Select select = (Select) fromItem;
107+
PlainSelect plainSelect = select.getPlainSelect();
108+
this.appendTableAlias(plainSelect.getFromItem());
109+
List<Join> joins = plainSelect.getJoins();
110+
if (joins != null) {
111+
for (Join join : joins) {
112+
if (join.getRightItem() instanceof Table) {
113+
this.appendTableAlias(join.getRightItem());
114+
}
115+
if (join.getRightItem() instanceof Select) {
116+
this.appendTableAlias(join.getRightItem());
117+
}
118+
}
119+
}
120+
this.appendColumnAlias(plainSelect.getSelectItems());
121+
}
122+
123+
}
124+
125+
126+
private void appendColumnAlias(List<SelectItem<?>> selectItems) {
127+
if (selectItems != null) {
128+
for (SelectItem<?> selectItem : selectItems) {
129+
if (selectItem.getExpression() instanceof Column) {
130+
Column column = (Column) selectItem.getExpression();
131+
String aliasName = column.getTable().getName();
132+
String columnName = column.getColumnName();
133+
aliasContext.add(aliasName, columnName);
134+
}
135+
}
136+
}
137+
}
138+
139+
89140
// 注入数据权限条件
90141
private void injectDataPermissionCondition(PlainSelect plainSelect, Table table, Expression where) throws Exception {
91142
String tableName = table.getName();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.codingapi.springboot.authorization.enhancer;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class TableColumnAlias {
7+
8+
private final String aliasName;
9+
private final String columnName;
10+
11+
private Map<String,TableColumnAlias> children;
12+
13+
public String getKey(){
14+
return aliasName+"."+columnName;
15+
}
16+
17+
public TableColumnAlias(String aliasName, String columnName) {
18+
this.aliasName = aliasName;
19+
this.columnName = columnName;
20+
this.children = new HashMap<>();
21+
}
22+
23+
public TableColumnAlias add(String aliasName,String columnName){
24+
TableColumnAlias tableColumnAlias = new TableColumnAlias(aliasName,columnName);
25+
children.put(tableColumnAlias.getKey(),tableColumnAlias);
26+
return tableColumnAlias;
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.codingapi.springboot.authorization.enhancer;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class TableColumnAliasContext {
7+
8+
private final Map<String,TableColumnAlias> tableColumnAliasMap;
9+
10+
public TableColumnAliasContext() {
11+
this.tableColumnAliasMap = new HashMap<>();
12+
}
13+
14+
public TableColumnAlias add(String aliasName,String columnName){
15+
TableColumnAlias tableColumnAlias = new TableColumnAlias(aliasName,columnName);
16+
tableColumnAliasMap.put(tableColumnAlias.getKey(),tableColumnAlias);
17+
return tableColumnAlias;
18+
}
19+
20+
21+
public void print(){
22+
for(String key:tableColumnAliasMap.keySet()){
23+
TableColumnAlias tableColumnAlias = tableColumnAliasMap.get(key);
24+
System.out.println(tableColumnAlias.getKey());
25+
}
26+
}
27+
}

springboot-starter-data-authorization/src/test/java/com/codingapi/springboot/authorization/DataAuthorizationContextTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ public boolean supportColumnAuthorization(String tableName, String columnName, O
279279
}
280280

281281

282-
// @Test
283-
// @Order(4)
282+
@Test
283+
@Order(4)
284284
void test4() throws Exception{
285285
String sql = "SELECT\n" +
286286
"\tUNYiV.id AS '历史工作经历编号',\n" +
@@ -345,7 +345,7 @@ public Condition rowAuthorization(String tableName, String tableAlias) {
345345

346346
@Override
347347
public <T> T columnAuthorization(String tableName, String columnName, T value) {
348-
System.out.println("tableName:" + tableName + ",columnName:" + columnName + ",value:" + value);
348+
// System.out.println("tableName:" + tableName + ",columnName:" + columnName + ",value:" + value);
349349
return value;
350350
}
351351

Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11

2-
spring.datasource.driver-class-name=com.codingapi.springboot.authorization.jdbc.AuthorizationJdbcDriver
3-
spring.datasource.url=jdbc:h2:file:./test.db
4-
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
5-
spring.jpa.hibernate.ddl-auto=create-drop
6-
spring.jpa.show-sql=true
7-
82
#spring.datasource.driver-class-name=com.codingapi.springboot.authorization.jdbc.AuthorizationJdbcDriver
9-
#spring.datasource.url=jdbc:mysql://localhost:3306/example?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
10-
#spring.datasource.username=root
11-
#spring.datasource.password=lorne4j#2024
3+
#spring.datasource.url=jdbc:h2:file:./test.db
4+
#spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
5+
#spring.jpa.hibernate.ddl-auto=create-drop
6+
#spring.jpa.show-sql=true
7+
8+
spring.datasource.driver-class-name=com.codingapi.springboot.authorization.jdbc.AuthorizationJdbcDriver
9+
spring.datasource.url=jdbc:mysql://localhost:3306/example?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
10+
spring.datasource.username=root
11+
spring.datasource.password=lorne4j#2024
1212

1313
logging.level.com.codingapi.springboot.authorization=debug

0 commit comments

Comments
 (0)