Java 使用spring从tomcat server.xml解码加密密码

Java 使用spring从tomcat server.xml解码加密密码,java,spring,Java,Spring,我的spring应用程序中有以下配置 --main-context.xml <bean class="com.crisil.ram.hdfc.util.SpringContextUtil" /> <import resource="DataSource.xml"/> --DataSource.xml <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigu

我的spring应用程序中有以下配置

--main-context.xml
<bean class="com.crisil.ram.hdfc.util.SpringContextUtil" />   
<import resource="DataSource.xml"/>

--DataSource.xml
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>WEB-INF/database.properties</value>
    </property>
</bean>

<!--<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="${jdbc.jndiName}"/>
</bean>-->
在这里,我使用了加密格式的密码,我想在连接池中解密它。
请帮忙。

(a)有什么问题?(b) 请注意,base64不是加密。这仅仅是一种编码(传递相同信息的另一种方式,它不需要密钥,任何人都可以解码,包括黑客)。如果你必须加密自己的源代码/配置文件,你会遇到比用代码解决的问题更大的问题。解决此问题的通常方法是物理锁定服务器:卡访问、日志簿、安全警卫……请具体说明问题是,我已加密数据库密码并粘贴到server.xml资源中。现在,当连接到数据库时,我想对其进行解码。我已经在spring配置中进行了类似的配置。现在我想知道如何从java类中的server.xml中获取密码,这样我就可以对其进行解码并再次将其设置为resource。请任何人都可以帮助我
<bean id="dataSource" class="my.app.util.EncryptedDataSource">
    <property name="jndiName" value="${jdbc.jndiName}"/>
</bean>

--database.properties

jdbc.jndiName=java:comp/env/jdbc/MYAPPDB

--server.xml
<Context crossContext="true" debug="0" docBase="D:/apache-tomcat-7.0.32/webapps/MyApp" path="/MyApp" reloadable="true">
<Resource auth="Container" driverClassName="oracle.jdbc.driver.OracleDriver" logAbandoned="true" maxActive="20" maxIdle="40" 
 maxWait="60000" name="jdbc/MYAPPDB" removeAbandoned="true" removeAbandonedTimeout="60" type="javax.sql.DataSource" 
 url="jdbc:oracle:thin:@MYDESK:1521:u11g4777" username="user" password="cGFzc3dvcmQ="/><!--password-->
</Context>
package my.app.util;

import java.io.IOException;

import javax.naming.NamingException;

import org.apache.commons.configuration.JNDIConfiguration;
import org.springframework.jndi.JndiObjectFactoryBean;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class EncryptedDataSource extends JndiObjectFactoryBean{

    /*@Override
    public String getPassword() {
        String password = super.getPassword();
        password = decode(password);
        return password;
    }*/

    private String decode(String decode) {
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            decode = new String(decoder.decodeBuffer(decode));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return decode;
    }

    private String encode(String encode) throws IOException {
        BASE64Encoder encoder = new BASE64Encoder();
        encode = new String(encoder.encodeBuffer(encode.getBytes()));
        return encode;
    }

    public static void main(String[] args) throws IllegalArgumentException, NamingException {

        EncryptedDataSource ed = new EncryptedDataSource();

        try {
            String password = ed.encode("password");
            System.out.println("password = "+password);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}