新增密码传输解密服务
This commit is contained in:
@@ -80,3 +80,7 @@ spring:
|
||||
multi-statement-allow: true
|
||||
model:
|
||||
url: http://localhost:8080/rate/pricing/mock/invokeModel
|
||||
|
||||
security:
|
||||
password-transfer:
|
||||
key: "1234567890abcdef"
|
||||
|
||||
@@ -99,3 +99,7 @@ xss:
|
||||
excludes: /system/notice
|
||||
# 匹配链接
|
||||
urlPatterns: /system/*,/monitor/*,/tool/*
|
||||
|
||||
security:
|
||||
password-transfer:
|
||||
key: "1234567890abcdef"
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.ruoyi.framework.web.service;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.ruoyi.common.exception.ServiceException;
|
||||
|
||||
@Service
|
||||
public class PasswordTransferCryptoService
|
||||
{
|
||||
@Value("${security.password-transfer.key}")
|
||||
private String key;
|
||||
|
||||
public String decrypt(String cipherText)
|
||||
{
|
||||
try
|
||||
{
|
||||
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
||||
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES"));
|
||||
return new String(cipher.doFinal(Base64.getDecoder().decode(cipherText)), StandardCharsets.UTF_8);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new ServiceException("密码解密失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.ruoyi.framework.web.service;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import com.ruoyi.common.exception.ServiceException;
|
||||
|
||||
class PasswordTransferCryptoServiceTest
|
||||
{
|
||||
private static final String KEY = "1234567890abcdef";
|
||||
|
||||
private PasswordTransferCryptoService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp()
|
||||
{
|
||||
service = new PasswordTransferCryptoService();
|
||||
ReflectionTestUtils.setField(service, "key", KEY);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDecryptValidCipherText() throws Exception
|
||||
{
|
||||
String plain = service.decrypt(encrypt("admin123"));
|
||||
assertEquals("admin123", plain);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectInvalidCipherText()
|
||||
{
|
||||
assertThrows(ServiceException.class, () -> service.decrypt("not-base64"));
|
||||
}
|
||||
|
||||
private String encrypt(String plainText) throws Exception
|
||||
{
|
||||
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(KEY.getBytes(StandardCharsets.UTF_8), "AES"));
|
||||
return Base64.getEncoder().encodeToString(cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user