Browse Source

加密解密错误处理

于俊龙 1 week ago
parent
commit
39335eb16a

+ 32 - 19
farm-common/src/main/java/jp/yamoto/farm/common/utils/DecryptUtils.java

@@ -3,6 +3,7 @@ package jp.yamoto.farm.common.utils;
 
 
 import jp.yamoto.farm.common.config.AppConfig;
 import jp.yamoto.farm.common.config.AppConfig;
 import jp.yamoto.farm.common.constant.Constants;
 import jp.yamoto.farm.common.constant.Constants;
+import jp.yamoto.farm.common.exception.ServiceException;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
 
 
 import javax.crypto.Cipher;
 import javax.crypto.Cipher;
@@ -27,37 +28,49 @@ public class DecryptUtils {
     /**
     /**
      * AES 暗号化
      * AES 暗号化
      */
      */
-    public static String encryptAES(String data) throws Exception {
-        byte[] keyBytes = hexStringToByteArray(AppConfig.getDecryptFieldKey());
-        byte[] ivBytes = hexStringToByteArray(AppConfig.getDecryptFieldIv());
+    public static String encryptAES(String data) {
+        try {
+            byte[] keyBytes = hexStringToByteArray(AppConfig.getDecryptFieldKey());
+            byte[] ivBytes = hexStringToByteArray(AppConfig.getDecryptFieldIv());
 
 
-        SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, AES_ALGORITHM);
-        IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
+            SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, AES_ALGORITHM);
+            IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
 
 
-        Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5_PADDING);
-        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
+            Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5_PADDING);
+            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
 
 
-        byte[] encrypted = cipher.doFinal(data.getBytes(Constants.UTF8));
-        return Base64.getEncoder().encodeToString(encrypted);
+            byte[] encrypted = cipher.doFinal(data.getBytes(Constants.UTF8));
+            return Base64.getEncoder().encodeToString(encrypted);
+        } catch (Exception e) {
+            // 例外が発生した場合、スタックトレースを出力し、空文字列を返す
+            log.error("decryptAES error: {} {}", data, e.getMessage());
+            throw new ServiceException(MessageUtils.message("EM007"));
+        }
     }
     }
 
 
     /**
     /**
      * AES 暗号化されたテキストを復号します。
      * AES 暗号化されたテキストを復号します。
      *
      *
      */
      */
-    public static String decryptAES(String encryptedData) throws Exception {
-        byte[] keyBytes = hexStringToByteArray(AppConfig.getDecryptFieldKey());
-        byte[] ivBytes = hexStringToByteArray(AppConfig.getDecryptFieldIv());
+    public static String decryptAES(String encryptedData) {
+        try {
+            byte[] keyBytes = hexStringToByteArray(AppConfig.getDecryptFieldKey());
+            byte[] ivBytes = hexStringToByteArray(AppConfig.getDecryptFieldIv());
 
 
-        SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, AES_ALGORITHM);
-        IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
+            SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, AES_ALGORITHM);
+            IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
 
 
-        Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5_PADDING);
-        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
+            Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5_PADDING);
+            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
 
 
-        byte[] encryptedBytes = Base64.getDecoder().decode(encryptedData);
-        byte[] decrypted = cipher.doFinal(encryptedBytes);
-        return new String(decrypted, Constants.UTF8);
+            byte[] encryptedBytes = Base64.getDecoder().decode(encryptedData);
+            byte[] decrypted = cipher.doFinal(encryptedBytes);
+            return new String(decrypted, Constants.UTF8);
+        } catch (Exception e) {
+            // 例外が発生した場合、スタックトレースを出力し、空文字列を返す
+            log.error("decryptAES error: {} {}", encryptedData, e.getMessage());
+            throw new ServiceException(MessageUtils.message("EM008"));
+        }
     }
     }
 
 
     /**
     /**

+ 2 - 0
farm-crm/src/main/resources/i18n/messages.properties

@@ -68,6 +68,8 @@ EM003={0}\u306F\u5FC5\u9808\u5165\u529B\u3067\u3059\u3002
 EM004={0}\u306F{1}\u306E\u307F\u3067\u5165\u529B\u3057\u3066\u304F\u3060\u3055\u3044\u3002
 EM004={0}\u306F{1}\u306E\u307F\u3067\u5165\u529B\u3057\u3066\u304F\u3060\u3055\u3044\u3002
 EM005={0}\u306F{1}\u6587\u5B57\u4EE5\u4E0B\u3067\u5165\u529B\u3057\u3066\u304F\u3060\u3055\u3044\u3002
 EM005={0}\u306F{1}\u6587\u5B57\u4EE5\u4E0B\u3067\u5165\u529B\u3057\u3066\u304F\u3060\u3055\u3044\u3002
 EM006={0}\u304C\u4E0D\u6B63\u3067\u3059\u3002
 EM006={0}\u304C\u4E0D\u6B63\u3067\u3059\u3002
+EM007=\u6697\u53F7\u5316\u30A8\u30E9\u30FC\u3002
+EM008=\u5FA9\u53F7\u30A8\u30E9\u30FC\u3002
 
 
 I0001=\u6210\u529F\u3057\u307E\u3057\u305F\u3002
 I0001=\u6210\u529F\u3057\u307E\u3057\u305F\u3002
 I0002=\u5931\u6557\u3057\u307E\u3057\u305F\u3002
 I0002=\u5931\u6557\u3057\u307E\u3057\u305F\u3002

+ 1 - 1
farm-sankin-biz/src/main/java/jp/yamoto/farm/sankin/biz/constants/SankinConstants.java

@@ -1,7 +1,7 @@
 package jp.yamoto.farm.sankin.biz.constants;
 package jp.yamoto.farm.sankin.biz.constants;
 
 
 /**
 /**
- * 農家支援システム 共通定数情報
+ * 農家支援システム 新・産直くん(仮称)共通定数情報
  */
  */
 public class SankinConstants {
 public class SankinConstants {
 }
 }

