Kaynağa Gözat

account-service 启动时创建默认的 admin 帐号(若不存在)

reghao 2 ay önce
ebeveyn
işleme
ba39e9223e

+ 5 - 1
account/account-service/src/main/java/cn/reghao/tnb/account/app/config/spring/SpringLifecycle.java

@@ -1,5 +1,6 @@
 package cn.reghao.tnb.account.app.config.spring;
 
+import cn.reghao.tnb.account.app.service.AccountRegistryService;
 import cn.reghao.tnb.account.app.service.PubkeyService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.DisposableBean;
@@ -15,14 +16,17 @@ import org.springframework.stereotype.Component;
 @Component
 public class SpringLifecycle implements ApplicationRunner, DisposableBean {
     private final PubkeyService pubkeyService;
+    private final AccountRegistryService accountRegistryService;
 
-    public SpringLifecycle(PubkeyService pubkeyService) {
+    public SpringLifecycle(PubkeyService pubkeyService, AccountRegistryService accountRegistryService) {
         this.pubkeyService = pubkeyService;
+        this.accountRegistryService = accountRegistryService;
     }
 
     @Override
     public void run(ApplicationArguments args) throws Exception {
         pubkeyService.getPrivateKey();
+        accountRegistryService.createAdminAccount();
         log.info("AccountService 启动...");
     }
 

+ 1 - 0
account/account-service/src/main/java/cn/reghao/tnb/account/app/service/AccountRegistryService.java

@@ -13,4 +13,5 @@ public interface AccountRegistryService {
     UserRegistry getUserRegistry();
     Result createAccount(AccountRegisterDto accountRegisterDto);
     UserAccount createAccount(String principal, String password);
+    void createAdminAccount();
 }

+ 20 - 7
account/account-service/src/main/java/cn/reghao/tnb/account/app/service/impl/AccountRegistryServiceImpl.java

@@ -3,6 +3,7 @@ package cn.reghao.tnb.account.app.service.impl;
 import cn.reghao.jutil.jdk.web.result.Result;
 import cn.reghao.jutil.jdk.web.result.ResultStatus;
 import cn.reghao.jutil.jdk.security.RandomString;
+import cn.reghao.tnb.account.api.dto.AccountInfo;
 import cn.reghao.tnb.common.util.StringRegexp;
 import cn.reghao.tnb.account.app.model.constant.VerifyChannel;
 import cn.reghao.tnb.account.app.model.dto.AccountRegisterDto;
@@ -86,13 +87,7 @@ public class AccountRegistryServiceImpl implements AccountRegistryService {
         long userId = accountRepository.getNextUserId();
         String salt = RandomString.getSalt(64);
         String encodedPassword = passwordEncoder.encode(decryptCredential + salt);
-        Set<UserAuthority> authorities;
-        if (userId == ConstantId.START_USER_ID) {
-            authorities = Set.of(new UserAuthority(AccountRole.admin.getValue()));
-        } else {
-            authorities = Set.of(new UserAuthority());
-        }
-
+        Set<UserAuthority> authorities = Set.of(new UserAuthority());
         if (StringRegexp.matchMobile(principal)) {
             UserAccount userAccount = new UserAccount(principal, userId, encodedPassword, salt, authorities);
             accountRepository.saveAccount(userAccount);
@@ -111,4 +106,22 @@ public class AccountRegistryServiceImpl implements AccountRegistryService {
         }
         throw new UsernameNotFoundException(String.format("%s is not mobile number", principal));
     }
+
+    @Override
+    public void createAdminAccount() {
+        long userId = accountRepository.getNextUserId();
+        if (userId == ConstantId.START_USER_ID) {
+            String principal = ConstantId.ADMIN_MOBILE;
+            String password = ConstantId.ADMIN_PASSWORD;
+            String salt = RandomString.getSalt(64);
+            String encodedPassword = passwordEncoder.encode(password + salt);
+            Set<UserAuthority> authorities = Set.of(new UserAuthority(AccountRole.admin.getValue()));
+
+            UserAccount userAccount = new UserAccount(principal, userId, encodedPassword, salt, authorities);
+            accountRepository.saveAccount(userAccount);
+            log.info("admin 帐号已创建");
+        } else {
+            log.info("admin 帐号已存在");
+        }
+    }
 }

+ 2 - 0
common/src/main/java/cn/reghao/tnb/common/util/ConstantId.java

@@ -17,4 +17,6 @@ public class ConstantId {
     public static final String RAW_PASSWORD = "UWA3kY.ltRAscO(KpZvC";
     public static final String ENCODED_PASSWORD = "dee544231c7faacd88aaacfc4c951c55";
     public static final String SALT = "nAyoy2hr+YCQKvQsUVwXptgwpqdc3N/0q+EXSbLKybKG2GhHsXx85ouuI8U9ubpAqV0tekj7pqQp1H4Phypfdw==";
+    public static final String ADMIN_MOBILE = "13012345678";
+    public static final String ADMIN_PASSWORD = "Admin12345678";
 }