Spring+Hibernate 数据库配置信息的加密

本文介绍了如何解决在hibernate.cfg.xml中明文存放数据库用户和密码的安全问题。通过重写Spring的DriverManagerDataSource类,实现配置信息的加密解密方法,确保敏感数据的安全。具体操作包括创建一个继承自AbstractDriverBasedDataSource的类,覆盖相关方法以处理加密的用户和密码,然后将应用的dataSource替换为这个自定义类。如果使用第三方连接池,如C3P0,需要继承相应的ConnectionProvider并进行相应改造。

在hibernate.cfg.xml中,用户和密码是明文存放的,存放某些安全问题,可以重写dataSource类来实现对配置信息加密的解密方法

 <bean
        id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property
            name="driverClassName"
            value="${jdbc.driverClassName}" />
        <property
            name="url"
            value="${jdbc.url}" />
        <property
            name="username"
            value="${jdbc.username}" />
        <property
            name="password"
            value="${jdbc.password}" />
</bean>

比如这个配置中我们需要重写org.springframework.jdbc.datasource.DriverManagerDataSource
这个类继承于AbstractDriverBasedDataSource

可以写个类继承AbstractDriverBasedDataSource,重写里面的方法

比如用户、密码照着DriverManagerDataSource改掉相应的方法

/**
     * Create a new DriverManagerDataSource with the given standard
     * DriverManager parameters.
     * @param url the JDBC URL to use for accessing the DriverManager
     * @param username the JDBC username to use for accessing the DriverManager
     * @param password the JDBC password to use for accessing the DriverManager
     * @see java.sql.DriverManager#getConnection(String, String, String)
     */
    public DriverManagerDataSource(String url, String username, String password) {
        setUrl(url);
        setUsername(username);
        setPassword(password);
    }

    /**
     * Create a new DriverManagerDataSource with the given JDBC URL,
     * not specifying a username or password for JDBC access.
     * @param url the JDBC URL to use for accessing the DriverManager
     * @param conProps JDBC connection properties
     * @see java.sql.DriverManager#getConnection(String)
     */
    public DriverManagerDataSource(String url, Properties conProps) {
        setUrl(url);
        setConnectionProperties(conProps);
    }

修改后

/**
     * Create a new DriverManagerDataSource with the given standard
     * DriverManager parameters.
     * @param url the JDBC URL to use for accessing the DriverManager
     * @param username the JDBC username to use for accessing the DriverManager
     * @param password the JDBC password to use for accessing the DriverManager
     * @see java.sql.DriverManager#getConnection(String, String, String)
     */
    public DriverManagerDataSource(String url, String username, String password) {
        setUrl(url);
        setUsername(DesEncrypter.getInstance().decrypt(username));
        setPassword(DesEncrypter.getInstance().decrypt(password));
    }

    /**
     * Create a new DriverManagerDataSource with the given JDBC URL,
     * not specifying a username or password for JDBC access.
     * @param url the JDBC URL to use for accessing the DriverManager
     * @param conProps JDBC connection properties
     * @see java.sql.DriverManager#getConnection(String)
     */
    public DriverManagerDataSource(String url, Properties conProps) {
        setUrl(url);
        if(conProps.containsKey("user")){
            conProps.setProperty("user", DesEncrypter.getInstance().decrypt(conProps.getProperty("user")));
        }
        if(conProps.containsKey("password")){
            conProps.setProperty("password", DesEncrypter.getInstance().decrypt(conProps.getProperty("password")));
        }
        setConnectionProperties(conProps);
    }

最后把dataSource改为重写的类

<bean
        id="dataSource"
        class="com.ht.platform.datasource.DriverManagerDataSource">

PS:如果使用第三方的连接器,CustomDriverManagerConnectionProvider则需要继承于相应的连接器,如C3P0ConnectionProvider

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值