+ 1 - 0
farm-sankin/src/main/java/jp/yamoto/farm/sankin/web/controller/AuthController.java

@@ -94,6 +94,7 @@ public class AuthController
         UserInfoVo userInfoVo = new UserInfoVo();
         UserInfoVo userInfoVo = new UserInfoVo();
         userInfoVo.setUserName(user.getFarmerName());
         userInfoVo.setUserName(user.getFarmerName());
         userInfoVo.setUserId(user.getFarmerId());
         userInfoVo.setUserId(user.getFarmerId());
+        userInfoVo.setFirstLogin(user.getLoginCnt() == 1);
 
 
         AjaxResult ajax = AjaxResult.success(userInfoVo);
         AjaxResult ajax = AjaxResult.success(userInfoVo);
 
 

+ 2 - 0
farm-sankin/src/main/resources/i18n/messages.properties

@@ -68,6 +68,8 @@ EM003={0}\u306F\u5FC5\u9808\u5165\u529B\u3067\u3059\u3002
 EM004={0}\u306F{1}\u306E\u307F\u3067\u5165\u529B\u3057\u3066\u304F\u3060\u3055\u3044\u3002
 EM004={0}\u306F{1}\u306E\u307F\u3067\u5165\u529B\u3057\u3066\u304F\u3060\u3055\u3044\u3002
 EM005={0}\u306F{1}\u6587\u5B57\u4EE5\u4E0B\u3067\u5165\u529B\u3057\u3066\u304F\u3060\u3055\u3044\u3002
 EM005={0}\u306F{1}\u6587\u5B57\u4EE5\u4E0B\u3067\u5165\u529B\u3057\u3066\u304F\u3060\u3055\u3044\u3002
 EM006={0}\u304C\u4E0D\u6B63\u3067\u3059\u3002
 EM006={0}\u304C\u4E0D\u6B63\u3067\u3059\u3002
+EM007=\u6697\u53F7\u5316\u30A8\u30E9\u30FC\u3002
+EM008=\u5FA9\u53F7\u30A8\u30E9\u30FC\u3002
 
 
 I0001=\u6210\u529F\u3057\u307E\u3057\u305F\u3002
 I0001=\u6210\u529F\u3057\u307E\u3057\u305F\u3002
 I0002=\u5931\u6557\u3057\u307E\u3057\u305F\u3002
 I0002=\u5931\u6557\u3057\u307E\u3057\u305F\u3002