Skip to content

Commit eccd457

Browse files
committed
feat(security): SecurityUserContextHolder 代码优化
1 parent e43f0e9 commit eccd457

11 files changed

Lines changed: 93 additions & 199 deletions

File tree

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.3-SNAPSHOT</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.

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-dependencies/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@
215215
<dependency>
216216
<groupId>com.admin4j.common</groupId>
217217
<artifactId>admin4j-common-spring-web</artifactId>
218-
<version>${admin4j.version}</version>
218+
<version>0.9.3-SNAPSHOT</version>
219219
</dependency>
220220
<dependency>
221221
<groupId>com.admin4j.framework</groupId>

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
<module>enum-spring-boot-starter</module>
4444
</modules>
4545
<properties>
46-
<revision>0.9.0</revision>
47-
<admin4j-dependencies.version>0.9.2</admin4j-dependencies.version>
46+
<revision>0.9.3-SNAPSHOT</revision>
47+
<admin4j-dependencies.version>0.9.3-SNAPSHOT</admin4j-dependencies.version>
4848
<maven.compiler.source>8</maven.compiler.source>
4949
<maven.compiler.target>8</maven.compiler.target>
5050
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Lines changed: 2 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,13 @@
11
package com.admin4j.framework.security.context;
22

3-
import com.admin4j.common.pojo.AuthenticationUser;
4-
import com.admin4j.common.pojo.ResponseEnum;
5-
import com.admin4j.common.service.IUserContextHolder;
6-
import com.alibaba.ttl.TransmittableThreadLocal;
7-
import org.apache.commons.lang3.ObjectUtils;
3+
import com.admin4j.common.service.impl.SimpleUserContextHolder;
84

95
/**
106
* 当前登录用户上下文信息,可实现切换用户,切换租户。
117
*
128
* @author andanyang
139
* @since 2021/7/27 10:56
1410
*/
11+
public class SecurityUserContextHolder extends SimpleUserContextHolder {
1512

16-
public class SecurityUserContextHolder implements IUserContextHolder {
17-
18-
/**
19-
* 支持父子线程之间的数据传递 THREAD_LOCAL_TENANT
20-
*/
21-
private final ThreadLocal<AuthenticationUser> THREAD_LOCAL_USER = new TransmittableThreadLocal<>();
22-
23-
/**
24-
* 当前会话注销登录
25-
*/
26-
@Override
27-
public void loginOut() {
28-
clear();
29-
}
30-
31-
/**
32-
* 设置登录者信息
33-
*
34-
* @param authenticationUser 认证用户
35-
*/
36-
@Override
37-
public void setAuthenticationUser(AuthenticationUser authenticationUser) {
38-
THREAD_LOCAL_USER.set(authenticationUser);
39-
}
40-
41-
@Override
42-
public AuthenticationUser getAuthenticationUser() {
43-
return THREAD_LOCAL_USER.get();
44-
}
45-
46-
/**
47-
* 获取用户
48-
*
49-
* @return String
50-
*/
51-
public AuthenticationUser getLoginUser() {
52-
AuthenticationUser authenticationUser = THREAD_LOCAL_USER.get();
53-
54-
ResponseEnum.FAIL_AUTH_FORBIDDEN.notNull(authenticationUser);
55-
56-
return authenticationUser;
57-
}
58-
59-
/**
60-
* 获取用户
61-
*
62-
* @return String
63-
*/
64-
@Override
65-
public boolean isLogin() {
66-
AuthenticationUser authenticationUser = THREAD_LOCAL_USER.get();
67-
return ObjectUtils.isNotEmpty(authenticationUser);
68-
}
69-
70-
/**
71-
* 获取用户
72-
*
73-
* @return String
74-
*/
75-
public AuthenticationUser getLoginUserNoCheck() {
76-
return THREAD_LOCAL_USER.get();
77-
}
78-
79-
/**
80-
* 清除LOCAL
81-
*/
82-
@Override
83-
public void clear() {
84-
THREAD_LOCAL_USER.remove();
85-
}
86-
87-
@Override
88-
public void offTenant() {
89-
setTenantId(0L);
90-
}
91-
92-
/**
93-
* 设置租户
94-
*
95-
* @param tenant
96-
*/
97-
@Override
98-
public void setTenantId(Long tenant) {
99-
getLoginUser().setTenantId(tenant);
100-
}
101-
102-
/**
103-
* get租户
104-
*/
105-
@Override
106-
public Long getTenantId() {
107-
AuthenticationUser loginUserNoCheck = getLoginUserNoCheck();
108-
return loginUserNoCheck == null ? 0L : loginUserNoCheck.getTenantId();
109-
}
110-
111-
/**
112-
* 设置用户ID
113-
*
114-
* @param userId
115-
*/
116-
@Override
117-
public void setUserId(Long userId) {
118-
getLoginUser().setUserId(userId);
119-
}
120-
121-
/**
122-
* get用户ID
123-
*/
124-
@Override
125-
public Long getUserId() {
126-
return getLoginUser().getUserId();
127-
}
12813
}

web-spring-boot-starter/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@
1515
<version>0.9.3-SNAPSHOT</version>
1616

1717
<dependencies>
18-
<dependency>
19-
<groupId>com.alibaba</groupId>
20-
<artifactId>transmittable-thread-local</artifactId>
21-
<scope>provided</scope>
22-
</dependency>
2318
<dependency>
2419
<groupId>com.admin4j.common</groupId>
2520
<artifactId>admin4j-common</artifactId>

0 commit comments

Comments
 (0)