于俊龙 2 weeks ago
parent
commit
86d6ce124a

+ 81 - 12
farm-common/src/main/java/jp/yamoto/farm/common/utils/DecryptUtils.java

@@ -1,8 +1,16 @@
 package jp.yamoto.farm.common.utils;
 package jp.yamoto.farm.common.utils;
 
 
 
 
+import jp.yamoto.farm.common.config.AppConfig;
+import jp.yamoto.farm.common.constant.Constants;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
 
 
+import javax.crypto.Cipher;
+import javax.crypto.spec.IvParameterSpec;
+import javax.crypto.spec.SecretKeySpec;
+import java.security.*;
+import java.util.Base64;
+
 /**
 /**
  * デコードユーティリティクラス
  * デコードユーティリティクラス
  * <p>
  * <p>
@@ -13,6 +21,55 @@ import lombok.extern.slf4j.Slf4j;
 @Slf4j
 @Slf4j
 public class DecryptUtils {
 public class DecryptUtils {
 
 
+    private static final String AES_ALGORITHM = "AES";
+    private static final String AES_CBC_PKCS5_PADDING = "AES/CBC/PKCS5Padding";
+
+    /**
+     * AES 暗号化
+     */
+    public static String encryptAES(String data, String key) throws Exception {
+        byte[] keyBytes = Base64.getDecoder().decode(key);
+        SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, AES_ALGORITHM);
+
+        byte[] iv = new byte[16];
+        SecureRandom secureRandom = new SecureRandom();
+        secureRandom.nextBytes(iv);
+        IvParameterSpec ivSpec = new IvParameterSpec(iv);
+
+        Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5_PADDING);
+        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivSpec);
+
+        byte[] encrypted = cipher.doFinal(data.getBytes(Constants.UTF8));
+
+        byte[] combined = new byte[iv.length + encrypted.length];
+        System.arraycopy(iv, 0, combined, 0, iv.length);
+        System.arraycopy(encrypted, 0, combined, iv.length, encrypted.length);
+
+        return Base64.getEncoder().encodeToString(combined);
+    }
+
+    /**
+     * AES 暗号化されたテキストを復号します。
+     *
+     */
+    public static String decryptAES(String encryptedData, String key) throws Exception {
+        byte[] combined = Base64.getDecoder().decode(encryptedData);
+        byte[] keyBytes = Base64.getDecoder().decode(key);
+        SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, AES_ALGORITHM);
+
+        byte[] iv = new byte[16];
+        byte[] encrypted = new byte[combined.length - 16];
+        System.arraycopy(combined, 0, iv, 0, 16);
+        System.arraycopy(combined, 16, encrypted, 0, encrypted.length);
+
+        IvParameterSpec ivSpec = new IvParameterSpec(iv);
+        Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5_PADDING);
+        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivSpec);
+
+        byte[] decrypted = cipher.doFinal(encrypted);
+        return new String(decrypted, Constants.UTF8);
+    }
+
     /**
     /**
      * 暗号化されたテキストを復号します。
      * 暗号化されたテキストを復号します。
      *
      *
@@ -27,18 +84,21 @@ public class DecryptUtils {
      * 注意:復号中に例外が発生した場合、スタックトレースを出力し、空文字列を返します。
      * 注意:復号中に例外が発生した場合、スタックトレースを出力し、空文字列を返します。
      */
      */
     public static String decryptField(String encryptedText) {
     public static String decryptField(String encryptedText) {
-        return encryptedText;
-//        try {
-//            if (StringUtils.isEmpty(encryptedText)) {
-//                return encryptedText;
-//            }
-//
+        //return encryptedText;
+        try {
+            if (StringUtils.isEmpty(encryptedText)) {
+                return encryptedText;
+            }
+
 //            // URLセーフなBase64エンコードを標準Base64エンコードに変換
 //            // URLセーフなBase64エンコードを標準Base64エンコードに変換
 //            String base64Encoded = encryptedText.replace('-', '+').replace('_', '/');
 //            String base64Encoded = encryptedText.replace('-', '+').replace('_', '/');
 //            // Base64エンコードの長さを4の倍数にパディング
 //            // Base64エンコードの長さを4の倍数にパディング
 //            while (base64Encoded.length() % 4 != 0) {
 //            while (base64Encoded.length() % 4 != 0) {
 //                base64Encoded += "=";
 //                base64Encoded += "=";
 //            }
 //            }
+
+            return decryptAES(encryptedText, AppConfig.getDecryptFieldKey());
+
 //
 //
 //            // Base64文字列をデコード
 //            // Base64文字列をデコード
 //            byte[] encryptedBytes = Base64.getDecoder().decode(base64Encoded);
 //            byte[] encryptedBytes = Base64.getDecoder().decode(base64Encoded);
@@ -55,11 +115,20 @@ public class DecryptUtils {
 //            // 復号
 //            // 復号
 //            byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedString));
 //            byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedString));
 //            return new String(decryptedBytes);
 //            return new String(decryptedBytes);
-//        } catch (Exception e) {
-//            // 例外が発生した場合、スタックトレースを出力し、空文字列を返す
-//            e.printStackTrace();
-//            log.error("decryptField error: {} {}", encryptedText, e.getMessage());
-//            return encryptedText;
-//        }
+        } catch (Exception e) {
+            // 例外が発生した場合、スタックトレースを出力し、空文字列を返す
+            e.printStackTrace();
+            log.error("decryptField error: {} {}", encryptedText, e.getMessage());
+            return encryptedText;
+        }
+    }
+
+    public static void main(String[] args) throws Exception {
+        String key = "rtKDbersTvuTdyhbadsddd";
+        String s = DecryptUtils.encryptAES("abdafasdc@nextosd.com", key);
+
+        System.out.println(s);
+
+        System.out.println(DecryptUtils.decryptAES(s, key));
     }
     }
 }
 }

