SPRING 数据库密码加密存储 在配置文件的两种方式 第一种

package com.spring.demo.utils;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DecryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
    @Override
    protected String convertProperty(String propertyName, String propertyValue)
    {
        //如果在加密属性名单中发现该属性
        if (isEncryptProp(propertyName))
        {
            String decryptValue =AesUtils.decrypt(propertyValue,AesUtils.key);
            System.out.println(decryptValue);
            return decryptValue;
        }else {
            return propertyValue;
        }

    }

    private boolean isEncryptProp(String propertyName)
    {
       if (propertyName.startsWith("encrypt")){
           return true;
       }
        return false;
    }

    public  void setLocation(Resource location) {
        String locationStr = location.toString();
        if (locationStr == null || locationStr.equals("")){
            return;
        }
        int start = locationStr.indexOf("[");
        int end = locationStr.indexOf("]");
        if (start < 0 || end < 0 || end <= start+1){
            return;
        }
        String locStr = locationStr.substring(start+1,end);
        String[] paths = locStr.split(",");
        List<Resource> list = new ArrayList<>();
        for (int i = 0; i < paths.length; i++) {
            String loc = paths[i];
            if (loc == null || loc.equals("")){
                continue;
            }
            ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            //将加载多个绝对匹配的所有Resource
            //然后进行遍历模式匹配
            try {
                Resource[] resources=resolver.getResources(loc);
                list.addAll(Arrays.asList(resources));
            } catch (IOException e) {
                continue;
            }
        }
        this.setLocations(list.toArray(new Resource[list.size()]));

    }
}
<bean id="decryptPropertyPlaceholderConfigurer"
          class="com.spring.demo.utils.DecryptPropertyPlaceholderConfigurer"
          p:location="classpath*:*.properties,classpath*:loc/*.properties">
    </bean>
时间: 2024-09-20 10:42:08

SPRING 数据库密码加密存储 在配置文件的两种方式 第一种的相关文章

SPRING 数据库密码加密存储 在配置文件的两种方式 第二种

spring <context:property-placeholder local-override="true" properties-ref="dataSourceProperties" file-encoding="UTF-8" location="classpath:loc/config.properties" ignore-resource-not-found="true" /> 3

SPRING 数据库密码加密存储 在配置文件的两种方式 下一篇第二种方式

分析 SPRING通过 1.property-placeholder spring3.1以前实现是PropertyPlaceholderConfigurer,3.1以后是PropertySourcesPlaceholderConfigurer <context:property-placeholder local-override="true" properties-ref="dataSourceProperties" file-encoding="

hibernate配置文件中数据库密码加密

问题描述 hibernate配置文件中数据库密码加密 求大神支招啊... 这是的hibernate.xml <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-co

问个关于aspx网站数据库密码加密解密的问题

问题描述 333222310110410396919810307对应的明文密码为longbin3322222222110112454247464338404410对应的明文密码为xz74985026这是一种什么加密方式啊如何解密http://51.dc.ftn.qq.com/ftn_handler/fb1e7b2d252d2424bf6f3740d11f1fc8d27a82f6826b2b57904e7e5872b8bdb3e72380aee697bf93cf493f337530cf2ae315

不使用spring的情况下用java原生代码操作mongodb数据库的两种方式

由于更改了mongodb3.0数据库的密码,导致这几天storm组对数据进行处理的时候,一直在报mongodb数据库连接不上的异常.   主要原因实际上是和mongodb本身无关的,因为他们改的是配置文件的密码,而实际上这个密码在代码中根本就没有使用,他们在代码中已经把用户验证信息写死.   在协助他们解决这个问题的时候,我看到他们代码中在和mongodb数据库交互时使用了已经不被建议使用的方法,于是便抽时间尝试了一下另一种被建议的方式实现各功能.   当然了,生产环境中用的是mongodb集群

SQLServer · 最佳实践 · 数据库实现大容量插入的几种方式

背景 很多用户在使用阿里云云数据库SQL Server时,为了加快插入速度,都尝试使用大容量插入的方式,大家都知道,对于完整恢复模式下的数据库,大容量导入执行的所有行插入操作都会完整地记录在事务日志中.如果使用完整恢复模式,大型数据导入会导致填充事务日志的速度很快.相反,对于简单恢复模式或大容量日志恢复模式,大容量导入操作的按最小方式记录日志减少了大容量导入操作填满日志空间的可能性.另外,按最小方式记录日志的效率也比按完整方式记录日志高 . 但实际上,当大容量导入与数据库镜像共存时,会出现镜像

javascript记住用户名和登录密码(两种方式)_javascript技巧

下面主要通过代码给大家展示下javascript记住用户名和登录密码,具体代码内容请看下文. 第一种方式: CONTENT     login.html     welcome.html     cookie.js     common.jslogin.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-tr

Spring Security笔记:使用BCrypt算法加密存储登录密码

在前一节使用数据库进行用户认证(form login using database)里,我们学习了如何把"登录帐号.密码"存储在db中,但是密码都是明文存储的,显然不太讲究.这一节将学习如何使用spring security3新加入的bcrypt算法,将登录加密存储到db中,并正常通过验证. 一.Bcrypt算法 1 int t = 0; 2 String password = "123456"; 3 System.out.println(password + &q

使用 Salt + Hash 将密码加密后再存储进数据库_实用技巧

(一) 为什么要用哈希函数来加密密码 如果你需要保存密码(比如网站用户的密码),你要考虑如何保护这些密码数据,象下面那样直接将密码写入数据库中是极不安全的,因为任何可以打开数据库的人,都将可以直接看到这些密码. 解决的办法是将密码加密后再存储进数据库,比较常用的加密方法是使用哈希函数(Hash Function).哈希函数的具体定义,大家可以在网上或者相关书籍中查阅到,简单地说,它的特性如下: (1)原始密码经哈希函数计算后得到一个哈希值 (2)改变原始密码,哈希函数计算出的哈希值也会相应改变