jsp url 参数加密传送的终极解决方案

简介: 一般我们在form提交时不要担心我们的参数会显示给用户看。   我们主要解决的是以get方式进行url参数传递的问题,如:   http://xxx.xxx.xxx.xxx/xxx/xxx.do?pId=101&imageName=processimage.jpg这样的url pId=101&imageName=processimage.jgp这样的参数如何进行加密。

一般我们在form提交时不要担心我们的参数会显示给用户看。

 

我们主要解决的是以get方式进行url参数传递的问题,如:

 

http://xxx.xxx.xxx.xxx/xxx/xxx.do?pId=101&imageName=processimage.jpg这样的url pId=101&imageName=processimage.jgp这样的参数如何进行加密。

 

我们采用:

 

java.net.URLEncoder.encode(Base64编码(加密字串), StringCode) 这样的方法来对url中的参数进行加密。

 

首先我们先说一下如何加密。

 

一、算法的选择:

 

对于像对url中的参数进行加密的过程,我不建议使用rsa或者是三重des这样的加密算法,主要原因在于性能和速度会受影响。

 

我建议大家使用对称加密如:DES或者是PBE算法。

 

我们在这边就使用PBEWithMD5AndDES来实现加密。

 

二、加密原理

 

对于一个纯文本,加密后它会变成一堆乱码,这堆乱码包括了许多非法字符,我们不希望把这些字符放入bean中,因此在加密完后,我们还要对加密结果进行base64编码。

 

PBE从字面上理解,它必须使用一个口令,我们不希望我们的加密过于复杂而影响页面跳转的速度,因此我们不采用口令+KEY的形式,我们这边的口令就是我们的KEY

 

因此:

 

我们的整个加密过程实现如下:

 

输入口令(KEY)--> 加密文本 --> 以base64对加密后的结果进行编码-->以java.net.URLEncoder.encode编码成浏览器可以识别的形式-->传输给接受的action

 

而解密过程如下:

 

接受的action得到参数-->以base64对结果进行解码-->得到纯加密文本-->解密-->得到解密后的值

 

三、BASE64

这边对于BASE64的原理不多说了,只说实现,目前网上有很多实现方式,有自己写的,有用sun.misc.*的,我们在这个例子里将使用javax.mail.internet.MimeUtility自带的base64编码工具。

需要引入activation.jar和mail.jar两个包。 下面是具体的实现:

import javax.mail.internet.MimeUtility;

public class Base64 {
 public static byte[] encode(byte[] b) throws Exception {
  ByteArrayOutputStream baos = null;
  OutputStream b64os = null;
  try {
   baos = new ByteArrayOutputStream();
   b64os = MimeUtility.encode(baos, "base64");
   b64os.write(b);
   b64os.close();
   return baos.toByteArray();
  } catch (Exception e) {
   throw new Exception(e);
  } finally {
   try {
    if (baos != null) {
     baos.close();
     baos = null;
    }
   } catch (Exception e) {
   }
   try {
    if (b64os != null) {
     b64os.close();
     b64os = null;
    }
   } catch (Exception e) {
   }
  }
 }

 public static byte[] decode(byte[] b) throws Exception {
  ByteArrayInputStream bais = null;
  InputStream b64is = null;
  try {
   bais = new ByteArrayInputStream(b);
   b64is = MimeUtility.decode(bais, "base64");
   byte[] tmp = new byte[b.length];
   int n = b64is.read(tmp);
   byte[] res = new byte[n];
   System.arraycopy(tmp, 0, res, 0, n);

   return res;
  } catch (Exception e) {
   throw new Exception(e);
  } finally {
   try {
    if (bais != null) {
     bais.close();
     bais = null;
    }
   } catch (Exception e) {
   }
   try {
    if (b64is != null) {
     b64is.close();
     b64is = null;
    }
   } catch (Exception e) {
   }
  }
 }
}

 

四、加密解密工具类的实现

 

