日期:2014-05-17  浏览次数:21009 次

tomcat的项目配置文件,链数据库的用户和密码如何加密?
我用的是tomcat做服务器,然后在Tomcat5.5\conf\Catalina\localhost目录下建立了工程的配置文件,但是里面链接数据库的用户和密码是明文的,现在想加密,不知如何解决,请大家帮忙解决了。配置文件的内容如下:<?xml version="1.0" encoding="UTF-8"?>
<Context
  docBase="D:/workspace/BankCheckBusinessInsShow_shenzhen"
  reloadable="true"
  workDir="work/Catalina/localhost/BankCheckBusinessInsShow_shenzhen">
  <Resource
  auth="Container"
  name="jdbc/BankCheckBusinessInsShow"
  type="javax.sql.DataSource"
  maxActive="50"
  maxIdle="30"
  digest="MD5"
  username="pe_shenzhen"
  maxWait="5000"
  removeAbandoned="true"
  driverClassName="com.ibm.db2.jcc.DB2Driver"
  password="pe_shenzhen"
  url="jdbc:db2://rbht4:50000/pe"/>
</Context>

------解决方案--------------------
楼主可以试试以下步骤:

1. 生成一个Java项目 Test

2. 在这个项目下生成两个package
org.moss.jdj.codec 和 org.moss.jdj.dbcp

3. 把 commons-pool-1.3.jar 和 commons-dbcp-1.2.1.jar 放到 Test 的 classpath 上

4. 把下面这个 Class 放到 org.moss.jdj.dbcp 下:
Java code

package org.moss.jdj.dbcp;

import java.util.Enumeration;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.Name;
import javax.naming.RefAddr;
import javax.naming.Reference;
import javax.naming.StringRefAddr;

import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.moss.jdj.codec.Base64Coder;

public class EncryptedDataSourceFactory extends BasicDataSourceFactory 
{

    public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment)
    throws Exception      
    {
        if (obj instanceof Reference)
        {
            setUsername((Reference)obj);
            setPassword((Reference)obj);        
        }    
        return super.getObjectInstance(obj, name, nameCtx, environment);
    }

    private void setUsername(Reference ref) 
    throws Exception {
        findDecryptAndReplace("username", ref);        
    }
    
    private void setPassword(Reference ref) 
    throws Exception {
        findDecryptAndReplace("password", ref);
    }

    
    private void findDecryptAndReplace(String refType, Reference ref)
    throws Exception {
        int idx = find(refType, ref);
        String decrypted = decrypt(idx, ref);
        replace(idx, refType, decrypted, ref);
    }
    
    private void replace(int idx, String refType, String newValue, Reference ref)
    throws Exception
    {
        ref.remove(idx);
        ref.add(idx, new StringRefAddr(refType, newValue));
    }
    
    private String decrypt(int idx, Reference ref) 
    throws Exception {
        return Base64Coder.decode(
            ref.get(idx).getContent().toString()
        );
    }
    
    private int find(String addrType, Reference ref) 
    throws Exception
    {
        Enumeration enu = ref.getAll();
        for (int i=0; enu.hasMoreElements(); i++) {
            RefAddr addr = (RefAddr)enu.nextElement();
            if (addr.getType().compareTo(addrType) == 0) {
                return i;
            }
        }
        throw new Exception(
            "The \""+addrType+"\" name/value pair was not found"
            + " in the Reference object.  The reference Object is"
            + " " + ref.toString()
        );
    }
     
     
}

------解决方案--------------------
我刚好弄过这个
我这里是ssh的,先用加密算法加密数据库密码,然后再(我那里是spring解析的数据源)继承你解析数据源的spring的那个类,重写一个子类进行解密,把spring配置里面的解析类换成你的这个子类,然后就ok了。