• 【springboot】 整合 jasypt 配置信息加密


    项目集成jasypt的方式

    引入jasypt-spring-boot加密组件

    通过jasypt-spring-boot这个开箱即用的加密组件来引入Jasypt这个强大的加密库

    方式一:

    在Springboot应用程序中,如果使用了@SpringBootApplication or @EnableAutoConfiguration注解,则可以直接在pom文件中添加jasypt-spring-boot依赖,然后就可以在整个Spring环境中使用jasypt对属性进行加解密操作(属性包括:系统属性、环境属性、命令行参数、properties、yml以及任何其他属性源)。

    <dependency>
        <groupId>com.github.ulisesbocchiogroupId>
        <artifactId>jasypt-spring-boot-starterartifactId>
        <version>3.0.4version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    方式二:

    如果项目中没有使用到@SpringBootApplication or @EnableAutoConfiguration注解,则可以通过以下两个步骤完成对Jasypt的集成。

    步骤一:pom文件引入jasypt依赖

    <dependency>
            <groupId>com.github.ulisesbocchiogroupId>
            <artifactId>jasypt-spring-bootartifactId>
            <version>3.0.4version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    步骤二:在配置类中,添加@EnableEncryptableProperties注解,示例如下:

    @Configuration
    @EnableEncryptableProperties
    public class MyApplication {
        ...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    通过这种方式,你的项目一样可以集成Jasypt,并且可加密属性也可以在整个Spring环境中启用(属性包括:系统属性、环境属性、命令行参数、properties、yml以及任何其他属性源)。

    方式二:

    如果项目中没有使用到@SpringBootApplication or @EnableAutoConfiguration注解,又不想在整个Spring环境中启用加密的属性,则可以使用该种方式,具体步骤如下:

    步骤一:pom文件引入jasypt依赖

    <dependency>
            <groupId>com.github.ulisesbocchiogroupId>
            <artifactId>jasypt-spring-bootartifactId>
            <version>3.0.4version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    步骤二、在配置类中,使用@EncryptablePropertySource注解添加任意数量想要生效加密属性的配置文件路径,与Spring中@PropertySource注解的使用类似,示例如下:

    @Configuration
    @EncryptablePropertySource(name = "EncryptedProperties", value = "classpath:encrypted.properties")
    public class MyApplication {
    	...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    同时,还可以使用@EncryptablePropertySources 注解对@EncryptablePropertySource配置进行分组,示例如下:

    @Configuration
    @EncryptablePropertySources({@EncryptablePropertySource("classpath:encrypted.properties"),                         @EncryptablePropertySource("classpath:encrypted2.properties")})
    	public class MyApplication {
    		...
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5

    说明:从Jasypt 1.8版本开始,@EncryptablePropertySource注解支持配置YAML文件

    springboot整合jasypt

    引入依赖

    <dependency>
    	<groupId>com.github.ulisesbocchiogroupId>
    	<artifactId>jasypt-spring-boot-starterartifactId>
    	<version>3.0.4version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在配置文件中指定自定义加密器的bean

    # 指定bean
    jasypt:
      encryptor:
        bean: CodeEncryBean
        property:
          prefix: DONG(
          suffix: )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    使用自定义加密器(创建bean)

    说明:
    使用自定义加密器,这样会更安全,因为如果将加密密钥写在配置文件中,这跟没有加密差不多。
    jasypt.encryptor.password=abc(你的密钥)

    定义个配置类 MyJasyptConfig.java

    package com.example.springbootjasypt.config;
    
    import org.jasypt.encryption.StringEncryptor;
    import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
    import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * @author 成大事
     * @since 2022/7/16 16:57
     */
    @Configuration
    public class MyJasyptConfig {
        private String key = "PEB123@321BEP";
    
        @Bean(name = "CodeEncryBean")
        public StringEncryptor CodeEncryBean() {
            PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
    
            SimpleStringPBEConfig config = new SimpleStringPBEConfig();
    
            config.setPassword(key);
            config.setAlgorithm("PBEWithMD5AndDES");
            config.setKeyObtentionIterations("1000");
            config.setPoolSize("1");
            config.setProviderName("SunJCE");
            config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
            config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
            config.setStringOutputType("base64");
            encryptor.setConfig(config);
            return encryptor;
        }
    
        //测试
        public static void main(String[] args) {
            PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
            SimpleStringPBEConfig config = new SimpleStringPBEConfig();
            config.setPassword("PEB123@321BEP");
            config.setAlgorithm("PBEWithMD5AndDES");
            config.setKeyObtentionIterations("1000");
            config.setPoolSize("1");
            config.setProviderName("SunJCE");
            config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
            config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
            config.setStringOutputType("base64");
            encryptor.setConfig(config);
            //===================================
            String encrypt = encryptor.encrypt("root");
            String encrypt2 = encryptor.encrypt("mysql729");
            //String decrypt = encryptor.decrypt("ZxY08m8wOk4qE/cTEgfzhRbYQlxKg5mhG+kZ6P5lc0MQwy87Z3MouPFWyVGlGyPf");
            //String decrypt2 = encryptor.decrypt("vtK/ygFJN1togHuUS17IbKsc6QQtyK2L2huyXLMmsc9vRQE0zCU+Qo5zQy0FfkQs");
            System.out.println(encrypt);
            System.out.println(encrypt2);
            //System.out.println(decrypt);
            //System.out.println(decrypt2);
    
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    在配置文件中将密码替换

    注意:jasypt人家默认是要这样识别 ENC(密文)
    例如:

    spring:
      datasource:
        username: ENC(06otKd5RtT8tYtbrxZGPUG3Rc9X85BXa)               #数据库的用户名
        password: ENC(OkEx54575MtmLRQEmy1yCYitDhLS5Lay9YM+sRIR2Qg=)           #数据库的密码
    
    • 1
    • 2
    • 3
    • 4

    不使用NNC()包裹,自定义

    自己定义前后缀

    spring:
      datasource:
        username: DONG(06otKd5RtT8tYtbrxZGPUG3Rc9X85BXa)               #数据库的用户名
        password: DONG(OkEx54575MtmLRQEmy1yCYitDhLS5Lay9YM+sRIR2Qg=)           #数据库的密码
    # 指定bean
    jasypt:
      encryptor:
        bean: CodeEncryBean
        property:
          prefix: DONG(
          suffix: )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    当然这个加密的密钥还有其他的引入方式,但是感觉这种就很可以了

    方式一:直接作为程序启动时的命令行参数来带入
    方式二:直接作为程序启动时的应用环境变量来带入
    方式三:甚至可以作为系统环境变量的方式来带入–最安全

  • 相关阅读:
    在服务器上搭建Jenkins自动化部署工具
    Nodejs -- 一文学会如何在Express中使用JWT(json web token)
    强烈推荐APP破解常用工具集合!
    计算机毕业设计之java+springcloud分布式架构网上商城网站
    【Shell编程】字符截取命令cut、printf命令
    WPF透明置顶窗口wine适配穿透问题解决
    【操作系统】总结 (一) 机组部分知识
    什么是开源工作流框架?有什么特点?
    【优化模型】报童的诀窍
    低功耗工业RFID设备应用
  • 原文地址:https://blog.csdn.net/m0_49683806/article/details/125991772