有了BASE64的工具类,下面的工作将变得简单了,编写我们的加密解密工具类吧:

 

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;

import java.util.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SecurityHelper {
 protected final static Log logger = LogFactory.getLog(SecurityHelper.class);
 private final static int ITERATIONS = 20;

 public static String encrypt(String key, String plainText) throws Exception {
  String encryptTxt = "";
  try {
   byte[] salt = new byte[8];
   MessageDigest md = MessageDigest.getInstance("MD5");
   md.update(key.getBytes());
   byte[] digest = md.digest();
   for (int i = 0; i < 8; i++) {
    salt[i] = digest[i];
   }
   PBEKeySpec pbeKeySpec = new PBEKeySpec(key.toCharArray());
   SecretKeyFactory keyFactory = SecretKeyFactory
     .getInstance("PBEWithMD5AndDES");
   SecretKey skey = keyFactory.generateSecret(pbeKeySpec);
   PBEParameterSpec paramSpec = new PBEParameterSpec(salt, ITERATIONS);
   Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
   cipher.init(Cipher.ENCRYPT_MODE, skey, paramSpec);
   byte[] cipherText = cipher.doFinal(plainText.getBytes());
   String saltString = new String(Base64.encode(salt));
   String ciphertextString = new String(Base64.encode(cipherText));
   return saltString + ciphertextString;
  } catch (Exception e) {
   throw new Exception("Encrypt Text Error:" + e.getMessage(), e);
  }
 }

 public static String decrypt(String key, String encryptTxt)
   throws Exception {
  int saltLength = 12;
  try {
   String salt = encryptTxt.substring(0, saltLength);
   String ciphertext = encryptTxt.substring(saltLength, encryptTxt
     .length());
   byte[] saltarray = Base64.decode(salt.getBytes());
   byte[] ciphertextArray = Base64.decode(ciphertext.getBytes());
   PBEKeySpec keySpec = new PBEKeySpec(key.toCharArray());
   SecretKeyFactory keyFactory = SecretKeyFactory
     .getInstance("PBEWithMD5AndDES");
   SecretKey skey = keyFactory.generateSecret(keySpec);
   PBEParameterSpec paramSpec = new PBEParameterSpec(saltarray,
     ITERATIONS);
   Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
   cipher.init(Cipher.DECRYPT_MODE, skey, paramSpec);
   byte[] plaintextArray = cipher.doFinal(ciphertextArray);
   return new String(plaintextArray);
  } catch (Exception e) {
   throw new Exception(e);
  }
 }

 

注意上面加粗的三处地方:

 private final static int ITERATIONS = 20;

 

 上面的值越大,加密越深,一般例子都以"Java安全性编程指南”这本书中的例子的值为准,设成1000,我们在这边只需要20就够了,原因就是考虑到加解密的速度问题。

 int saltLength = 12;
这是base64解码后的盐的长度,加密后再经BASE64编码后盐的长度为8,BASE64解码后盐的长度为12,至于为什么,这也是根据BASE64的原理得出的,具体可以看BASE64原理,网上很多,说得也都很简单。

 

PBEWithMD5AndDES

我们使用的是PBEWithMD5AndDES加密。

 

下面编写一个测试类

 public static void main(String[] args) {
  String encryptTxt = "";
  String plainTxt = "hello oh my god";
  try {
   System.out.println(plainTxt);
   encryptTxt = encrypt("mypassword01", plainTxt);
   plainTxt = decrypt("mypassword01", encryptTxt);
   System.out.println(encryptTxt);
   System.out.println(plainTxt);
  } catch (Exception e) {
   e.printStackTrace();
   System.exit(-1);
  }
 }

}

 

