Skip to content

Commit ac3e405

Browse files
committed
fix DataAuthorization
1 parent a4ca886 commit ac3e405

File tree

4 files changed

+46
-37
lines changed

4 files changed

+46
-37
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ public void clearDataAuthorizationFilters() {
3333

3434
public <T> T columnAuthorization(SQLInterceptState interceptState, String tableName, String columnName, T value) {
3535
if (interceptState != null && interceptState.hasIntercept()) {
36-
String realTableName = interceptState.getTableName(tableName);
36+
// String realTableName = interceptState.getTableName(tableName);
3737
for (DataAuthorizationFilter filter : filters) {
38-
if (filter.supportColumnAuthorization(realTableName, columnName, value)) {
39-
return filter.columnAuthorization(realTableName, columnName, value);
38+
if (filter.supportColumnAuthorization(tableName, columnName, value)) {
39+
return filter.columnAuthorization(tableName, columnName, value);
4040
}
4141
}
4242
}

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

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public String getNewSQL() throws SQLException {
4646
PlainSelect plainSelect = select.getPlainSelect();
4747

4848
this.enhanceDataPermissionInSelect(plainSelect);
49+
System.out.println(tableAlias);
4950
return statement.toString();
5051
}
5152
} catch (Exception e) {
@@ -54,27 +55,32 @@ public String getNewSQL() throws SQLException {
5455
return sql;
5556
}
5657

58+
5759
// 增强 SELECT 语句
5860
private void enhanceDataPermissionInSelect(PlainSelect plainSelect) throws Exception {
59-
this.applyDataPermissionToSubquery(plainSelect);
60-
6161
FromItem fromItem = plainSelect.getFromItem();
6262

63-
// 处理主 FROM 项(如果是子查询)
63+
// FROM 项是表
64+
if (fromItem instanceof Table) {
65+
Table table = (Table) fromItem;
66+
this.injectDataPermissionCondition(plainSelect, table, plainSelect.getWhere());
67+
}
68+
69+
// FROM是子查询
6470
if (fromItem instanceof Select) {
65-
this.applyDataPermissionToSubquery((Select) fromItem);
71+
PlainSelect subPlainSelect = ((Select) fromItem).getPlainSelect();
72+
this.enhanceDataPermissionInSelect(subPlainSelect);
6673
}
67-
Expression where = plainSelect.getWhere();
6874

6975
// 处理JOIN或关联子查询
7076
if (plainSelect.getJoins() != null) {
7177
for (Join join : plainSelect.getJoins()) {
7278
if (join.getRightItem() instanceof Select) {
73-
PlainSelect subPlainSelect = ((Select) join.getRightItem()).getPlainSelect();
79+
PlainSelect subPlainSelect = ((Select) join.getRightItem()).getPlainSelect();
7480
this.enhanceDataPermissionInSelect(subPlainSelect);
7581
}
76-
if(join.getRightItem() instanceof Table){
77-
injectDataPermissionCondition(plainSelect, (Table) join.getRightItem(), where);
82+
if (join.getRightItem() instanceof Table) {
83+
injectDataPermissionCondition(plainSelect, (Table) join.getRightItem(), plainSelect.getWhere());
7884
}
7985
}
8086
}
@@ -97,16 +103,4 @@ private void injectDataPermissionCondition(PlainSelect plainSelect, Table table,
97103
}
98104
}
99105

100-
// 处理子查询
101-
private void applyDataPermissionToSubquery(Select subSelect) throws Exception {
102-
PlainSelect selectBody = subSelect.getPlainSelect();
103-
if (selectBody != null) {
104-
// 获取 WHERE 子句
105-
Expression where = selectBody.getWhere();
106-
FromItem fromItem = selectBody.getFromItem();
107-
if (fromItem instanceof Table) {
108-
injectDataPermissionCondition(selectBody, (Table) fromItem, where);
109-
}
110-
}
111-
}
112106
}

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
import com.codingapi.springboot.authorization.repository.UnitRepository;
1616
import com.codingapi.springboot.authorization.repository.UserRepository;
1717
import lombok.extern.slf4j.Slf4j;
18+
import net.sf.jsqlparser.expression.Expression;
19+
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
20+
import net.sf.jsqlparser.schema.Column;
21+
import net.sf.jsqlparser.statement.Statement;
22+
import net.sf.jsqlparser.statement.select.PlainSelect;
23+
import net.sf.jsqlparser.statement.select.Select;
24+
import net.sf.jsqlparser.statement.select.SelectItem;
25+
import net.sf.jsqlparser.statement.select.SelectItemVisitor;
1826
import org.junit.jupiter.api.MethodOrderer;
1927
import org.junit.jupiter.api.Order;
2028
import org.junit.jupiter.api.Test;
@@ -27,6 +35,7 @@
2735
import org.springframework.test.annotation.Rollback;
2836

2937
import java.time.LocalDate;
38+
import java.util.HashMap;
3039
import java.util.List;
3140
import java.util.Map;
3241

@@ -216,6 +225,10 @@ public boolean supportColumnAuthorization(String tableName, String columnName, O
216225
@Order(3)
217226
void test3() {
218227

228+
unitRepository.deleteAll();
229+
departRepository.deleteAll();
230+
userRepository.deleteAll();
231+
219232
ColumnMaskContext.getInstance().addColumnMask(new IDCardMask());
220233
ColumnMaskContext.getInstance().addColumnMask(new PhoneMask());
221234
ColumnMaskContext.getInstance().addColumnMask(new BankCardMask());
@@ -266,9 +279,9 @@ public boolean supportColumnAuthorization(String tableName, String columnName, O
266279
}
267280

268281

269-
@Test
270-
@Order(4)
271-
void test4() {
282+
// @Test
283+
// @Order(4)
284+
void test4() throws Exception{
272285
String sql = "SELECT\n" +
273286
"\tUNYiV.id AS '历史工作经历编号',\n" +
274287
"\tUNYiV.company_name AS '历史工作单位',\n" +
@@ -311,7 +324,8 @@ void test4() {
311324
"\t\t\tt_league_employee AS dEj96,\n" +
312325
"\t\t\tt_league AS rnGD4 \n" +
313326
"\t\tWHERE\n" +
314-
"\t\t\tdEj96.employee_id = WXJj8.id \n" +
327+
"\t\t\trnGD4.id < 100 \n" +
328+
"\t\t\tAND dEj96.employee_id = WXJj8.id \n" +
315329
"\t\t\tAND dEj96.league_id = rnGD4.id \n" +
316330
"\t\t\tAND 1 = 1 \n" +
317331
"\t) AS owasH \n" +
@@ -321,6 +335,7 @@ void test4() {
321335
"\tAND owasH.任现职编号 = pehMS.id \n" +
322336
"\tAND 1 = 1";
323337

338+
324339
DataAuthorizationContext.getInstance().clearDataAuthorizationFilters();
325340
DataAuthorizationContext.getInstance().addDataAuthorizationFilter(new DefaultDataAuthorizationFilter() {
326341
@Override
@@ -330,7 +345,7 @@ public Condition rowAuthorization(String tableName, String tableAlias) {
330345

331346
@Override
332347
public <T> T columnAuthorization(String tableName, String columnName, T value) {
333-
System.out.println(tableName + " " + columnName + " " + value);
348+
System.out.println("tableName:" + tableName + ",columnName:" + columnName + ",value:" + value);
334349
return value;
335350
}
336351

Lines changed: 9 additions & 9 deletions
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)