diff --git a/src/main/java/com/chint/domain/service/auth/AuthenticateServiceImpl.java b/src/main/java/com/chint/domain/service/auth/AuthenticateServiceImpl.java index 50eb2bd3..2e1d2d06 100644 --- a/src/main/java/com/chint/domain/service/auth/AuthenticateServiceImpl.java +++ b/src/main/java/com/chint/domain/service/auth/AuthenticateServiceImpl.java @@ -5,6 +5,7 @@ import com.chint.domain.factoriy.user.UserFactory; import com.chint.domain.repository.UserRepository; import com.chint.domain.value_object.UserLoginParam; import com.chint.domain.value_object.UserLoginResult; +import com.chint.infrastructure.util.Digest; import com.chint.infrastructure.util.JWTUtil; import com.chint.infrastructure.util.Json; import com.chint.infrastructure.util.Token; @@ -53,7 +54,8 @@ public class AuthenticateServiceImpl implements AuthenticateService { claims.put(USER_LOGIN_PARAM, json.gson().toJson(userLoginParam)); String jwt = JWTUtil.createJWT(claims); - return UserLoginResult.buildWithUser(user).loadToken(Token.of(jwt)); + return UserLoginResult.buildWithUser(user).loadToken(Token.of(jwt)).redirectUrl(SYSTEM_HOME_URL + + "?token=" + Digest.aes(jwt)); } else { User newUser = userFactory.create(userLoginParam.getSfno()); //如果数据库不存在该用户,需要通过sf信息进行查询并保存到数据库 diff --git a/src/main/java/com/chint/domain/value_object/UserLoginResult.java b/src/main/java/com/chint/domain/value_object/UserLoginResult.java index fe20ea8a..ba1d4fc7 100644 --- a/src/main/java/com/chint/domain/value_object/UserLoginResult.java +++ b/src/main/java/com/chint/domain/value_object/UserLoginResult.java @@ -2,10 +2,13 @@ package com.chint.domain.value_object; import com.chint.domain.aggregates.user.User; import com.chint.infrastructure.util.Token; +import lombok.Data; +@Data public class UserLoginResult { private User user; private Token token; + private String redirectUrl; public UserLoginResult(User user) { this.user = user; @@ -36,4 +39,9 @@ public class UserLoginResult { this.token = token; return this; } + + public UserLoginResult redirectUrl(String url) { + this.redirectUrl = url; + return this; + } } \ No newline at end of file diff --git a/src/main/java/com/chint/infrastructure/constant/Constant.java b/src/main/java/com/chint/infrastructure/constant/Constant.java index bf5aefb5..7454615a 100644 --- a/src/main/java/com/chint/infrastructure/constant/Constant.java +++ b/src/main/java/com/chint/infrastructure/constant/Constant.java @@ -202,4 +202,8 @@ public class Constant { public static final int CITY_TYPE_DOMESTIC = 0;//国内 public static final String CITY_TYPE_DOMESTIC_PATH = "3106_1_";//国内 public static final int CITY_TYPE_FOREIGN = 1;//国外 + + //system + public static final String SYSTEM_HOME_URL = "www.baidu.com";//国内 + public static final String AES_SECRET = "chint";//国内 } diff --git a/src/main/java/com/chint/infrastructure/util/Digest.java b/src/main/java/com/chint/infrastructure/util/Digest.java index 9ace4615..ee76ca45 100644 --- a/src/main/java/com/chint/infrastructure/util/Digest.java +++ b/src/main/java/com/chint/infrastructure/util/Digest.java @@ -1,7 +1,16 @@ package com.chint.infrastructure.util; +import javax.crypto.*; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.security.InvalidKeyException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.util.Base64; + +import static com.chint.infrastructure.constant.Constant.AES_SECRET; public class Digest { @@ -22,4 +31,44 @@ public class Digest { throw new RuntimeException(e); } } + + private static final SecretKey secretKey = generateKey(128); // 类级别的密钥 + + // 生成密钥 + private static SecretKey generateKey(int n) { + try { + KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); + keyGenerator.init(n); // 可以是 128, 192, 或 256 + return keyGenerator.generateKey(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public static String aes(String input) { + try { + Cipher cipher = Cipher.getInstance("AES"); + cipher.init(Cipher.ENCRYPT_MODE, secretKey); + byte[] encryptedBytes = cipher.doFinal(input.getBytes()); + + String encodedEncryptedToken = Base64.getEncoder().encodeToString(encryptedBytes); + return URLEncoder.encode(encodedEncryptedToken, StandardCharsets.UTF_8); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public static String aesBack(String input) { + try { + String urlDecodedToken = URLDecoder.decode(input, StandardCharsets.UTF_8); + byte[] encryptedBytes = Base64.getDecoder().decode(urlDecodedToken); + + Cipher cipher = Cipher.getInstance("AES"); + cipher.init(Cipher.DECRYPT_MODE, secretKey); + byte[] decryptedBytes = cipher.doFinal(encryptedBytes); + return new String(decryptedBytes); + } catch (Exception e) { + throw new RuntimeException(e); + } + } } diff --git a/src/main/java/com/chint/infrastructure/util/SignUtils.java b/src/main/java/com/chint/infrastructure/util/SignUtils.java index 107eebea..a369321d 100644 --- a/src/main/java/com/chint/infrastructure/util/SignUtils.java +++ b/src/main/java/com/chint/infrastructure/util/SignUtils.java @@ -42,7 +42,7 @@ public class SignUtils { for (int i = 0; i < bytes.length; i++) { temp = Integer.toHexString(bytes[i] & 0xFF); if (temp.length() == 1) { -//1得到一位的进行补0操作 + //1得到一位的进行补0操作 stringBuffer.append("0"); } stringBuffer.append(temp); diff --git a/src/test/java/com/chint/RouteApplicationTests.java b/src/test/java/com/chint/RouteApplicationTests.java index 299619c6..15024fc6 100644 --- a/src/test/java/com/chint/RouteApplicationTests.java +++ b/src/test/java/com/chint/RouteApplicationTests.java @@ -29,7 +29,7 @@ class RouteApplicationTests { userHttpRequest.loadUserInfo(user); } - //@Test + @Test void loginSign() { String sfno = "220322120"; String syscode = "abc";