Skip to content

Commit 0d1cbc0

Browse files
committed
PermissionHelper
0 parents  commit 0d1cbc0

File tree

9 files changed

+329
-0
lines changed

9 files changed

+329
-0
lines changed

ReadMe.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
项目利用mybatis拦截器原理,实现基于threadlocal的sql条件拼接

pom.xml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.2.2.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.elijah</groupId>
12+
<artifactId>permissionhelper</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>permissionhelper</name>
15+
<description>Demo project for Spring Boot</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.mybatis.spring.boot</groupId>
24+
<artifactId>mybatis-spring-boot-starter</artifactId>
25+
<version>2.1.1</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-jdbc</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>mysql</groupId>
33+
<artifactId>mysql-connector-java</artifactId>
34+
<scope>runtime</scope>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.projectlombok</groupId>
38+
<artifactId>lombok</artifactId>
39+
<optional>true</optional>
40+
</dependency>
41+
<dependency>
42+
<groupId>com.github.jsqlparser</groupId>
43+
<artifactId>jsqlparser</artifactId>
44+
<version>3.1</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-test</artifactId>
49+
<scope>test</scope>
50+
<exclusions>
51+
<exclusion>
52+
<groupId>org.junit.vintage</groupId>
53+
<artifactId>junit-vintage-engine</artifactId>
54+
</exclusion>
55+
</exclusions>
56+
</dependency>
57+
</dependencies>
58+
59+
<build>
60+
<plugins>
61+
<plugin>
62+
<groupId>org.springframework.boot</groupId>
63+
<artifactId>spring-boot-maven-plugin</artifactId>
64+
</plugin>
65+
</plugins>
66+
</build>
67+
68+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.elijah.permissionhelper;
2+
3+
import org.apache.ibatis.annotations.Mapper;
4+
import org.apache.ibatis.annotations.Select;
5+
import org.mybatis.spring.annotation.MapperScan;
6+
7+
import java.util.Map;
8+
9+
/**
10+
* Description:
11+
*
12+
* @author elijahliu
13+
* @Note Talk is cheap,just show me ur code.- -!
14+
* ProjectName:permissionhelper
15+
* PackageName: com.elijah.permissionhelper
16+
* Date: 2019/12/9 11:13
17+
*/
18+
@Mapper
19+
public interface EhrNewbornDao {
20+
21+
@Select("select * from ehr_newborn a ,ehr_newborn_result b where a.sampleid=b.sampleid and a.enable=1 limit 1;")
22+
Map selectOne();
23+
24+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.elijah.permissionhelper;
2+
3+
import java.security.Permission;
4+
5+
/**
6+
* Description:
7+
*
8+
* @author elijahliu
9+
* @Note Talk is cheap,just show me ur code.- -!
10+
* ProjectName:permissionhelper
11+
* PackageName: com.elijah.permissionhelper
12+
* Date: 2019/12/9 15:36
13+
*/
14+
public class PermissionHelper {
15+
private static final ThreadLocal<PermissionObject> permissionObjectThreadLocal = new ThreadLocal<>();
16+
17+
public static void doPermissionCheck(Object permissionKey,Object permissionValue){
18+
PermissionObject permissionObject = new PermissionObject();
19+
permissionObject.setPermissionKey(permissionKey);
20+
permissionObject.setPermissionValue(permissionValue);
21+
permissionObject.setPermission(Boolean.TRUE);
22+
permissionObjectThreadLocal.set(permissionObject);
23+
}
24+
25+
public static PermissionObject getLocalPermission() {
26+
if (permissionObjectThreadLocal.get() == null) {
27+
permissionObjectThreadLocal.set(new PermissionObject());
28+
}
29+
return permissionObjectThreadLocal.get();
30+
}
31+
public static void reset(){
32+
permissionObjectThreadLocal.set(null);
33+
}
34+
35+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.elijah.permissionhelper;
2+
3+
import net.sf.jsqlparser.expression.Expression;
4+
import net.sf.jsqlparser.expression.StringValue;
5+
import net.sf.jsqlparser.expression.WhenClause;
6+
import net.sf.jsqlparser.expression.operators.arithmetic.Addition;
7+
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
8+
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
9+
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
10+
import net.sf.jsqlparser.schema.Column;
11+
import net.sf.jsqlparser.statement.select.PlainSelect;
12+
import net.sf.jsqlparser.statement.select.Select;
13+
import net.sf.jsqlparser.util.SelectUtils;
14+
import org.apache.ibatis.executor.statement.StatementHandler;
15+
import org.apache.ibatis.mapping.MappedStatement;
16+
import org.apache.ibatis.plugin.*;
17+
import org.apache.ibatis.reflection.MetaObject;
18+
import org.apache.ibatis.reflection.SystemMetaObject;
19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
21+
import org.springframework.stereotype.Component;
22+
23+
import java.sql.Connection;
24+
import java.util.Properties;
25+
26+
/**
27+
* Description:
28+
*
29+
* @author elijahliu
30+
* @Note Talk is cheap,just show me ur code.- -!
31+
* ProjectName:permissionhelper
32+
* PackageName: com.elijah.permissionhelper
33+
* Date: 2019/12/9 12:21
34+
*/
35+
@Component
36+
@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
37+
public class PermissionInterceptor implements Interceptor {
38+
private static final Logger logger = LoggerFactory.getLogger(PermissionInterceptor.class);
39+
40+
@Override
41+
public Object intercept(Invocation invocation) throws Throwable {
42+
PermissionObject permissionObject = PermissionHelper.getLocalPermission();
43+
if (!permissionObject.getPermission()) {
44+
logger.info("current thread no need to check permission thread:{} permission:{}", Thread.currentThread().getName(), permissionObject);
45+
return invocation.proceed();
46+
} else {
47+
PermissionHelper.reset();
48+
}
49+
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
50+
MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
51+
String sql = (String) metaObject.getValue("delegate.boundSql.sql");
52+
Select select = (Select) CCJSqlParserUtil.parse(sql);
53+
PlainSelect selectBody = (PlainSelect) select.getSelectBody();
54+
Expression whereExpression = selectBody.getWhere();
55+
StringBuilder tableName;
56+
Boolean hasTableAlias = Boolean.FALSE;
57+
if (selectBody.getFromItem().getAlias() != null) {
58+
hasTableAlias = Boolean.TRUE;
59+
tableName = new StringBuilder(selectBody.getFromItem().getAlias().getName());
60+
} else {
61+
tableName = new StringBuilder(selectBody.getFromItem().toString());
62+
}
63+
EqualsTo equalsTo = new EqualsTo();
64+
if (hasTableAlias) {
65+
equalsTo.setLeftExpression(new Column(tableName.append(".").append(permissionObject.getPermissionKey()).toString()));
66+
} else {
67+
equalsTo.setLeftExpression(new Column(permissionObject.getPermissionKey().toString()));
68+
}
69+
equalsTo.setRightExpression(new StringValue(permissionObject.getPermissionValue().toString()));
70+
if (whereExpression != null) {
71+
AndExpression andExpression = new AndExpression(whereExpression, equalsTo);
72+
whereExpression = andExpression;
73+
} else {
74+
whereExpression = equalsTo;
75+
}
76+
selectBody.setWhere(whereExpression);
77+
metaObject.setValue("delegate.boundSql.sql", select.toString());
78+
return invocation.proceed();
79+
}
80+
81+
@Override
82+
public Object plugin(Object target) {
83+
return Plugin.wrap(target, this);
84+
}
85+
86+
@Override
87+
public void setProperties(Properties properties) {
88+
89+
}
90+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.elijah.permissionhelper;
2+
3+
/**
4+
* Description:
5+
*
6+
* @author elijahliu
7+
* @Note Talk is cheap,just show me ur code.- -!
8+
* ProjectName:permissionhelper
9+
* PackageName: com.elijah.permissionhelper
10+
* Date: 2019/12/9 15:39
11+
*/
12+
public class PermissionObject {
13+
public PermissionObject(){
14+
this.isPermission = Boolean.FALSE;
15+
this.permissionKey = null;
16+
this.permissionValue = null;
17+
}
18+
private Object permissionKey;
19+
private Object permissionValue;
20+
private Boolean isPermission;
21+
22+
public Object getPermissionKey() {
23+
return permissionKey;
24+
}
25+
26+
public void setPermissionKey(Object permissionKey) {
27+
this.permissionKey = permissionKey;
28+
}
29+
30+
public Object getPermissionValue() {
31+
return permissionValue;
32+
}
33+
34+
public void setPermissionValue(Object permissionValue) {
35+
this.permissionValue = permissionValue;
36+
}
37+
38+
public Boolean getPermission() {
39+
return isPermission;
40+
}
41+
42+
public void setPermission(Boolean permission) {
43+
isPermission = permission;
44+
}
45+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.elijah.permissionhelper;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class PermissionhelperApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(PermissionhelperApplication.class, args);
11+
}
12+
13+
}

src/main/resources/application.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
spring:
2+
datasource:
3+
url: jdbc:mysql://172.16.10.23:3306/acme?characterEncoding=UTF-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useUnicode=true&allowMultiQueries=true
4+
driver-class-name: com.mysql.jdbc.Driver
5+
username: root
6+
password: biosan#17
7+
8+
logging:
9+
com:
10+
elijah:
11+
permissionhelper:
12+
level: debug
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.elijah.permissionhelper;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
9+
import java.util.Map;
10+
import java.util.concurrent.Callable;
11+
import java.util.concurrent.ExecutionException;
12+
import java.util.concurrent.FutureTask;
13+
14+
@SpringBootTest
15+
class PermissionhelperApplicationTests {
16+
17+
private static final Logger logger = LoggerFactory.getLogger(PermissionhelperApplicationTests.class);
18+
@Autowired
19+
EhrNewbornDao ehrNewbornDao;
20+
21+
22+
@Test
23+
void contextLoads() {
24+
}
25+
26+
@Test
27+
public void testSelectOne() throws ExecutionException, InterruptedException {
28+
FutureTask futureTask1 = new FutureTask(() -> {
29+
PermissionHelper.doPermissionCheck("centerid","bb717701-10de-47ee-b49e-693261c6df22");
30+
logger.info("frist time invoke:{}",ehrNewbornDao.selectOne().toString());
31+
logger.info("second time invoke:{}",ehrNewbornDao.selectOne().toString());
32+
return null;
33+
});
34+
FutureTask<Map> futureTask2 = new FutureTask(() -> ehrNewbornDao.selectOne());
35+
new Thread(futureTask1).start();
36+
new Thread(futureTask2).start();
37+
Object res1 = futureTask1.get();
38+
Object res2 = futureTask2.get();
39+
logger.info("res1:{}\nres2:{}", res1, res2);
40+
}
41+
}

0 commit comments

Comments
 (0)