Skip to content

Commit a3aa61d

Browse files
committed
add springboot-starter-data-authorization
1 parent d485139 commit a3aa61d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+4430
-12
lines changed

example/example-application/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>springboot-example</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>3.3.26</version>
8+
<version>3.3.27.dev</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

example/example-domain/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>springboot-example</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>3.3.26</version>
8+
<version>3.3.27.dev</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

example/example-infra-flow/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>springboot-example</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>3.3.26</version>
8+
<version>3.3.27.dev</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

example/example-infra-jpa/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>springboot-example</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>3.3.26</version>
8+
<version>3.3.27.dev</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

example/example-server/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>springboot-example</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>3.3.26</version>
8+
<version>3.3.27.dev</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

example/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</parent>
1818

1919
<artifactId>springboot-example</artifactId>
20-
<version>3.3.26</version>
20+
<version>3.3.27.dev</version>
2121

2222
<name>springboot-example</name>
2323
<description>springboot-example project for Spring Boot</description>

pom.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
<groupId>com.codingapi.springboot</groupId>
1717
<artifactId>springboot-parent</artifactId>
18-
<version>3.3.26</version>
18+
<version>3.3.27.dev</version>
1919

2020
<url>https://github.com/codingapi/springboot-framewrok</url>
2121
<name>springboot-parent</name>
@@ -48,6 +48,7 @@
4848
<apache-groovy.version>4.0.24</apache-groovy.version>
4949
<h2.version>2.3.232</h2.version>
5050
<esotericsoftware.kryo.version>5.6.2</esotericsoftware.kryo.version>
51+
<jsqlparser.version>5.0</jsqlparser.version>
5152
</properties>
5253

5354
<dependencies>
@@ -209,6 +210,13 @@
209210
<version>${apache-groovy.version}</version>
210211
</dependency>
211212

213+
<dependency>
214+
<groupId>com.github.jsqlparser</groupId>
215+
<artifactId>jsqlparser</artifactId>
216+
<version>${jsqlparser.version}</version>
217+
</dependency>
218+
219+
212220
</dependencies>
213221
</dependencyManagement>
214222

springboot-starter-data-authorization/pom.xml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.codingapi.springboot</groupId>
88
<artifactId>springboot-parent</artifactId>
9-
<version>3.3.26</version>
9+
<version>3.3.27.dev</version>
1010
</parent>
1111

1212
<artifactId>springboot-starter-data-authorization</artifactId>
@@ -16,6 +16,26 @@
1616
<java.version>17</java.version>
1717
</properties>
1818

19+
<dependencies>
20+
<dependency>
21+
<groupId>com.github.jsqlparser</groupId>
22+
<artifactId>jsqlparser</artifactId>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-data-jpa</artifactId>
28+
<scope>test</scope>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>com.h2database</groupId>
33+
<artifactId>h2</artifactId>
34+
<scope>test</scope>
35+
</dependency>
36+
37+
</dependencies>
38+
1939

