Procházet zdrojové kódy

继续推进 userId 混淆

reghao před 11 měsíci
rodič
revize
814d7e0ad0

+ 1 - 1
account/account-api/src/main/java/cn/reghao/tnb/account/api/dto/AccountInfo.java

@@ -15,7 +15,7 @@ import java.io.Serializable;
 public class AccountInfo implements Serializable {
     private static final long serialVersionUID = 1L;
 
-    private Long userId;
+    private String userId;
     private String screenName;
     private String avatarUrl;
     private String username;

+ 17 - 0
account/account-service/src/main/java/cn/reghao/tnb/account/app/config/BeansConfig.java

@@ -0,0 +1,17 @@
+package cn.reghao.tnb.account.app.config;
+
+import cn.reghao.jutil.jdk.string.IDObfuscation;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * @author reghao
+ * @date 2025-04-02 20:26:54
+ */
+@Configuration
+public class BeansConfig {
+    @Bean
+    public IDObfuscation userIdObfuscation() {
+        return new IDObfuscation(0x12345);
+    }
+}

+ 7 - 2
account/account-service/src/main/java/cn/reghao/tnb/account/app/controller/AccountResourceController.java

@@ -1,5 +1,6 @@
 package cn.reghao.tnb.account.app.controller;
 
+import cn.reghao.jutil.jdk.string.IDObfuscation;
 import cn.reghao.jutil.web.WebResult;
 import cn.reghao.jutil.jdk.string.StringRegexp;
 import cn.reghao.tnb.account.api.dto.AccountInfo;
@@ -23,9 +24,11 @@ import org.springframework.web.bind.annotation.*;
 @RequestMapping("/api/account/resource")
 public class AccountResourceController {
     private final UserAccountMapper userAccountMapper;
+    private final IDObfuscation userIdObfuscation;
 
-    public AccountResourceController(UserAccountMapper userAccountMapper) {
+    public AccountResourceController(UserAccountMapper userAccountMapper, IDObfuscation userIdObfuscation) {
         this.userAccountMapper = userAccountMapper;
+        this.userIdObfuscation = userIdObfuscation;
     }
 
     @ApiOperation(value = "OAuth2 认证访问", notes = "N")
@@ -39,7 +42,9 @@ public class AccountResourceController {
             log.info("principal -> {}", principal);
             AccountInfo accountInfo = getAccountInfo(principal);
             if (accountInfo != null) {
-                OAuthAccountInfo oAuthAccountInfo = new OAuthAccountInfo(accountInfo.getUserId(), accountInfo.getScreenName());
+                String userIdStr = accountInfo.getUserId();
+                long userId = userIdObfuscation.restore(userIdStr);
+                OAuthAccountInfo oAuthAccountInfo = new OAuthAccountInfo(userId, accountInfo.getScreenName());
                 return WebResult.success(oAuthAccountInfo);
             }
         }

+ 3 - 3
account/account-service/src/main/java/cn/reghao/tnb/account/app/model/dto/AccountLoginRet.java

@@ -17,7 +17,7 @@ public class AccountLoginRet implements Serializable {
 
     private AccountInfo accountInfo;
     private AccountToken accountToken;
-    private long userId;
+    private String userId;
     private String sessId;
     private int plat;
     private String redirectPath;
@@ -32,8 +32,8 @@ public class AccountLoginRet implements Serializable {
         this.redirectPath = redirectPath;
     }
 
-    public AccountLoginRet(long userId, String loginId, int plat, String redirectPath) {
-        this.userId = userId;
+    public AccountLoginRet(String userIdStr, String loginId, int plat, String redirectPath) {
+        this.userId = userIdStr;
         this.sessId = loginId;
         this.plat = plat;
         this.redirectPath = redirectPath;

+ 6 - 2
account/account-service/src/main/java/cn/reghao/tnb/account/app/rpc/AccountQueryImpl.java

@@ -1,5 +1,6 @@
 package cn.reghao.tnb.account.app.rpc;
 
+import cn.reghao.jutil.jdk.string.IDObfuscation;
 import cn.reghao.tnb.account.api.dto.AccountAvatar;
 import cn.reghao.tnb.account.api.dto.AccountInfo;
 import cn.reghao.tnb.account.api.dto.AuthedAccount;
@@ -26,19 +27,22 @@ public class AccountQueryImpl implements AccountQuery {
     private final AccountRepository accountRepository;
     private final AccountRegistryServiceImpl accountRegistryService;
     private final AccountTokenService accountTokenService;
+    private final IDObfuscation userIdObfuscation;
 
     public AccountQueryImpl(AccountRepository accountRepository, AccountRegistryServiceImpl accountRegistryService,
-                            AccountTokenService accountTokenService) {
+                            AccountTokenService accountTokenService, IDObfuscation userIdObfuscation) {
         this.accountRepository = accountRepository;
         this.accountRegistryService = accountRegistryService;
         this.accountTokenService = accountTokenService;
+        this.userIdObfuscation = userIdObfuscation;
     }
 
     @Override
     public AuthedAccount getAuthedAccount(int type, String sessId) {
         AccountInfo accountInfo = accountTokenService.getAccountInfo(type, sessId);
         if (accountInfo != null) {
-            long userId = accountInfo.getUserId();
+            String userIdStr = accountInfo.getUserId();
+            long userId = userIdObfuscation.restore(userIdStr);
             UserAccount userAccount = accountRepository.getUserAccount(userId);
             String role = userAccount.getRole();
             return new AuthedAccount(userAccount.getUserId());

+ 7 - 2
account/account-service/src/main/java/cn/reghao/tnb/account/app/security/handler/AuthSuccessHandlerImpl.java

@@ -1,6 +1,7 @@
 package cn.reghao.tnb.account.app.security.handler;
 
 import cn.reghao.jutil.jdk.converter.DateTimeConverter;
+import cn.reghao.jutil.jdk.string.IDObfuscation;
 import cn.reghao.jutil.web.WebResult;
 import cn.reghao.jutil.web.ServletUtil;
 import cn.reghao.tnb.account.app.middleware.RabbitProducer;
@@ -40,13 +41,16 @@ public class AuthSuccessHandlerImpl implements AuthenticationSuccessHandler {
     private final LoginAttemptService loginAttemptService;
     private final UserAccountMapper userAccountMapper;
     private final AccountTokenService accountTokenService;
+    private final IDObfuscation userIdObfuscation;
 
     public AuthSuccessHandlerImpl(RabbitProducer rabbitProducer, LoginAttemptService loginAttemptService,
-                                  UserAccountMapper userAccountMapper, AccountTokenService accountTokenService) {
+                                  UserAccountMapper userAccountMapper, AccountTokenService accountTokenService,
+                                  IDObfuscation userIdObfuscation) {
         this.rabbitProducer = rabbitProducer;
         this.loginAttemptService = loginAttemptService;
         this.userAccountMapper = userAccountMapper;
         this.accountTokenService = accountTokenService;
+        this.userIdObfuscation = userIdObfuscation;
     }
 
     @Override
@@ -95,7 +99,8 @@ public class AuthSuccessHandlerImpl implements AuthenticationSuccessHandler {
         AccountLoginRet accountLoginRet = new AccountLoginRet(redirectPath);
         if (LoginPlat.web.getValue() == plat) {
             accountTokenService.setCookie(authToken, timeout);
-            accountLoginRet = new AccountLoginRet(userId, loginId, plat, redirectPath);
+            String userIdStr = userIdObfuscation.obfuscate(userId);
+            accountLoginRet = new AccountLoginRet(userIdStr, loginId, plat, redirectPath);
         } else if (LoginPlat.android.getValue() == plat) {
             AccountInfo accountInfo = userAccountMapper.findAccountInfo(userId);
             AccountToken accountToken = accountTokenService.grantUserToken(authToken);

+ 10 - 3
user/user-service/src/main/java/cn/reghao/tnb/user/app/service/ContactService.java

@@ -1,6 +1,7 @@
 package cn.reghao.tnb.user.app.service;
 
 import cn.reghao.jutil.jdk.result.Result;
+import cn.reghao.jutil.jdk.string.IDObfuscation;
 import cn.reghao.tnb.account.api.dto.AccountInfo;
 import cn.reghao.tnb.account.api.iface.AccountQuery;
 import cn.reghao.tnb.common.auth.UserContext;
@@ -33,18 +34,22 @@ public class ContactService {
     private final UserContactMapper userContactMapper;
     private final UserContactRecordMapper userContactRecordMapper;
     private final UserProfileService userProfileService;
+    private final IDObfuscation userIdObfuscation;
 
     public ContactService(UserProfileService userProfileService, UserContactMapper userContactMapper,
-                          UserContactRecordMapper userContactRecordMapper, UserMessageService userMessageService) {
+                          UserContactRecordMapper userContactRecordMapper, UserMessageService userMessageService,
+                          IDObfuscation userIdObfuscation) {
         this.userProfileService = userProfileService;
         this.userContactMapper = userContactMapper;
         this.userContactRecordMapper = userContactRecordMapper;
+        this.userIdObfuscation = userIdObfuscation;
     }
 
     public SearchContactResult searchUser(String mobile) {
         AccountInfo accountInfo = accountQuery.getByMobile(mobile);
         if (accountInfo != null) {
-            long userId = accountInfo.getUserId();
+            String userIdStr = accountInfo.getUserId();
+            long userId = userIdObfuscation.restore(userIdStr);
             UserInfo userInfo = userProfileService.getUserInfo(userId);
             return new SearchContactResult(userInfo);
         }
@@ -54,7 +59,9 @@ public class ContactService {
 
     public ContactInfoResult getContactInfoResult(long friendId) {
         AccountInfo accountInfo = accountQuery.getAccountInfo(friendId);
-        UserInfo userInfo = userProfileService.getUserInfo(accountInfo.getUserId());
+        String userIdStr = accountInfo.getUserId();
+        long userId = userIdObfuscation.restore(userIdStr);
+        UserInfo userInfo = userProfileService.getUserInfo(userId);
         ContactInfoResult contactInfoResult = new ContactInfoResult(userInfo);
 
         long loginUser = UserContext.getUser();

+ 7 - 2
user/user-service/src/main/java/cn/reghao/tnb/user/app/service/CrawledUserService.java

@@ -1,5 +1,6 @@
 package cn.reghao.tnb.user.app.service;
 
+import cn.reghao.jutil.jdk.string.IDObfuscation;
 import cn.reghao.tnb.account.api.dto.AccountInfo;
 import cn.reghao.tnb.account.api.dto.CrawledUser;
 import cn.reghao.tnb.account.api.iface.AccountQuery;
@@ -17,15 +18,19 @@ public class CrawledUserService {
     @DubboReference(check = false)
     private AccountQuery accountQuery;
     private final UserProfileMapper userProfileMapper;
+    private final IDObfuscation userIdObfuscation;
 
-    public CrawledUserService(UserProfileMapper userProfileMapper) {
+    public CrawledUserService(UserProfileMapper userProfileMapper, IDObfuscation userIdObfuscation) {
         this.userProfileMapper = userProfileMapper;
+        this.userIdObfuscation = userIdObfuscation;
     }
 
     public Long getUserId(String username) {
         AccountInfo accountInfo = accountQuery.getByUsername(username);
         if (accountInfo != null) {
-            return accountInfo.getUserId();
+            String userIdStr = accountInfo.getUserId();
+            long userId = userIdObfuscation.restore(userIdStr);
+            return userId;
         }
 
         return null;

+ 3 - 2
user/user-service/src/main/java/cn/reghao/tnb/user/app/service/UserProfileService.java

@@ -90,7 +90,8 @@ public class UserProfileService {
         List<AccountInfo> list = accountQuery.getAccountInfos(userIds);
         return list.stream()
                 .map(accountInfo -> {
-                    long userId = accountInfo.getUserId();
+                    String userIdStr = accountInfo.getUserId();
+                    long userId = userIdObfuscation.restore(userIdStr);
                     UserProfile userProfile = userProfileMapper.findByUserId(userId);
                     if (userProfile == null) {
                         userProfile = new UserProfile(userId);
@@ -102,7 +103,7 @@ public class UserProfileService {
                     int following = userProfile.getFollowing();
                     int follower = userProfile.getFollower();
                     boolean vip = userVipService.isVip(userId);
-                    String userIdStr = userIdObfuscation.obfuscate(userId);
+                    //String userIdStr = userIdObfuscation.obfuscate(userId);
                     return new UserInfo(accountInfo, userIdStr, gender, signature, following, follower, vip);
                 })
                 .collect(Collectors.toList());