+ 2 - 1
farm-common/src/main/java/jp/yamoto/farm/common/validator/constraint/LmNotBlankValidator.java

@@ -3,6 +3,7 @@ package jp.yamoto.farm.common.validator.constraint;
 import jakarta.validation.ConstraintValidator;
 import jakarta.validation.ConstraintValidator;
 import jakarta.validation.ConstraintValidatorContext;
 import jakarta.validation.ConstraintValidatorContext;
 import jp.yamoto.farm.common.utils.StringUtils;
 import jp.yamoto.farm.common.utils.StringUtils;
+import jp.yamoto.farm.common.utils.ValueUtils;
 import jp.yamoto.farm.common.utils.spring.SpringUtils;
 import jp.yamoto.farm.common.utils.spring.SpringUtils;
 import jp.yamoto.farm.common.validator.annotation.LmNotBlank;
 import jp.yamoto.farm.common.validator.annotation.LmNotBlank;
 import jp.yamoto.farm.common.validator.service.IValidatorService;
 import jp.yamoto.farm.common.validator.service.IValidatorService;
@@ -27,7 +28,7 @@ public class LmNotBlankValidator implements ConstraintValidator<LmNotBlank, Obje
 
 
     @Override
     @Override
     public boolean isValid(Object value, ConstraintValidatorContext context) {
     public boolean isValid(Object value, ConstraintValidatorContext context) {
-        boolean isValid = !StringUtils.isObjectEmpty(value);
+        boolean isValid = !ValueUtils.isEmpty(value);
         if (!isValid) {
         if (!isValid) {
             String messageString = SpringUtils.getBean(IValidatorService.class).getErrorMessage(lmNotBlank.message(),
             String messageString = SpringUtils.getBean(IValidatorService.class).getErrorMessage(lmNotBlank.message(),
                     lmNotBlank.params());
                     lmNotBlank.params());

+ 2 - 1
farm-crm/src/main/java/jp/yamoto/farm/crm/web/controller/system/SysUserController.java

@@ -13,6 +13,7 @@ import jp.yamoto.farm.common.enums.BusinessType;
 import jp.yamoto.farm.common.utils.SecurityUtils;
 import jp.yamoto.farm.common.utils.SecurityUtils;
 import jp.yamoto.farm.common.utils.StringUtils;
 import jp.yamoto.farm.common.utils.StringUtils;
 import jp.yamoto.farm.common.utils.poi.ExcelUtil;
 import jp.yamoto.farm.common.utils.poi.ExcelUtil;
+import jp.yamoto.farm.common.validator.utils.ValidatorGroup;
 import jp.yamoto.farm.crm.biz.sys.service.ISysRoleService;
 import jp.yamoto.farm.crm.biz.sys.service.ISysRoleService;
 import jp.yamoto.farm.crm.biz.sys.service.ISysUserService;
 import jp.yamoto.farm.crm.biz.sys.service.ISysUserService;
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.commons.lang3.ArrayUtils;
@@ -111,7 +112,7 @@ public class SysUserController extends BaseController
     @PreAuthorize("@ss.hasPermi('system:user:add')")
     @PreAuthorize("@ss.hasPermi('system:user:add')")
     @Log(title = "ユーザー管理", businessType = BusinessType.INSERT)
     @Log(title = "ユーザー管理", businessType = BusinessType.INSERT)
     @PostMapping
     @PostMapping
-    public AjaxResult add(@Validated @RequestBody SysUser user)
+    public AjaxResult add(@RequestBody @Validated({ValidatorGroup.AddGroup.class}) SysUser user)
     {
     {
         roleService.checkRoleDataScope(user.getRoleIds());
         roleService.checkRoleDataScope(user.getRoleIds());
         if (!userService.checkUserNameUnique(user))
         if (!userService.checkUserNameUnique(user))