admin管理员组

文章数量:1589660

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

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

public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {


    /**
     * 需要加解密的配置文件key
     */
    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配置文件)


	<!-- 加载 解密 配置文件 -->
	<bean id="placeholderConfig" class="com.thinkgem.jeesite.config.EncryptPropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:jeesite.properties</value>
			</list>
		</property>
	</bean>

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

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

public static void main(String[] args) {
        /**
         * 需要加解密的配置文件key
         */
        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);
    }
}

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

<!-- 数据源配置, 使用 BoneCP 数据库连接池 -->
	<bean id="dataSource" class="com.thinkgem.jeesite.config.SecurityDateSource" init-method="init" destroy-method="close">

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

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

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

jasypt路径
 

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

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

<dependency>
        <groupId>com.github.ulisesbocchio</groupId>
        <artifactId>jasypt-spring-boot-starter</artifactId>
        <version>2.1.0</version>
</dependency>
# 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==)

本文标签: 文件加密SpringMVCSpringBoot