web项目中对properties文件的加密

背景:出于对项目安全方面的考虑,要求对properties中的有关数据库连接信息加密。本例中用了BASE64加密

  • 首先:将数据库密码用BASE64加密,获得加密后的密文,比如”bG92ZTQzMQT==”,将该字符串写到properties相应位置。比如:
#连接池通用url配置
jdbc.url=jdbc:oracle:thin:@192.168.0.25:1521:orcl
jdbc.username=CROSS_INFO_BASE
jdbc.password=<<bG92ZTQzMQT==>>
  • 第二步:重写PropertyPlaceholderConfigurer类
public class EncryptedPlaceholderConfigurer   extends PropertyPlaceholderConfigurer{

    public static final String PREFIX = "<<";

    @Override
    protected String convertPropertyValue(String originalValue) {
        if (originalValue.startsWith(PREFIX)) {
            logger.debug("Find encrypted value:" + originalValue);
            String jmStr=originalValue.substring(2,originalValue.length()-2);
            //解密
            String newValue =Base64.getFromBase64(jmStr);
            return newValue;
        }
        return originalValue;
    }

}
  • 最后:修改spring配置文件即可。
<!-- 属性文件读入 -->
    <bean id="propertyConfigurer" class="org.work.util.EncryptedPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath*:config/jdbc.properties</value>
                <value>classpath*:config/hibernate.properties</value>
                <value>classpath*:config/redis.properties</value>
            </list>
        </property>
    </bean>

猜你喜欢

转载自blog.csdn.net/weixin_36087172/article/details/78496559