Skip to content

Commit 75e28a8

Browse files
authored
Merge pull request #8 from admin4j/dev
v0.9.5
2 parents 880b8cd + 98a83a2 commit 75e28a8

90 files changed

Lines changed: 3225 additions & 743 deletions

File tree

Some content is hidden

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

admin4j-common-spring-web/pom.xml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
66
<parent>
@@ -11,6 +11,7 @@
1111

1212
<groupId>com.admin4j.common</groupId>
1313
<artifactId>admin4j-common-spring-web</artifactId>
14+
<version>0.9.5</version>
1415
<description>与业务无关的工具类库</description>
1516

1617
<properties>
@@ -20,9 +21,16 @@
2021
</properties>
2122

2223
<dependencies>
24+
25+
<dependency>
26+
<groupId>com.alibaba</groupId>
27+
<artifactId>transmittable-thread-local</artifactId>
28+
<scope>provided</scope>
29+
</dependency>
2330
<dependency>
2431
<groupId>org.springframework.boot</groupId>
2532
<artifactId>spring-boot</artifactId>
33+
<scope>provided</scope>
2634
</dependency>
2735
<dependency>
2836
<groupId>io.swagger</groupId>
@@ -71,5 +79,15 @@
7179
<artifactId>hibernate-validator</artifactId>
7280
<scope>provided</scope>
7381
</dependency>
82+
<dependency>
83+
<groupId>org.springframework.boot</groupId>
84+
<artifactId>spring-boot-autoconfigure</artifactId>
85+
<scope>provided</scope>
86+
</dependency>
87+
<dependency>
88+
<groupId>org.springframework.boot</groupId>
89+
<artifactId>spring-boot-autoconfigure-processor</artifactId>
90+
<scope>provided</scope>
91+
</dependency>
7492
</dependencies>
7593
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.admin4j.common.config;
2+
3+
import com.admin4j.common.constant.WebConstant;
4+
import com.admin4j.common.service.IUserContextHolder;
5+
import com.admin4j.common.service.impl.SimpleUserContextHolder;
6+
import com.admin4j.common.util.UserContextUtil;
7+
import org.springframework.beans.BeansException;
8+
import org.springframework.beans.factory.InitializingBean;
9+
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
10+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
11+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
12+
import org.springframework.context.ApplicationContext;
13+
import org.springframework.context.ApplicationContextAware;
14+
import org.springframework.context.annotation.Bean;
15+
import org.springframework.context.annotation.Configuration;
16+
17+
/**
18+
* @author andanyang
19+
* @since 2023/9/15 9:19
20+
*/
21+
@Configuration
22+
@AutoConfigureOrder(WebConstant.IUserContextHolderOrder + 6)
23+
public class UserContextAutoConfiguration implements InitializingBean, ApplicationContextAware {
24+
25+
private ApplicationContext applicationContext;
26+
27+
@Bean
28+
@ConditionalOnMissingBean(IUserContextHolder.class)
29+
@ConditionalOnClass(name = "com.alibaba.ttl.TransmittableThreadLocal")
30+
public IUserContextHolder userContextHolder() {
31+
return new SimpleUserContextHolder();
32+
}
33+
34+
@Override
35+
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
36+
this.applicationContext = applicationContext;
37+
}
38+
39+
@Override
40+
public void afterPropertiesSet() throws Exception {
41+
UserContextUtil.userContextHolder = applicationContext.getBean(IUserContextHolder.class);
42+
}
43+
}

admin4j-common-spring-web/src/main/java/com/admin4j/common/config/UserContextConfig.java

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.admin4j.common.constant;
2+
3+
/**
4+
* @author andanyang
5+
* @since 2023/12/1 11:18
6+
*/
7+
public final class WebConstant {
8+
9+
/**
10+
* IUserContextHolder 接口再spring中初始化顺序
11+
*/
12+
public static final int IUserContextHolderOrder = -1;
13+
}

admin4j-common-spring-web/src/main/java/com/admin4j/common/pojo/AuthenticationUser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import lombok.Data;
66

77
import java.io.Serializable;
8-
import java.util.Set;
8+
import java.util.Collection;
99

1010
/**
1111
* UserContext 用户上下文
@@ -35,7 +35,7 @@ public class AuthenticationUser implements Serializable {
3535
* 权限列表
3636
*/
3737
@ApiModelProperty("权限code列表")
38-
private Set<String> permissions;
38+
private Collection<String> permissions;
3939

4040
// private String fromService;
4141

web-spring-boot-starter/src/main/java/com/admin4j/framework/web/SimpleUserContextHolder.java renamed to admin4j-common-spring-web/src/main/java/com/admin4j/common/service/impl/SimpleUserContextHolder.java

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.admin4j.framework.web;
1+
package com.admin4j.common.service.impl;
22

