Java Web項(xiàng)目URL參數(shù)加密的終極解決方案
對(duì)于Java Web項(xiàng)目中傳遞的URL參數(shù),通常需要進(jìn)行加密以保護(hù)數(shù)據(jù)安全。本文將介紹一種終極解決方案來(lái)實(shí)現(xiàn)URL參數(shù)的加密。一、算法的選擇在對(duì)URL參數(shù)進(jìn)行加密時(shí),不建議使用RSA或者三重DES等復(fù)
對(duì)于Java Web項(xiàng)目中傳遞的URL參數(shù),通常需要進(jìn)行加密以保護(hù)數(shù)據(jù)安全。本文將介紹一種終極解決方案來(lái)實(shí)現(xiàn)URL參數(shù)的加密。
一、算法的選擇
在對(duì)URL參數(shù)進(jìn)行加密時(shí),不建議使用RSA或者三重DES等復(fù)雜的加密算法,因?yàn)檫@會(huì)影響性能和速度。建議使用對(duì)稱加密算法,如DES或PBE(Password Based Encryption)算法。本文將使用PBEWithMD5AndDES算法來(lái)實(shí)現(xiàn)加密。
二、加密原理
對(duì)于純文本,加密后會(huì)生成一串亂碼。為了避免亂碼中包含非法字符,我們需要對(duì)加密結(jié)果進(jìn)行Base64編碼。PBE算法需要一個(gè)口令(KEY),為了簡(jiǎn)化加密過(guò)程并提高頁(yè)面跳轉(zhuǎn)速度,我們將直接使用KEY作為口令。具體加密過(guò)程如下:
輸入口令(KEY) -> 加密文本 -> 使用Base64編碼加密結(jié)果 -> 使用方法編碼為瀏覽器可識(shí)別的形式 -> 傳輸給接收的Action。
解密過(guò)程如下:
接收的Action獲取參數(shù) -> 使用Base64解碼結(jié)果 -> 獲取純加密文本 -> 解密 -> 獲取解密后的值。
三、Base64實(shí)現(xiàn)
Base64編碼有很多實(shí)現(xiàn)方式,可以自己編寫(xiě),也可以使用內(nèi)置的base64編碼工具。需要引入activation.jar和mail.jar兩個(gè)包。具體實(shí)現(xiàn)如下:
```java
import ;
public class Base64Util {
public static String encode(String text) throws Exception {
return MimeUtility.encodeText(text, "utf-8", "B");
}
public static String decode(String text) throws Exception {
return (text);
}
}
```
四、加密解密工具類的實(shí)現(xiàn)
有了Base64工具類之后,實(shí)現(xiàn)加密解密工具類就變得簡(jiǎn)單了。下面是一個(gè)示例代碼:
```java
import ;
import ;
import ;
import ;
import ;
import ;
public class EncryptionUtils {
private final static int ITERATIONS 20;
private final static int SALT_LENGTH 12;
private final static String ALGORITHM "PBEWithMD5AndDES";
private final static String KEY "your_key";
public static String encrypt(String input) throws Exception {
byte[] salt new byte[SALT_LENGTH];
PBEParameterSpec parameterSpec new PBEParameterSpec(salt, ITERATIONS);
PBEKeySpec keySpec new PBEKeySpec(());
SecretKeyFactory keyFactory (ALGORITHM);
Cipher cipher (ALGORITHM);
(Cipher.ENCRYPT_MODE, (keySpec), parameterSpec);
byte[] encryptedData (());
return Base64Util.encode(new String(encryptedData));
}
public static String decrypt(String input) throws Exception {
byte[] salt new byte[SALT_LENGTH];
PBEParameterSpec parameterSpec new PBEParameterSpec(salt, ITERATIONS);
PBEKeySpec keySpec new PBEKeySpec(());
SecretKeyFactory keyFactory (ALGORITHM);
Cipher cipher (ALGORITHM);
(_MODE, (keySpec), parameterSpec);
byte[] decryptedData ((input).getBytes());
return new String(decryptedData);
}
}
```
五、在Struts Action中的使用
可以在Struts Action中直接調(diào)用加密解密工具類來(lái)處理加密和解密操作。例如:
```java
public class MyAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String encryptedValue EncryptionUtils.encrypt("123456");
("encryptedValue", encryptedValue);
return ("success");
}
}
```
六、在JSP頁(yè)面中的處理
在JSP頁(yè)面中,可以通過(guò)EL表達(dá)式獲取到加密后的參數(shù),并使用``方法進(jìn)行解碼。例如:
```jsp
<%
String encryptedValue (String) ("encryptedValue");
String decryptedValue (encryptedValue);
%>
Decrypted Value: <% decryptedValue %>
```
七、接收加密參數(shù)的Action中的解密
假設(shè)接收加密參數(shù)的Action為`QueryAction`,其中的`processDefId`和`processImgPath`是加密的參數(shù)??梢栽谠揂ction中直接調(diào)用解密方法進(jìn)行解密操作。例如:
```java
public class QueryAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String encryptedProcessDefId ("processDefId");
String encryptedProcessImgPath ("processImgPath");
String processDefId (encryptedProcessDefId);
String processImgPath (encryptedProcessImgPath);
// 其他業(yè)務(wù)邏輯處理
...
return ("success");
}
}
```
八、KEY(口令)的存放
將KEY存放在服務(wù)器端的properties文件中,可以使用Spring Jasypt來(lái)實(shí)現(xiàn)加密存儲(chǔ)。首先需要使用jasypt的encrypt工具對(duì)KEY進(jìn)行加密,并將加密后的值存放在properties文件中。例如:
```bash
encrypt inputmykey passwordsecret algorithmPBEWithMD5AndDES
```
然后將生成的亂碼復(fù)制到properties文件中,并在外層加上`ENC()`,如:
```
(OlO0LqELUuLOVreCtDngHaNgMcZWUyUg)
```
在工程部署的機(jī)器上設(shè)置一個(gè)環(huán)境變量,如:
```bash
set APP_ENCRYPTION_PASSWORDsecret
```
然后配置Spring,使其在加載工程時(shí)自動(dòng)解密。以下是詳細(xì)的配置內(nèi)容:
```xml
```
九、注意事項(xiàng)
如果使用MyEclipse進(jìn)行開(kāi)發(fā),在運(yùn)行時(shí)可能會(huì)出現(xiàn)`NoClassDefFoundError`錯(cuò)誤。這是因?yàn)閖ar包版本不統(tǒng)一導(dǎo)致的。解決方法是刪除`Java EE 5 Libraries/javaee.jar/mail`目錄中的包。具體步驟如下:用rar打開(kāi)`D:MyEclipse8.6GenuitecCommonplugins_datalibrarysetEE_5javaee.jar`,然后刪除`mail`目錄即可。