五、工具类在struts action中的具体使用

    MyTaskDTO taskDTO = new MyTaskDTO();
    TaskInstance ti = (TaskInstance) it.next();
    taskDTO.setTaskName(ti.getName());
    taskDTO.setTaskCreateDate(sd.format(ti.getCreate()));
    taskDTO.setTaskDescr(ti.getDescription());
    /* no encrypted data */
    String taskId = String.valueOf(ti.getId());
    String tokenId = String.valueOf(ti.getToken().getId());
    processImgName = PropertyUtil.getProperty(
      Constants.BPM_PROCESS_PAYMENT_PROCESSIMAGE).toString()
      + ".jpg";
    processDefId = String.valueOf(ti.getToken()
      .getProcessInstance().getProcessDefinition().getId());

    /* encrypted data */
    taskId = EncryptUrlPara.encrypt(taskId);
    tokenId = EncryptUrlPara.encrypt(tokenId);
    processImgName = EncryptUrlPara.encrypt(processImgName);
    processDefId = EncryptUrlPara.encrypt(processDefId);

    taskDTO.setTaskId(taskId);
    taskDTO.setTokenId(tokenId);
    taskDTO.setProcessDefinitionId(processDefId);
    taskDTO.setProcessImageName(processImgName);

 

六、jsp页面中的encode

把上述这个bean放入request中,带到下一个jsp页面中后,在jsp页面的处理如下:

    String processImgPath=taskDTO.getProcessImageName();
    String processDefId=taskDTO.getProcessDefinitionId();
    processImgPath=java.net.URLEncoder.encode(processImgPath,"UTF-8");
    processDefId=java.net.URLEncoder.encode(processDefId,"UTF-8");
    String showProcessImgUrl=request.getContextPath()+"/queryMyTask.do";

  <a href="<%=showProcessImgUrl%>?method=showProcessImg&processDefinitionId=<%=processDefId%>&processImgPath=<%=processImgPath%>" target="_blank"><u><span class="left_txt">查看当前进程</span></u></a>

七、在接受加密参数的action中对加密的值进行解密

我们假设我们的接受的action为: queryMyTask.do,它接受一系列的参数,基中,processDefId和processImgPath是加密的。

实现如下:

String processImgFilePath = "";
 String processDefinitionId = (String) request.getParameter("processDefinitionId");
 processImgFilePath = (String) request.getParameter("processImgPath");

 processDefinitionId = EncryptUrlPara.decrypt(processDefinitionId);
 processImgFilePath = EncryptUrlPara.decrypt(processImgFilePath);

需要注意的是此处不需要再decode了。

 

八、key(口令)的存放

 

因为我们这边的key就是口令,是一个文本,我们将它存放在server端的properties中,当然,我们也是加密存放的。

 

我们使用spring+jasypt1.5(java simple encrypt包)。

 

设我们有一个properties文件,其中:

security.des.key=ENC(OlO0LqELUuLOVreCtDngHaNgMcZWUyUg)

这个就是我们在encrypt和decrypt方法中用到的key.

我们不希望这个key以明文的形式设在properties中,我们对这个key再进行一次加密用的同样也是PBEWithMD5AndDES,当然因为有了spring因为有了jasypt包,因此这个过程一切是自动的。

我们使用jasypt包下的bin中自带的encrypt.bat工具:

 

encrypt input=mykey password=secret algorithm=PBEWithMD5AndDES

该命令会输出一行乱码,把这行乱码复制到properties文件中,在外层加上ENC(),如:

 

生成: OlO0LqELUuLOVreCtDngHaNgMcZWUyUg

放入properties后需要转换成: ENC(OlO0LqELUuLOVreCtDngHaNgMcZWUyUg)

然后在工程布署的机器上需要设一个环境变理,如:

set APP_ENCRYPTION_PASSWORD=secret   此处的值必须和上面encrypt.bat命令行中的password=后的值一样。

(linux请用export APP_ENCRYPTION_PASSWORD=secret)

 