33
import com.admin4j.common.pojo.AuthenticationUser;
44
import com.admin4j.common.pojo.ResponseEnum;
@@ -13,7 +13,6 @@
1313
* @author andanyang
1414
* @since 2021/7/27 10:56
1515
*/
16-
1716
public class SimpleUserContextHolder implements IUserContextHolder {
1817

1918
/**
@@ -29,6 +28,11 @@ public void loginOut() {
2928
clear();
3029
}
3130

31+
@Override
32+
public AuthenticationUser getAuthenticationUser() {
33+
return THREAD_LOCAL_USER.get();
34+
}
35+
3236
/**
3337
* 设置登录者信息
3438
*
@@ -39,11 +43,6 @@ public void setAuthenticationUser(AuthenticationUser authenticationUser) {
3943
THREAD_LOCAL_USER.set(authenticationUser);
4044
}
4145

42-
@Override
43-
public AuthenticationUser getAuthenticationUser() {
44-
return THREAD_LOCAL_USER.get();
45-
}
46-
4746
/**
4847
* 获取用户
4948
*
@@ -90,6 +89,20 @@ public void offTenant() {
9089
setTenantId(0L);
9190
}
9291

92+
/**
93+
* get租户
94+
*/
95+
@Override
96+
public Long getTenantId() {
97+
AuthenticationUser loginUserNoCheck = getLoginUserNoCheck();
98+
// 小心三目表达式,NPE
99+
if (loginUserNoCheck == null) {
100+
return null;
101+
} else {
102+
return loginUserNoCheck.getTenantId();
103+
}
104+
}
105+
93106
/**
94107
* 设置租户
95108
*
@@ -101,12 +114,11 @@ public void setTenantId(Long tenant) {
101114
}
102115

103116
/**
104-
* get租户
117+
* get用户ID
105118
*/
106119
@Override
107-
public Long getTenantId() {
108-
AuthenticationUser loginUserNoCheck = getLoginUserNoCheck();
109-
return loginUserNoCheck == null ? 0L : loginUserNoCheck.getTenantId();
120+
public Long getUserId() {
121+
return getLoginUser().getUserId();
110122
}
111123

112124
/**
@@ -118,12 +130,4 @@ public Long getTenantId() {
118130
public void setUserId(Long userId) {
119131
getLoginUser().setUserId(userId);
120132
}
121-
122-
/**
123-
* get用户ID
124-
*/
125-
@Override
126-
public Long getUserId() {
127-
return getLoginUser().getUserId();
128-
}
129133
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
2-
com.admin4j.common.config.UserContextConfig
2+
com.admin4j.common.config.UserContextAutoConfiguration

admin4j-common-spring/src/main/java/com/admin4j/spring/util/SpelUtil.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,34 @@
2020
*/
2121
public class SpelUtil {
2222

23-
private SpelUtil() {
24-
}
25-
23+
/**
24+
* 是一种基于本地变量表的参数解析器,依赖于编译时的选项,可能无法在所有情况下获取参数名称
25+
*/
2626
private static final LocalVariableTableParameterNameDiscoverer U = new LocalVariableTableParameterNameDiscoverer();
27-
//使用SPEL进行key的解析
27+
// 使用SPEL进行key的解析
2828
private static final ExpressionParser EL_PARSER = new SpelExpressionParser();
2929

30+
private SpelUtil() {
31+
}
3032

33+
/**
34+
* el表达式解析
35+
*
36+
* @param spel 解析值
37+
* @param method 方法
38+
* @param args 参数
39+
* @return
40+
*/
3141
public static String parse(String spel, Method method, Object[] args) {
32-
//获取被拦截方法参数名列表(使用Spring支持类库)
42+
if (StringUtils.isBlank(spel)) {
43+
return null;
44+
}
45+
// 获取被拦截方法参数名列表(使用Spring支持类库)
3346
String[] paraNameArr = U.getParameterNames(method);
3447

35-
//SPEL上下文
48+
// SPEL上下文
3649
StandardEvaluationContext context = new StandardEvaluationContext();
37-
//把方法参数放入SPEL上下文中
50+
// 把方法参数放入SPEL上下文中
3851
if (paraNameArr != null) {
3952
for (int i = 0; i < paraNameArr.length; i++) {
4053
context.setVariable(paraNameArr[i], args[i]);
@@ -63,12 +76,12 @@ public static String parse(Object rootObject, String spel, Method method, Object
6376
} else if (!spel.contains("#")) {
6477
return parse(spel, method, args);
6578
}
66-
//获取被拦截方法参数名列表(使用Spring支持类库)
79+
// 获取被拦截方法参数名列表(使用Spring支持类库)
6780
String[] paraNameArr = U.getParameterNames(method);
6881

69-
//SPEL上下文
82+
// SPEL上下文
7083
StandardEvaluationContext context = new MethodBasedEvaluationContext(rootObject, method, args, U);
71-
//把方法参数放入SPEL上下文中
84+
// 把方法参数放入SPEL上下文中
7285
if (paraNameArr != null) {
7386
for (int i = 0; i < paraNameArr.length; i++) {
7487
context.setVariable(paraNameArr[i], args[i]);

admin4j-common/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
44
<parent>
@@ -23,6 +23,11 @@
2323
<groupId>org.apache.commons</groupId>
2424
<artifactId>commons-lang3</artifactId>
2525
</dependency>
26+
27+
<dependency>
28+
<groupId>commons-codec</groupId>
29+
<artifactId>commons-codec</artifactId>
30+
</dependency>
2631
<dependency>
2732
<groupId>org.slf4j</groupId>
2833
<artifactId>slf4j-api</artifactId>

admin4j-common/src/main/java/com/admin4j/common/exception/EncryptException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ public class EncryptException extends SystemException {
99
public EncryptException(String message, Throwable throwable) {
1010
super(message, throwable);
1111
}
12+
13+
public EncryptException(Throwable throwable) {
14+
super(throwable);
15+
}
1216
}

0 commit comments

Comments
 (0)