栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring mvc和springboot 配置文件加密

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring mvc和springboot 配置文件加密

1、spring mvc 配置文件加密,直接上代码:

     流程:编写EncryptPropertyPlaceholderConfigurer  --> 配置spring-context.xml

public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {


    
    private String[] ENCRYPT_PROP_NAMES = {
            "jdbc.url",
            "jdbc.username",
            "jdbc.password"
    };

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactory,
                                     Properties props) throws BeansException {
        try {
            for (String kye :  ENCRYPT_PROP_NAMES) {
                if (!isEncryptProp(kye)) {
                    throw new BeanInitializationException("没有获取到加密配置文件,请检查jeesite.properties文件中是否存在"+kye);
                }
                String value = props.getProperty(kye);
                if (value != null) {
                    props.setProperty(kye,Cryptos.aesDecrypt(value));
                }
            }
            super.processProperties(beanFactory, props);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BeanInitializationException(e.getMessage());
        }
    }


    private boolean isEncryptProp(String propertyName) {
        for (String encryptPropName : ENCRYPT_PROP_NAMES) {
            if (encryptPropName.equals(propertyName)) {
                return true;
            }
        }
        return false;
    }

加密方式自行选择,我这是AES加密和解密-->

配置增加下面信息(springmvc的spring-context.xml配置文件)

	
	
		
			
				classpath:jeesite.properties
			
		
	

这个方法只是在配置文件中加密敏感信息,要是在项目中还需要使用的话,需要自行解密(如:连接jdbc驱动时,需要解密)

-- 生成加密后的信息,复制到properties 文件中去

public static void main(String[] args) {
        
        String[] ENCRYPT_PROP_NAMES = {
                "jdbc.url",
                "jdbc.username",
                "jdbc.password"
        };

        System.out.println("加密:");
        for (String key : ENCRYPT_PROP_NAMES) {
            String keyValue = Global.getConfig(key);//这里获取的是明文信息
            String aesEncrypt = Cryptos.aesEncrypt(keyValue); //这里是加密信息
            System.out.println( key + "="+aesEncrypt);
//            System.out.println("解密(" + key + "):" + Cryptos.aesDecrypt(aesEncrypt));
        }
    }

注:如果用的是其它数据源的话,需要重写初始化类

        流程:重写相关的初始化类,如下:因为我只加密3个属性,如有其它可根据继承中的去处理

public class SecurityDateSource extends DruidDataSource {

    @Override
    public void setUsername(String username) {
        username = Cryptos.aesDecrypt(username);
        super.setUsername(username);
    }

    @Override
    public void setPassword(String password) {
        password = Cryptos.aesDecrypt(password);
        super.setPassword(password);
    }

    @Override
    public void setUrl(String jdbcUrl) {
        jdbcUrl = Cryptos.aesDecrypt(jdbcUrl);
        super.setUrl(jdbcUrl);
    }
}

再配置文件中配置重写的即可:

	

---------------------------------------------------------------------------------------------------------------------------------

springboot项目配置文件加密:要使用jasypt工具包

jasypt由一个国外大神写了一个springboot下的工具包,用来加密配置文件中的信息。

jasypt路径
 

操作步骤:导入jasypt包-->配置文件配置加密密码-->使用ENC配置相关配置信息

http://可查看最新版本jasypt


        com.github.ulisesbocchio
        jasypt-spring-boot-starter
        2.1.0
# jasypt加密的密匙
jasypt:
  encryptor:
    password: 1234567890abcdefghijklnmopqrstuvwxyz


 编写测试类获取加密信息

@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class JasyptTest {

    @Resource
    StringEncryptor encryptor;

    @Test
    public void getPass() {
 
        String url = encryptor.encrypt("jdbc:kingbase8://127.0.0.1:54321/TEST");
        String name = encryptor.encrypt("TEST");
        String password = encryptor.encrypt("123456789");
        System.out.println(url);
        System.out.println(name);
        System.out.println(password);
        Assert.assertTrue(name.length() > 0);
        Assert.assertTrue(password.length() > 0);
    }
}

最后配置yml

        driver-class-name: com.kingbase8.Driver
        url: ENC(dzs/ACxGmH45RiujmHNObq2LQAI/rDLolp1Jw4IzgsfsOi9eEKbSKiUOs6v5cHi6fsI0uf0=)
        username: ENC(6sh7wuK2iGm4PkJFIyow==)
        password: ENC(VmD2nR9y226lftE6zEQ==)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/704721.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号