然后配置spring,使该properties在工程被app 容器load时,自动解密,这样我们在我们的方法中直接取到该KEY时就已经是明文了(解密过程是jasypt+spring自动完成的),以下是这一步配置的详细内容:

 

 <context:component-scan base-package="jbpmweb" />
 <bean id="environmentVariablesConfiguration"
  class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig"
  p:algorithm="PBEWithMD5AndDES" p:passwordEnvName="APP_ENCRYPTION_PASSWORD" />

 <!--
  The will be the encryptor used for decrypting configuration values.
 -->

 <bean id="configurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor"
  p:config-ref="environmentVariablesConfiguration" />

 <bean id="propertyConfigurer"
  class="org.jasypt.spring.properties.EncryptablePropertyPlaceholderConfigurer">
  <constructor-arg ref="configurationEncryptor" />
  <property name="locations">
   <list>
    <value>classpath:xxx.properties</value>
   </list>
  </property>
 </bean>

 <!--
  Configurer that replaces ${...} placeholders with values from a
  properties file
 -->
 <context:property-placeholder location="classpath:jbpmweb.properties" />

 <bean id="commonsConfigurationFactoryBean" class="xxx.xxx.CommonsConfigurationFactoryBean"
  p:systemPropertiesModeName="SYSTEM_PROPERTIES_MODE_OVERRIDE" p:encryptor-ref="configurationEncryptor">
  <constructor-arg>
   <bean class="org.apache.commons.configuration.PropertiesConfiguration">
    <constructor-arg value="xxx.properties" />
   </bean>
  </constructor-arg>
 </bean>
 
  
 <bean id="propertiesConfiguration" factory-bean="&amp;commonsConfigurationFactoryBean"
  factory-method="getConfiguration"/>

目录
相关文章
|
16天前
|
JavaScript
vue截取URL中的参数
vue截取URL中的参数
12 0
|
2月前
|
前端开发
[牛客网-前端大挑战QD2] 获取url参数
[牛客网-前端大挑战QD2] 获取url参数
19 0
|
2月前
BurpSuite8.2 -- 查找包含id参数的URL
BurpSuite8.2 -- 查找包含id参数的URL
19 1
|
2月前
|
分布式计算 DataWorks 关系型数据库
DataWorks数据源问题之参数“连接Url”如何解决
DataWorks数据源是指DataWorks中配置的用于数据集成的外部数据源;本合集将讲解如何在DataWorks中配置和管理数据源,以及处理数据源连接和集成过程中的问题。
29 5
|
3月前
Copy网页中F12里的请求url到postman,并且把所有参数都带过来
Copy网页中F12里的请求url到postman,并且把所有参数都带过来
29 0
|
3月前
|
JavaScript
如何在JS中实现修改URL参数而不刷新页面
如何在JS中实现修改URL参数而不刷新页面
60 1
|
2月前
feign使用url参数传参@SpringQueryMap使用
feign使用url参数传参@SpringQueryMap使用
32 0
|
12天前
|
JavaScript 前端开发 关系型数据库
金融技术解决方案:用Python和Vue开发加密货币交易平台
【4月更文挑战第11天】本文介绍了如何使用Python和Vue.js构建加密货币交易平台。首先确保安装了Python、Node.js、数据库系统和Git。后端可选择Flask或Django框架,通过RESTful API处理交易。前端利用Vue.js、Vuex和Vue Router创建用户友好的界面,并用Axios与后端通信。这种架构促进团队协作,提升代码质量和平台功能。
|
15天前
【超实用】Angular如何修改当前页面网页浏览器url后面?param1=xxx&param2=xxx参数(多用于通过浏览器地址参数保存用户当前操作状态的需求),实现监听url路由切换、状态变化。
【超实用】Angular如何修改当前页面网页浏览器url后面?param1=xxx&param2=xxx参数(多用于通过浏览器地址参数保存用户当前操作状态的需求),实现监听url路由切换、状态变化。
|
15天前
|
JavaScript
【归总】原生js操作浏览器hash、url参数参数获取/修改方法合集
【归总】原生js操作浏览器hash、url参数参数获取/修改方法合集

相关课程

更多