|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+package com.huimv.employment.service.auth;
|
|
|
2
|
+
|
|
|
3
|
+import com.huimv.employment.common.exception.BizException;
|
|
|
4
|
+import com.huimv.employment.common.exception.ErrorCode;
|
|
|
5
|
+import com.huimv.employment.dao.entity.FeUser;
|
|
|
6
|
+import com.huimv.employment.dao.mapper.FeUserMapper;
|
|
|
7
|
+import org.springframework.stereotype.Service;
|
|
|
8
|
+
|
|
|
9
|
+/**
|
|
|
10
|
+ * JWT 鉴权后校验用户是否仍有效,并以数据库 {@code user_type} 为准。
|
|
|
11
|
+ */
|
|
|
12
|
+@Service
|
|
|
13
|
+public class AuthUserValidator {
|
|
|
14
|
+
|
|
|
15
|
+ private static final String STATUS_DISABLED = "disabled";
|
|
|
16
|
+
|
|
|
17
|
+ private final FeUserMapper feUserMapper;
|
|
|
18
|
+
|
|
|
19
|
+ public AuthUserValidator(FeUserMapper feUserMapper) {
|
|
|
20
|
+ this.feUserMapper = feUserMapper;
|
|
|
21
|
+ }
|
|
|
22
|
+
|
|
|
23
|
+ /**
|
|
|
24
|
+ * 校验用户存在且未禁用,返回库内真实身份类型。
|
|
|
25
|
+ */
|
|
|
26
|
+ public ValidatedUser validate(Long userId) {
|
|
|
27
|
+ FeUser user = feUserMapper.selectById(userId);
|
|
|
28
|
+ if (user == null) {
|
|
|
29
|
+ throw new BizException(ErrorCode.UNAUTHORIZED);
|
|
|
30
|
+ }
|
|
|
31
|
+ if (STATUS_DISABLED.equals(user.getStatus())) {
|
|
|
32
|
+ throw new BizException(ErrorCode.USER_DISABLED);
|
|
|
33
|
+ }
|
|
|
34
|
+ return new ValidatedUser(user.getId(), user.getUserType());
|
|
|
35
|
+ }
|
|
|
36
|
+
|
|
|
37
|
+ public static class ValidatedUser {
|
|
|
38
|
+ private final Long userId;
|
|
|
39
|
+ private final String userType;
|
|
|
40
|
+
|
|
|
41
|
+ public ValidatedUser(Long userId, String userType) {
|
|
|
42
|
+ this.userId = userId;
|
|
|
43
|
+ this.userType = userType;
|
|
|
44
|
+ }
|
|
|
45
|
+
|
|
|
46
|
+ public Long getUserId() {
|
|
|
47
|
+ return userId;
|
|
|
48
|
+ }
|
|
|
49
|
+
|
|
|
50
|
+ public String getUserType() {
|
|
|
51
|
+ return userType;
|
|
|
52
|
+ }
|
|
|
53
|
+ }
|
|
|
54
|
+}
|