2040
<build>
2141
<plugins>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.codingapi.springboot.authorization;
2+
3+
4+
import com.codingapi.springboot.authorization.handler.ColumnHandler;
5+
import com.codingapi.springboot.authorization.handler.RowHandler;
6+
import com.codingapi.springboot.authorization.interceptor.SQLInterceptor;
7+
import com.codingapi.springboot.authorization.register.ConditionHandlerRegister;
8+
import com.codingapi.springboot.authorization.register.ResultSetHandlerRegister;
9+
import com.codingapi.springboot.authorization.register.SQLInterceptorRegister;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.context.annotation.Bean;
12+
import org.springframework.context.annotation.Configuration;
13+
14+
@Configuration
15+
public class DataAuthorizationConfiguration {
16+
17+
@Bean
18+
public ConditionHandlerRegister conditionHandlerRegister(@Autowired(required = false) RowHandler rowHandler) {
19+
return new ConditionHandlerRegister(rowHandler);
20+
}
21+
22+
@Bean
23+
public ResultSetHandlerRegister resultSetHandlerRegister(@Autowired(required = false) ColumnHandler columnHandler) {
24+
return new ResultSetHandlerRegister(columnHandler);
25+
}
26+
27+
@Bean
28+
public SQLInterceptorRegister sqlInterceptorRegister(@Autowired(required = false) SQLInterceptor sqlInterceptor) {
29+
return new SQLInterceptorRegister(sqlInterceptor);
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.codingapi.springboot.authorization.analyzer;
2+
3+
import com.codingapi.springboot.authorization.handler.Condition;
4+
import com.codingapi.springboot.authorization.handler.RowHandler;
5+
import net.sf.jsqlparser.expression.Expression;
6+
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
7+
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
8+
import net.sf.jsqlparser.schema.Table;
9+
import net.sf.jsqlparser.statement.Statement;
10+
import net.sf.jsqlparser.statement.select.FromItem;
11+
import net.sf.jsqlparser.statement.select.Join;
12+
import net.sf.jsqlparser.statement.select.PlainSelect;
13+
import net.sf.jsqlparser.statement.select.Select;
14+
15+
import java.sql.SQLException;
16+
17+
public class SelectSQLAnalyzer {
18+
19+
private final String sql;
20+
private final RowHandler rowHandler;
21+
22+
public SelectSQLAnalyzer(String sql, RowHandler rowHandler) {
23+
this.sql = sql;
24+
this.rowHandler = rowHandler;
25+
}
26+
27+
public String getNewSQL() throws SQLException {
28+
try {
29+
Statement statement = CCJSqlParserUtil.parse(sql);
30+
if (statement instanceof Select) {
31+
Select select = (Select) statement;
32+
PlainSelect plainSelect = select.getPlainSelect();
33+
this.processFromItems(plainSelect);
34+
return statement.toString();
35+
}
36+
} catch (Exception e) {
37+
throw new SQLException(e);
38+
}
39+
return sql;
40+
}
41+
42+
private void processFromItems(PlainSelect plainSelect) throws Exception {
43+
this.addConditionToSubSelect(plainSelect);
44+
45+
FromItem fromItem = plainSelect.getFromItem();
46+
47+
// 处理主 FROM 项(如果是子查询)
48+
if (fromItem instanceof Select) {
49+
this.addConditionToSubSelect((Select) fromItem);
50+
}
51+
52+
// 处理 JOIN 子查询
53+
if (plainSelect.getJoins() != null) {
54+
for (Join join : plainSelect.getJoins()) {
55+
if (join.getRightItem() instanceof Select) {
56+
this.addConditionToSubSelect((Select) join.getRightItem());
57+
}
58+
}
59+
}
60+
}
61+
62+
63+
private void addConditionToSubSelect(Select subSelect) throws Exception {
64+
PlainSelect selectBody = subSelect.getPlainSelect();
65+
if (selectBody != null) {
66+
// 获取 WHERE 子句
67+
Expression where = selectBody.getWhere();
68+
FromItem fromItem = selectBody.getFromItem();
69+
if (fromItem instanceof Table) {
70+
Table table = (Table) fromItem;
71+
String tableName = table.getName();
72+
String aliaName = table.getAlias() != null ? table.getAlias().getName() : null;
73+
Condition condition = rowHandler.handler(selectBody.toString(), tableName, aliaName);
74+
if (condition != null) {
75+
// 添加自定义条件
76+
Expression customExpression = CCJSqlParserUtil.parseCondExpression(condition.getCondition());
77+
if (where != null) {
78+
selectBody.setWhere(new AndExpression(customExpression, where));
79+
} else {
80+
selectBody.setWhere(customExpression);
81+
}
82+
}
83+
}
84+
}
85+
}
86+
87+
}

0 commit comments

Comments
 (0)