在使用springMVC开发web项目中,数据库的用户名,密码一般都是配置在.properties文件中

然后在通过.xml配置文件引入.properties的变量,例如

在config.properties文件中,配置如下变量,变量值配置在pom.xml的profile标签下,在此就不再赘述

jdbc.driverClassName=com.mysql.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}

在applicationContext.xml中,通过如下标签引入这些变量值

    <!-- 将多个配置文件读取到容器中,交给Spring管理 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.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 = KeyGenerator.getInstance("DES");
SecureRandom secureRandom=SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(KEY_STR.getBytes());
generator.init(secureRandom);
key = generator.generateKey();
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 = str.getBytes("UTF-8");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptStrBytes = cipher.doFinal(strBytes);
return base64Encoder.encode(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 = Cipher.getInstance("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 = DESUtils.getDecryptString(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.beans.factory.config.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

springMVC web项目 对访问数据库的用户名密码进行加密解密的更多相关文章

  1. Eclipse | 如何修改web项目的访问链接名,项目名

    转: Eclipse | 如何修改web项目的访问链接名,项目名 2018-01-04 17:52:05 Mandsence 阅读数 2180更多 分类专栏: 其他   版权声明:本文为博主原创文章, ...

  2. 基于SpringBoot + Mybatis实现SpringMVC Web项目

    一.热身 一个现实的场景是:当我们开发一个Web工程时,架构师和开发工程师可能更关心项目技术结构上的设计.而几乎所有结构良好的软件(项目)都使用了分层设计.分层设计是将项目按技术职能分为几个内聚的部分 ...

  3. 【Maven】Eclipse 使用Maven创建SpringMVC Web项目

    创建环境 系统:win 10 软件:eclipse,maven. 创建步骤 创建一个Maven Web项目,可以参照:[Maven]Eclipse 使用Maven创建Java Web项目 添加spri ...

  4. web api中访问数据库的内存释放问题

    在使用web api开发微信后台的时候,本来想像MVC一样在controller中申明dbcontext全局变量,其它地方直接使用就可以了,结果调试过程中发现使用dbcontext访问数据库并获取qu ...

  5. Eclipse (JavaEE版)中修改web项目的访问路径

    访问路径,也就是指在浏览器中访问该web系统时的根路径,比如http://localhost:8080/xxxx/index.jsp  这里的xxxx. 也就是request.getContextPa ...

  6. C# web项目中sql数据库转sqlite数据库

    最近做了一个小网站,用到了一个使用sql server 2005的.net cms系统,但是现在我所买虚拟主机的服务商,不给虚拟主机提供sql server服务了,那就转数据库吧,转啥好呢,思来想去, ...

  7. 【MyEclipse初级】Web项目的访问路径更改

    背景:MyEclipse 开发的Web项目,发布Web项目到Tomcat,从浏览器访问路径配置和工程名称一致,思考是否可以自定义访问虚拟路径. 目标:修改访问Web项目的虚拟路径 步骤:工程名右键-& ...

  8. java web中Jdbc访问数据库步骤通俗解释(吃饭),与MVC的通俗解释(做饭)

    一.Jdbc访问数据库步骤通俗解释(吃饭) 1)加载驱动 Class.forName(“com.microsoft.jdbc.sqlserver.SQLServer”); 2) 与数据库建立连接 Co ...

  9. spring boot之创建web项目并访问jsp页面

    1,创建spring boot的web项目 刚创建好的项目路径如下: 2,pom中要有下面的依赖 <dependency> <groupId>org.springframewo ...

随机推荐

  1. BZOJ 1076: [SCOI2008]奖励关 [DP 期望 状压]

    传送门 题意:$n$种宝物,出现$k$次每次一种,每种宝物有价值和吃掉它之前必须要吃掉的宝物的集合,求采取最优策略的期望最大价值 1<=k<=100,1<=n<=15,分值为[ ...

  2. java windows自动化-mail自动发邮件

    本文旨在让测试人员了解如何发邮件 发邮件的话,最简单的事是直接手动发邮件,但是在自动化测试中,应做到让机器或者代码来自动发送邮件,笔者大概了解以下几种方法,总有一款口味适合你:1java代码来做下面即 ...

  3. SpringBoot学习之Json数据交互

    最近在弄监控主机项目,对javaweb又再努力学习.实际的项目场景中,前后分离几乎是所以项目的标配,全栈的时代的逐渐远去,后端负责业务逻辑处理,前端负责数据展示成了一种固定的开发模式.像thymele ...

  4. Access denied for user 'root'@'localhost' (using password: YES)的解决

    今天使用phpmyadmin登录远程数据库所以改了一些配置,结果回过头来登录本地mysql时来了一句mysql Access denied for user 'root'@'localhost' (u ...

  5. 用mount挂载远程服务器网络硬盘

    环境: 服务器:192.168.20.204 客户端:192.168.20.203 1. 在服务器配置/etc/export  添加可以共享的文件夹和允许的客户端地址 /home/dir 192.16 ...

  6. js分页功能实现

    实现一个js的分页并在弹出框中显示 1.分页插件使用:bootstarp-paginator.js,需要先引入bootstarp.js和jquery.js等: !function($){"u ...

  7. 【学习笔记】Struts2 类型转换

    为什么需要类型转换 在基于HTTP协议的Web应用中 客户端请求的所有内容(表单中提交的内容等)都以文本编码的方式传输到服务器端但服务器端的编程语言(如Java)有着丰富的数据类型 如 int boo ...

  8. Hibernate学习(一)创建数据表

    (1)生成数据库表的创建: // 默认读取hibernate.cfg.xml文件 Configuration cfg = new Configuration().configure(); // 生成并 ...

  9. Opencv 330 如何裁剪图片中大的目标?

    main.cpp cv::Mat CropImage(cv::Mat& src, cv::RotatedRect& rotatedRect); cv::Mat MaxBoxSelect ...

  10. caffe之路-SIGTERM信号捕捉

    Caffe在1.0版本仅支持两种信号的处理: 1) SIGHUP 2) SIGINT SIGHUP:caffe接收到此信号后进行snapshot,并不会中断caffe的训练. SIGINT:caffe ...