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

来源:互联网 发布:天庭淘宝城在线txt下载 编辑:程序博客网 时间:2024/05/17 22:51

在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

0 0
原创粉丝点击