springMVCweb项⽬对访问数据库的⽤户名密码进⾏加密解密
在使⽤springMVC开发web项⽬中,数据库的⽤户名,密码⼀般都是配置在.properties⽂件中
然后在通过.xml配置⽂件引⼊.properties的变量,例如
在config.properties⽂件中,配置如下变量,变量值配置在l的profile标签下,在此就不再赘述
jdbc.sql.jdbc.Driver
jdbc.url=jdbc\:mysql\://${p.jdbc.url}/${p.jdbc.dbname}?useUnicode\=true&characterEncoding\=UTF-8&zeroDateTimeBehavior\=convertToNull&rewriteBatchedStatements\=true  jdbc.username=${p.jdbc.username}
jdbc.password=${p.jdbc.password}
在l中,通过如下标签引⼊这些变量值
<!-- 将多个配置⽂件读取到容器中,交给Spring管理 -->
<bean id="propertyConfigurer"
class="org.springframework.fig.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/properties/*.properties</value>
</list>
</property>
</bean>
这样对于是明⽂的帐号,密码,是没有问题的。但是如果我在配置⽂件中的帐号密码是加密后的,那么如何进⾏使⽤配置呢?
解决办法:
1.⾸先确定加密解密,这种情况下,我们肯定选择是对称性加密解密算法,⾸选DES算法,在这⾥就拿他举例
2.完成加密解密算法,这个代码很简单,就不赘述,密钥⾃⼰决定,保密即可
public class DESUtils
{
private static Key key;
private static String KEY_STR="mykey";
static{
try
{
KeyGenerator generator = Instance("DES");
SecureRandom Instance("SHA1PRNG");
secureRandom.setSeed(Bytes());
generator.init(secureRandom);
key = ateKey();
generator=null;
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
/**
* 对字符串进⾏加密,返回BASE64的加密字符串
* <;功能详细描述>
* @param str
* @return
* @see [类、类#⽅法、类#成员]
*/
public static String getEncryptString(String str){
BASE64Encoder base64Encoder = new BASE64Encoder();
System.out.println(key);
try
{
byte[] strBytes = Bytes("UTF-8");
Cipher cipher = Instance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
springmvc常用标签
byte[] encryptStrBytes = cipher.doFinal(strBytes);
de(encryptStrBytes);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
/**
* 对BASE64加密字符串进⾏解密
* <;功能详细描述>
* @param str
* @return
* @see [类、类#⽅法、类#成员]
*/
public static String getDecryptString(String str){
BASE64Decoder base64Decoder = new BASE64Decoder();
try
{
byte[] strBytes = base64Decoder.decodeBuffer(str);
Cipher cipher = Instance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] encryptStrBytes = cipher.doFinal(strBytes);
return new String(encryptStrBytes,"UTF-8");
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
public static void main(String[] args)
{
String name ="root";
String password="1234";
String encryname = getEncryptString(name);
String encrypassword = getEncryptString(password);
System.out.println(encryname);
System.out.println(encrypassword);
System.out.println(getDecryptString(encryname));
System.out.println(getDecryptString(encrypassword));
}
}
3.springMVC中需要实现解密的接⼝
需要覆盖convertProperty⽅法,encryptPropNames存储的是需要解密的属性
public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer
{
private String[] encryptPropNames = {"jdbc.username", "jdbc.password"};
@Override
protected String convertProperty(String propertyName, String propertyValue)
{
//如果在加密属性名单中发现该属性
if (isEncryptProp(propertyName))
{
String decryptValue = DecryptString(propertyValue);
System.out.println(decryptValue);
return decryptValue;
}else {
return propertyValue;
}
}
private boolean isEncryptProp(String propertyName)
{
for (String encryptName : encryptPropNames)
{
if (encryptName.equals(propertyName))
{
return true;
}
}
return false;
}
}
4.配置这个解密类
<bean id="propertyConfigurer" class="org.utils.EncryptPropertyPlaceholderConfigurer" >
<property name="locations">
<list>
<value>classpath:/properties/*.properties</value>
</list>
</property>
</bean>
替换
<bean id="propertyConfigurer" class="org.springframework.fig.PropertyPlaceholderConfigurer">        <property name="locations">
<list>
<value>classpath:/properties/*.properties</value>
</list>
</property>
</bean>
备注:实际使⽤中可能会遇到 sun.misc.BASE64Encoder⽆法使⽤的问题,下⾯给出解决办法
解决⽅案1(推荐):
只需要在project build path中先移除JRE System Library,再添加库JRE System Library,重新编译后就⼀切正常了。解决⽅案2:
Windows -> Preferences -> Java -> Compiler -> Errors/Warnings ->
Deprecated and trstricted API -> Forbidden reference (access rules): -> change to warning

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。