Spring Boot 配置文件这样加密,才足够安全!

2023-11-19

1. 前景

在使用Springboot时,通常很多信息都是在application.yml中直接明文配置的,比如数据库链接信息,redis链接信息等等。但是这样是不安全的。

所以需要对敏感数据进行加密,这样防止密码泄露

Jasypt这个库为我们解决了这个问题,实现了springboot配置的自定加密加密

2. 简单使用

源码对应地址:

http://gitlab.sea-clouds.cn/csdn/spring-boot-csdn/-/tree/master/05-spring-boot-jasypt

2.1 引入依赖

<properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.4.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <!-- web 和 测试 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- jdbc -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <!-- jasypt 加密 -->
    <dependency>
        <groupId>com.github.ulisesbocchio</groupId>
        <artifactId>jasypt-spring-boot-starter</artifactId>
        <version>3.0.3</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
</dependencies>

2.2 配置application信息

jasypt配置

jasypt:
  encryptor:
    # 加密算法
    algorithm: PBEWITHHMACSHA512ANDAES_256
    # 加密使用的盐
    password: jaspyt_password

2.3 加密解密测试

/**
 * @author HLH
 * @description: 加密解密测试
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class JasyptTest {

    @Autowired
    private StringEncryptor stringEncryptor;

    /**
     * 加密解密测试
     */
    @Test
    public void jasyptTest() {
        // 加密
        System.out.println(stringEncryptor.encrypt("root"));    // JSrINYe4IBotHndGjX1hnmY3mtPNUJlXjP12cx1+pHqUz2FNXGPu3Frnajh3QCXg
        // 解密
        System.out.println(stringEncryptor.decrypt("JSrINYe4IBotHndGjX1hnmY3mtPNUJlXjP12cx1+pHqUz2FNXGPu3Frnajh3QCXg"));    // root
    }

    /**
     * 手动测试
     */
    @Test
    public void test() {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword("jaspyt_password");
        config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
        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);
        System.out.println(encryptor.encrypt("root"));    // JSrINYe4IBotHndGjX1hnmY3mtPNUJlXjP12cx1+pHqUz2FNXGPu3Frnajh3QCXg
    }

}

3. 使用Jasypt加密后的字符串代替数据库密码

3.1 使用加密类进行加密

密码 root 加密之后 XjYnpGd3JGICnxumpFcfRP8J83m265yC/r1FiwLr9Yo1PNbPXQ2xykLHPpy02CZ1

/**
 * 数据库密码加密
 */
@Test
public void encryptPasswored() {
    // 加密
    System.out.println(stringEncryptor.encrypt("root"));    // XjYnpGd3JGICnxumpFcfRP8J83m265yC/r1FiwLr9Yo1PNbPXQ2xykLHPpy02CZ1
    // 解密
    System.out.println(stringEncryptor.decrypt("XjYnpGd3JGICnxumpFcfRP8J83m265yC/r1FiwLr9Yo1PNbPXQ2xykLHPpy02CZ1"));    // root
}

3.2 替换数据库配置

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.10.31/mp
    username: root
    # 使用ENC()包裹,标识为加密之后的,否则无法解密,会报错
    password: ENC(R2H69h1aEgJ3EDPLXAVQ5CxZJWtl8EvqIJUtlATRt6om4w46/J+blu2JAvkR7Yvp)

3.3 测试

@Autowired
private DataSource dataSource;
/**
 * 测试加密之后的数据源使用是否正常
 *  查看是否能正常获取链接
 */
@Test
public void datasourceTest() throws SQLException {
    Connection connection = dataSource.getConnection();
    System.out.println(connection);     // HikariProxyConnection@1487059223 wrapping com.mysql.cj.jdbc.ConnectionImpl@48904d5a
    connection.close();
}

4. Jasypt配置详解

所有配置都在JasyptEncryptorConfigurationProperties类中定义,我们只需要在yml中配置属性,即可达到重写的目的

Jasypt使用StringEncryptor来解密属性。如果Spring上下文中找不到自定义的StringEncryptor,就会自动创建一个,可以通过以下属性进行配置

唯一需要的属性是加密的盐,其余的可以使用默认值。虽然所有这些属性都可以在属性文件中生命,但加密所使用的盐不应该存储在属性文件中,而是应该通过系统属性、命令行参数或者环境变量传递,只要他的名称是jasypt.encryptor.password,它就可以工作。

倒数第二个属性jasypt.encryptor.proxyPropertySources用于只是jasypt spring boot如何拦截属性值进行解密。默认值false使用PropertySourceEnumerablePropertySourceMapPropertySource的自定义包装器实现。当为true时,拦截机制将在每个特定的PropertySource实现上使用CGLib代理。在某些必须保留原始PropertySource类型的场景中,这可能很有用。

5. 自定义加密

默认情况下,bean容器会配置LazyJasyptSringEncryptor

5.1 官方配置

官方配置的Bean都是在EncryptablePropertyResolverConfiguration中进行注入的

@Bean(
    name = {"lazyJasyptStringEncryptor"}
)
public StringEncryptor stringEncryptor(EnvCopy envCopy, BeanFactory bf) {
    String customEncryptorBeanName = envCopy.get().resolveRequiredPlaceholders(ENCRYPTOR_BEAN_PLACEHOLDER);
    boolean isCustom = envCopy.get().containsProperty("jasypt.encryptor.bean");
    return new DefaultLazyEncryptor(envCopy.get(), customEncryptorBeanName, isCustom, bf);
}

5.2 自定义加密

可以在Spring上下文中共自定义自己的StringEncryptor Bean,默认的加密程序将被忽略

注意

自定义Bean的名称必须为 jasyptStringEncryptor,否则解密不生效

自定义注入bean

/**
 * 加入 StringEncryptor 加密解密类
 *      beanName 必须为 jasyptStringEncryptor 才能是自定义的生效
 *      configProps 为jasypt框架中读取的配置类,就不用自己读取了
 */
@Bean("jasyptStringEncryptor")
public StringEncryptor jasyptStringEncryptor(Singleton<JasyptEncryptorConfigurationProperties> configProps) {
    PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
    JasyptEncryptorConfigurationProperties jasyptProperties = configProps.get();
    SimpleStringPBEConfig config = new SimpleStringPBEConfig();
    config.setPassword(jasyptProperties.getPassword());
    config.setAlgorithm(jasyptProperties.getAlgorithm());
    config.setKeyObtentionIterations(jasyptProperties.getKeyObtentionIterations());
    config.setPoolSize(jasyptProperties.getPoolSize());
    config.setProviderName(jasyptProperties.getProviderName());
    config.setSaltGeneratorClassName(jasyptProperties.getSaltGeneratorClassname());
    config.setIvGeneratorClassName(jasyptProperties.getIvGeneratorClassname());
    config.setStringOutputType(jasyptProperties.getStringOutputType());
    encryptor.setConfig(config);
    return encryptor;
}

6. 自定义属性探测器

属性探测器为判断一个属性值是否为加密后的字符串,并且截取真实字符串

6.1 官方处理流程

6.1.2 注入

EncryptablePropertyResolverConfiguration类中

@Bean(
    name = {"lazyEncryptablePropertyDetector"}
)
public EncryptablePropertyDetector encryptablePropertyDetector(EnvCopy envCopy, BeanFactory bf) {
    String customDetectorBeanName = envCopy.get().resolveRequiredPlaceholders(DETECTOR_BEAN_PLACEHOLDER);
    boolean isCustom = envCopy.get().containsProperty("jasypt.encryptor.property.detector-bean");
    return new DefaultLazyPropertyDetector(envCopy.get(), customDetectorBeanName, isCustom, bf);
}

6.1.2 DefaultLazyPropertyDetector

默认实现是DefaultLazyPropertyDetector,具体代码是

@Slf4j
public class DefaultLazyPropertyDetector implements EncryptablePropertyDetector {

    // 属性探测器
    private Singleton<EncryptablePropertyDetector> singleton;

    public DefaultLazyPropertyDetector(ConfigurableEnvironment environment, String customDetectorBeanName, boolean isCustom, BeanFactory bf) {
        singleton = new Singleton<>(() ->
                Optional.of(customDetectorBeanName)
                        .filter(bf::containsBean)
                        .map(name -> (EncryptablePropertyDetector) bf.getBean(name))
                        .map(tap(bean -> log.info("Found Custom Detector Bean {} with name: {}", bean, customDetectorBeanName)))
                        .orElseGet(() -> {
                            if(isCustom) {
                                throw new IllegalStateException(String.format("Property Detector custom Bean not found with name '%s'", customDetectorBeanName));
                            }
                            log.info("Property Detector custom Bean not found with name '{}'. Initializing Default Property Detector", customDetectorBeanName);
                            return createDefault(environment);
                        }));
    }

    public DefaultLazyPropertyDetector(ConfigurableEnvironment environment) {
        // 创建一个属性探测器
        singleton = new Singleton<>(() -> createDefault(environment));
    }

    private DefaultPropertyDetector createDefault(ConfigurableEnvironment environment) {
        // 读取所有的属性
        JasyptEncryptorConfigurationProperties props = JasyptEncryptorConfigurationProperties.bindConfigProps(environment);
        // 创建一个默认的属性探测器,读取配置文件中的前缀和后缀
        return new DefaultPropertyDetector(props.getProperty().getPrefix(), props.getProperty().getSuffix());
    }

    /**
      * 是否为解密格式字符串
      */
    @Override
    public boolean isEncrypted(String property) {
        return singleton.get().isEncrypted(property);
    }

    /**
      * 获取真是的加密后的字符串
      */
    @Override
    public String unwrapEncryptedValue(String property) {
        return singleton.get().unwrapEncryptedValue(property);
    }
}

在其中是创建了一个DefaultPropertyDetector对象

6.1.3 DefaultPropertyDetector

public class DefaultPropertyDetector implements EncryptablePropertyDetector {

    // 默认前缀和后缀
    private String prefix = "ENC(";
    private String suffix = ")";

    public DefaultPropertyDetector() {
    }

    public DefaultPropertyDetector(String prefix, String suffix) {
        Assert.notNull(prefix, "Prefix can't be null");
        Assert.notNull(suffix, "Suffix can't be null");
        this.prefix = prefix;
        this.suffix = suffix;
    }

    @Override
    public boolean isEncrypted(String property) {
        if (property == null) {
            return false;
        }
        final String trimmedValue = property.trim();
        return (trimmedValue.startsWith(prefix) &&
                trimmedValue.endsWith(suffix));
    }

    // 去掉前缀和后缀
    @Override
    public String unwrapEncryptedValue(String property) {
        return property.substring(
                prefix.length(),
                (property.length() - suffix.length()));
    }
}

6.2 自定义规则探测器

两种方式自定义

  • 提供一个名为encryptablePropertyDetectorEncryptablePropertyDetector类型的Bean来覆盖默认的实现

  • 如果提供的bean名称不为encryptablePropertyDetector,可以通过修改yml中的属性jasypt.encryptor.property.detector-Bean为自己的bean的名称。

方式

  • 要么自定义类

  • 要么修改yml中的前缀和后缀

6.2.1 自定义属性探测器,加入容器

/**
 * 自定义属性探测器
 *  beanName为 encryptablePropertyDetector
 */
@Bean(name = "encryptablePropertyDetector")
public EncryptablePropertyDetector encryptablePropertyDetector() {
    return new MyEncryptablePropertyDetector();
}


/**
 * @author HLH
 * @description: 自定义的属性探测器
 * @email 17703595860@163.com
 * @date : Created in 2021/8/19 20:01
 */
public class MyEncryptablePropertyDetector implements EncryptablePropertyDetector {

    /**
     * 是否为可以解密的字符串
     * @param value 全部的字符串
     * @return 是否是解密的字符串,true,是,false,否
     */
    @Override
    public boolean isEncrypted(String value) {
        if (value != null) {
            return value.startsWith("ENC@");    // 自定义规则为 ENC@开头
        }
        return false;
    }

    /**
     * 截取到除了标识之后的值
     * @param value 带前缀
     * @return string 去掉标识符的字符串
     */
    @Override
    public String unwrapEncryptedValue(String value) {
        return value.substring("ENC@".length());        // 截取ENC@之后的字符串
    }
}

yml中的配置

jasypt:
  encryptor:
    # 加密算法
    algorithm: PBEWITHHMACSHA512ANDAES_256
    # 加密使用的盐
    password: jaspyt_password
    property:
       # 修改默认的前缀和后缀,如果自定义属性探测器,那么此项配置不起作用
       # prefix: ENC_(
       # suffix: )
       # 自定义的属性探测器,如果这个是自定义的,那么上述的前缀后缀不生效
       detector-bean: encryptablePropertyDetector

6.2.2 修改yml中的配置

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.10.31/mp
    username: root
    # 使用ENC()包裹,标识为加密之后的,否则无法解密,会报错
    # 自定义规则之后,使用ENC@开头
    password: ENC@JSrINYe4IBotHndGjX1hnmY3mtPNUJlXjP12cx1+pHqUz2FNXGPu3Frnajh3QCXg

7. 自定义规则的前缀和后缀

在上述说明中,在DefaultLazyPropertyDetector中是默认是通过配置文件中的规则进行匹配的。默认规则是以ENC(开头,以)结尾,可以复写配置来自定义前缀和后缀

上面第6条是自定义了属性探测器,包括了定义规则和过滤字符串

如果只是想自定义前缀和后缀,那么可以直接修改yml中的配置来修改自定义的前缀和后缀

jasypt:
  encryptor:
    # 加密算法
    algorithm: PBEWITHHMACSHA512ANDAES_256
    # 加密使用的盐
    password: jaspyt_password
    property:
      # 修改默认的前缀和后缀,如果自定义属性探测器,那么此项配置不起作用
      prefix: ENC(
      suffix: )

8. 直接自定义解密规则

上述6和7自定义了解密字符串的规则和解密字符串的过滤,但是真正的解析处理还是Jasypt框架来负责的。我们也可以直接自定义解密的一系列流程。

8.1 官方处理流程

8.1.1 官方的注入

EncryptablePropertyResolverConfiguration类中

@Bean(
    name = {"lazyEncryptablePropertyResolver"}
)
public EncryptablePropertyResolver encryptablePropertyResolver(@Qualifier("lazyEncryptablePropertyDetector") EncryptablePropertyDetector propertyDetector, @Qualifier("lazyJasyptStringEncryptor") StringEncryptor encryptor, BeanFactory bf, EnvCopy envCopy, ConfigurableEnvironment environment) {
    String customResolverBeanName = envCopy.get().resolveRequiredPlaceholders(RESOLVER_BEAN_PLACEHOLDER);
    boolean isCustom = envCopy.get().containsProperty("jasypt.encryptor.property.resolver-bean");
    return new DefaultLazyPropertyResolver(propertyDetector, encryptor, customResolverBeanName, isCustom, bf, environment);
}

默认注入的是DefaultLazyPropertyResolver但是在其中创建的是EncryptablePropertyResolver对象

8.1.2 EncryptablePropertyResolver

  1. 官方默认是通过EncryptablePropertyResolver接口来处理解析字符串的

public interface EncryptablePropertyResolver {

    /**
     * 处理所有属性的解密处理
     * 如果为检测到加密规则,那么返回实际为相同的字符创
     *
     * @param value 属性值
     * @return 如果值未加密,返回原值,如果加密,返回加密之后的值
     */
    String resolvePropertyValue(String value);
}
  1. 其真实性使用的实现类是DefaultPropertyResolver用来真正处理解析。就是通过调用上文中的StringEncryptor处理解密,使用EncryptablePropertyDetector定义的解密字符串规则定义是否为加密的字符串

public class DefaultPropertyResolver implements EncryptablePropertyResolver {

    private final Environment environment;
    // 默认的或者自定义的StringEncryptor,用来解密
    private StringEncryptor encryptor;
    // 默认的或者自定义的EncryptablePropertyDetector,用来定义是否为加密的字符串
    private EncryptablePropertyDetector detector;

    public DefaultPropertyResolver(StringEncryptor encryptor, Environment environment) {
        this(encryptor, new DefaultPropertyDetector(), environment);
    }

    public DefaultPropertyResolver(StringEncryptor encryptor, EncryptablePropertyDetector detector, Environment environment) {
        this.environment = environment;
        Assert.notNull(encryptor, "String encryptor can't be null");
        Assert.notNull(detector, "Encryptable Property detector can't be null");
        this.encryptor = encryptor;
        this.detector = detector;
    }

    @Override
    public String resolvePropertyValue(String value) {
        return Optional.ofNullable(value)
                .map(environment::resolvePlaceholders)
                .filter(detector::isEncrypted)  // 如果经过属性探测器确认的,才继续
                .map(resolvedValue -> {
                    try {
                        String unwrappedProperty = detector.unwrapEncryptedValue(resolvedValue.trim()); // 过滤加密规则后的字符串
                        String resolvedProperty = environment.resolvePlaceholders(unwrappedProperty); 
                        return encryptor.decrypt(resolvedProperty); // 解密
                    } catch (EncryptionOperationNotPossibleException e) {
                        throw new DecryptionException("Unable to decrypt: " + value + ". Decryption of Properties failed,  make sure encryption/decryption " +
                                "passwords match", e);
                    }
                })
                .orElse(value);
    }
}

8.2 自定义的解密逻辑

编写自己的解密逻辑类

加入spring容器,命名为encryptablePropertyResolver,或者通过yml方式配置自定义bean名称

@Bean("encryptablePropertyResolver")
public EncryptablePropertyResolver encryptablePropertyResolver(
        StringEncryptor jasyptStringEncryptor, EncryptablePropertyDetector encryptablePropertyDetector) {
    return new MyEncryptablePropertyResolver(jasyptStringEncryptor, encryptablePropertyDetector);
}

/**
 * @author HLH
 * @description: 直接自定义解密规则
 * @email 17703595860@163.com
 * @date : Created in 2021/8/21 21:22
 */
public class MyEncryptablePropertyResolver implements EncryptablePropertyResolver {

    // 处理解密
    private final StringEncryptor encryptor;
    // 属性探测器
    private final EncryptablePropertyDetector detector;

    public MyEncryptablePropertyResolver(StringEncryptor encryptor, EncryptablePropertyDetector detector) {
        this.encryptor = encryptor;
        this.detector = detector;
    }

    /**
     * 处理真正的解密逻辑
     * @param value 原始值
     * @return 如果值未加密,返回原值,如果加密,返回加密之后的值
     */
    @Override
    public String resolvePropertyValue(String value) {
        return Optional.ofNullable(value)
                .filter(detector::isEncrypted)  // 如果经过属性探测器确认的,才继续
                .map(resolvedValue -> {
                    try {
                        String unwrappedProperty = detector.unwrapEncryptedValue(resolvedValue.trim()); // 过滤加密规则后的字符串
                        return encryptor.decrypt(unwrappedProperty); // 解密
                    } catch (EncryptionOperationNotPossibleException e) {
                        throw new DecryptionException("Unable to decrypt: " + value + ". Decryption of Properties failed,  make sure encryption/decryption " +
                                "passwords match", e);
                    }
                })
                .orElse(value);
    }
}

yml配置

jasypt:
  encryptor:
    # 加密算法
    algorithm: PBEWITHHMACSHA512ANDAES_256
    # 加密使用的盐
    password: jaspyt_password
    property:
       # 修改默认的前缀和后缀,如果自定义属性探测器,那么此项配置不起作用
       # prefix: ENC_(
       # suffix: )
       # 自定义的属性探测器,如果这个是自定义的,那么上述的前缀后缀不生效
       detector-bean: encryptablePropertyDetector
       # 自定义解密逻辑类 如果配置了,默认的解析器将不工作
       resolver-bean: encryptablePropertyResolver

9. 自定义过滤器

在Jasypt-spring-boot中,引入了过滤器

过滤器filter允许过滤某些属性,不进行解密。默认情况下,jasypt.encryptor开头的所有属性都会将从检查项中排除掉。这是为了配置Bean,在加载时循环依赖

9.1 默认处理流程

9.1.1 官方的注入

EncryptablePropertyResolverConfiguration类中

@Bean(
    name = {"lazyEncryptablePropertyFilter"}
)
public EncryptablePropertyFilter encryptablePropertyFilter(EnvCopy envCopy, ConfigurableBeanFactory bf) {
    String customFilterBeanName = envCopy.get().resolveRequiredPlaceholders(FILTER_BEAN_PLACEHOLDER);
    boolean isCustom = envCopy.get().containsProperty("jasypt.encryptor.property.filter-bean");
    return new DefaultLazyPropertyFilter(envCopy.get(), customFilterBeanName, isCustom, bf);
}

于上面的逻辑一样,在DefaultLazyPropertyFilter中其实是新建了一个EncryptablePropertyFilter对象,默认实现类是DefaultPropertyFilter

9.1.2 DefaultPropertyFilter

public class DefaultPropertyFilter implements EncryptablePropertyFilter {

    // 过滤的和包含的,优先读取配置文件的
    private final List<String> includeSourceNames;
    private final List<String> excludeSourceNames;
    private final List<String> includePropertyNames;
    private final List<String> excludePropertyNames;

    public DefaultPropertyFilter() {
        includeSourceNames = null;
        includePropertyNames = null;
        excludeSourceNames = null;
        excludePropertyNames = null;
    }

    public DefaultPropertyFilter(List<String> includeSourceNames, List<String> excludeSourceNames, List<String> includePropertyNames, List<String> excludePropertyNames) {
        this.includeSourceNames = includeSourceNames;
        this.excludeSourceNames = excludeSourceNames;
        this.includePropertyNames = includePropertyNames;
        this.excludePropertyNames = excludePropertyNames;
    }

    // 是否拦截
    @Override
    public boolean shouldInclude(PropertySource<?> source, String name) {
        // 如果上述四个都没有配置,那么全部放行
        if (isIncludeAll()) {
            return true;
        }

        // 如果是不包含的,返回false,就过滤掉了
        if (isMatch(source.getName(), excludeSourceNames) || isMatch(name, excludePropertyNames)) {
            return false;
        }

        // 如果是包含的,就放行
        return isIncludeUnset() || isMatch(source.getName(), includeSourceNames) || isMatch(name, includePropertyNames);

    }

    private boolean isIncludeAll() {
        return isIncludeUnset() && isExcludeUnset();
    }

    private boolean isIncludeUnset() {
        return isEmpty(includeSourceNames) && isEmpty(includePropertyNames);
    }

    private boolean isExcludeUnset() {
        return isEmpty(excludeSourceNames) && isEmpty(excludePropertyNames);
    }

    private boolean isEmpty(List<String> patterns) {
        return patterns == null || patterns.isEmpty();
    }

    // 传递的配置其实是正则,进行正则匹配
    private boolean isMatch(String name, List<String> patterns) {
        return name != null && !isEmpty(patterns) && patterns.stream().anyMatch(name::matches);
    }
}

9.2 自定义过滤器

方式

  • 要么自定义过滤器

  • 要么修改jasypt.encryptor.property.include-names或者jasypt.encryptor.property.exclude-names配置拦截和放行的资源key

自定义过滤器类

加入spring容器,命名为encryptablePropertyFilter

/**
 * 自定义的属性拦截器
 * @param configProps Jasypt官方读取的配置集合
 * @return 自定义属性拦截器
 */
@Bean(name="encryptablePropertyFilter")
public EncryptablePropertyFilter encryptablePropertyFilter(
        Singleton<JasyptEncryptorConfigurationProperties> configProps) {
    return new MyEncryptablePropertyFilter(configProps.get());
}

/**
 * @author HLH
 * @description: 自定义的属性过滤器
 * @email 17703595860@163.com
 * @date : Created in 2021/8/22 13:37
 */
public class MyEncryptablePropertyFilter implements EncryptablePropertyFilter {

    /** jasypt 的所有配置*/
    JasyptEncryptorConfigurationProperties jasyptProperties;

    public MyEncryptablePropertyFilter(JasyptEncryptorConfigurationProperties jasyptProperties) {
        this.jasyptProperties = jasyptProperties;
    }

    @Override
    public boolean shouldInclude(PropertySource<?> source, String name) {
        List<String> excludeNames = jasyptProperties.getProperty().getFilter().getExcludeNames();
        List<String> includeNames = jasyptProperties.getProperty().getFilter().getIncludeNames();
        if (CollectionUtils.isEmpty(includeNames) && CollectionUtils.isEmpty(excludeNames)) {
            return true;
        }

        if (isMatch(source.getName(), excludeNames) || isMatch(source.getName(), excludeNames)) {
            return false;
        }

        return CollectionUtils.isEmpty(includeNames) ||
                isMatch(source.getName(), includeNames) ||
                isMatch(name, includeNames);

    }

    /**
     * 正则判断,如果满足,返回true,如果不满足,返回false
     * @param name 配置的key
     * @param patterns 正则列表
     * @return 如果满足,返回true,如果不满足,返回false
     */
    private boolean isMatch(String name, List<String> patterns) {
        return name != null && !CollectionUtils.isEmpty(patterns) && patterns.stream().anyMatch(name::matches);
    }

}

yml配置

jasypt:
  encryptor:
    # 加密算法
    algorithm: PBEWITHHMACSHA512ANDAES_256
    # 加密使用的盐
    password: jaspyt_password
    property:
      # 修改默认的前缀和后缀,如果自定义属性探测器,那么此项配置不起作用
      # prefix: ENC_(
      # suffix: )
      # 自定义的属性探测器,如果这个是自定义的,那么上述的前缀后缀不生效
      detector-bean: encryptablePropertyDetector
      # 自定义解密逻辑类 如果配置了,默认的解析器将不工作
      resolver-bean: encryptablePropertyResolver
      # 过滤器的bean
      filter-bean: encryptablePropertyFilter
      # 过滤器配置,正则
      filter:
        # 默认包含的
        include-names:
        # 默认拦截的,默认拦截jasypt.encryptor的配置
        exclude-names:
          - ^jasypt\.encryptor\.*

10. 使用mvn插件加密解密

使用代码的方式比较不方便,还需要编码实现,如果不想编码,简单的进行加密解密,就可以使用maven的插件,使用mvn命令进行加密解密

10.1 引入Jasypt的maven插件

<build>
    <plugins>
        <!-- Jasypt 的maven插件 -->
        <plugin>
            <groupId>com.github.ulisesbocchio</groupId>
            <artifactId>jasypt-maven-plugin</artifactId>
            <version>3.0.2</version>
        </plugin>
    </plugins>
</build>

10.2 加密

使用jasypt-maven-plugin插件加密明文密码:(如果配置项是默认值,可以不指定)

mvn jasypt:encrypt-value -Djasypt.encryptor.password="jaspyt_password" -Djasypt.plugin.value="root" -Djasypt.encryptor.algorithm="PBEWITHHMACSHA512ANDAES_256"
  • jasypt.encryptor.password 是秘钥,尽量复杂!不能放在代码和配置文件里面!不能泄漏

  • jasypt.plugin.value 是要加密的明文密码

  • jasypt.encryptor.algorithm默认加密算法是PBEWITHHMACSHA512ANDAES_256,需要有JCE(Java Cryptography Extension)支持,如果不想安装JCE,可以使用PBEWithMD5AndDES算法。windows下的jdk自带

进入项目所在的目录,输入命令,成功加密

10.3 解密

使用jasypt-maven-plugin插件解密密文密码:(如果配置项是默认值,可以不指定)

mvn jasypt:decrypt-value -Djasypt.encryptor.password="jaspyt_password" -Djasypt.plugin.value="pqsp6kvVfBcKoEltxP9MilGGRo8EE506mDWAuTFIKePDXMeArta13bT6Hl8QqVlC" -Djasypt.encryptor.algorithm="PBEWITHHMACSHA512ANDAES_256"
  • jasypt.encryptor.password 是秘钥,尽量复杂!不能放在代码和配置文件里面!不能泄漏

  • jasypt.plugin.value 是要加密的明文密码,有ENC()包裹或者不包裹都可以

  • jasypt.encryptor.algorithm默认加密算法是PBEWITHHMACSHA512ANDAES_256,需要有JCE(Java Cryptography Extension)支持,如果不想安装JCE,可以使用PBEWithMD5AndDES算法。windows下的jdk自带

进入项目所在的目录,输入命令,成功加密

11. 思维导图

最后再来一张思维导图        

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring Boot 配置文件这样加密,才足够安全! 的相关文章

  • 将元素添加到数组java中

    布局是这样的 index num 0 10 1 20 2 30 Add 35 here 3 40 Move elements down 4 50 5 60 6 70 那么我的方法是这样的 public static void method
  • 使用 BlobOutputStream 在 Azure 中上传 blob

    我正在尝试直接从流上传 blob 因为我不知道我决定尝试的流的长度这个答案 https stackoverflow com a 24621538 3695939 这不起作用 即使它从流中读取并且不会抛出任何异常 内容也不会上传到我的容器 我
  • string.split("(?!^)") 解释

    我正在尝试将字符串的字符拆分为字符串数组 我找到了解决方案here https stackoverflow com questions 5235401 split string into array of character strings
  • 从SQLite列中获取所有数字字符串并进行总和计算

    我是 Android 和 SQLite 的新手 我在 SQLite 中有一个只有数字的 AMOUNT 列 我可以在 ListView 中显示它 但我无法找到任何我理解的方法来将它们全部添加并显示在 TextView 中 这是数据库助手 im
  • Spring MVC - 自动查找验证器

    假设我有一个像这样的示例实体类 public class Address 和相应的验证器 Component public AddressValidator implements Validator Override public bool
  • Java 7u51/7u55 带星号的清单变量

    我正在部署一个小程序 其中包含清单中的下一个变量 Manifest Version 2 0 Ant Version Apache Ant 1 8 2 Trusted Library true Permissions all permissi
  • 从 Eclipse 导出后,WAR 文件中缺少一些必要的库 - 为什么?

    我接手了一个大学的项目 其中包含一些 Web 服务 通过将项目导出为 WAR 文件 一些库包含在文件中 例如 Axis2 而另一些则不包含 hibernate JDBC 驱动程序 另外 添加到类路径中的 jar 尚未导出 所有库都位于硬盘驱
  • selenium 2.0 中的 isElementPresent

    大家好 我正在使用 webdriver 所以如果我想使用 selenium s rc 函数 isElementPresent 我必须模拟 selenium rc 所以我会执行以下操作 import org openqa selenium B
  • Java中的字节和字符转换

    如果我将一个字符转换为byte然后回到char 那个角色神秘地消失了 变成了别的东西 这怎么可能 这是代码 char a line 1 byte b byte a line 2 char c char b line 3 System out
  • 如何对 jar 文件资源使用 File.separator?

    我正在尝试读取位于 jar 文件中的属性文件 我想使用 File separator 因为应用程序将在多个平台上运行 我正在构建路径如下 jarFilePath jar file jarFile getAbsolutePath jarPro
  • 使用 POJO 仅更新 JOOQ 记录中已更改的字段

    我想使用 POJO 作为源来更新 JOOQ 记录中已更改的字段 Record from Object http www jooq org javadoc 3 8 x org jooq Record html from java lang O
  • Java 套接字 - 读和写

    问题 客户端没有收到任何消息 这是客户端和服务器的完整代码 CLIENT public class Client extends Socket public Client String hostName int port throws Un
  • 检查更新时 Maven 无限期挂起

    我正在使用 Maven 构建一个项目 我是新手 并且它挂起 mvn package INFO Scanning for projects INFO INFO Building Presentation Reports INFO task s
  • Netty Nio java 中的通信

    我想在 Netty nio 中创建一个具有两个客户端和一个服务器的通信系统 更具体地说 首先 我希望当两个客户端与服务器连接时从服务器发送消息 然后能够在两个客户端之间交换数据 我正在使用本示例提供的代码 https github com
  • 如何根据从 jtextfield 和组合框接收的值将数据行添加到 Jtable

    我有一个JFrame表格有JTextFields JCombobox等等 我能够将这些值接收到变量 现在我想将接收到的数据添加到JTable当用户单击 添加 或类似的操作时在新行中 我创造了JTable使用 net beans 的问题是将这
  • 有没有办法删除 JShell 中的导入?

    我正在发现 JShell 并且发现默认添加的导入 jshell gt imports import java io import java math import java net import java nio file import j
  • Unix 纪元时间转 Java Date 对象

    我有一个包含以下内容的字符串UNIX 纪元时间 https en wikipedia org wiki Unix time 我需要将其转换为 Java Date 对象 String date 1081157732 DateFormat df
  • AWS SQS Batch SendMessageBatchRequest 非常慢

    我的应用程序使用 SendMessageBatchRequest 将每个请求发布 10 条消息到 AWS SQS 每条消息的大小小于250字节 该应用程序预计每天发布约一百万条记录 但要实现这一目标 消息发布的速度非常慢 AmazonSQS
  • 使用 Retrofit 获取原始 HTTP 响应

    我想从我的 API REST 获取原始 http 响应 我尝试过这个界面 POST login FormUrlEncoded Call
  • 为什么我们不能在函数式接口中重载抽象方法? (爪哇)

    所以我熟悉java中的函数式接口 以及它们与lambda表达式的使用 一个函数式接口只能包含一个抽象方法 当从 lambda 表达式使用这一孤独方法时 您不需要指定其名称 因为接口中只有一个抽象方法 编译器知道这就是您正在引用的方法 Exa

随机推荐

  • ngrok服务端搭建并使用docker解放80端口

    start 前言 为什么要搭建ngrok服务端 为什么使用docker 1 开发环境下调试微信公众号使用 要求80端口 2 ngrok配置中要指定 http的端口 如果指定80端口的话 会和nginx抢端口 nginx肯定比ngrok重要
  • Ajax核心技术之XMLHttpRequest对象

    XMLHttpRequest对象到底是什么 跟Ajax到底有什么联系 在了解它之前还是要先了解一下Ajax的功能 与以往的技术不同 Ajax是为了实现异步操作 那么关于异步 好像一个管理者安排好一个项目计划后 将这个项目交给下属去做 而自己
  • 揭秘win10系统CPU占用100%的真正原因/找出那些罪魁祸首

    经常会有 Win10 用户反应 电脑没有运行太多程序 但是在任务管理器中 经常可以看到电脑CPU占用率却一直居高不下 那么 CPU占用100 的正真原因是什么呢 下面小编收集了一些针对CPU占用过高的原因及解决办法 这些可能就是导致你CPU
  • Spring Boot 快速入门、开发环境热部署

    SpringBoot快速上手 准备工作 我们将学习如何快速的创建一个Spring Boot应用 并且实现一个简单的Http请求处理 通过这个例子对Spring Boot有一个初步的了解 并体验其结构简单 开发快速的特性 我的环境准备 jav
  • Sping之自动注入-1

    最近终于能静下心来 一步步的学习Java Web开发 在学习的过程中 遇到太多的问题 一开始好些问题真是不知道怎么解决 在这里要非常感谢 Sping In Action 一书的作者 感谢他能写出此书 让我受益匪浅 您辛苦了 本着 相互学习
  • linux查看已安装的软件

    这本阿里P8撰写的算法笔记 再次推荐给大家 身边不少朋友学完这本书最后加入大厂 Github 疯传 史上最强悍 阿里大佬 LeetCode刷题手册 开放下载了 因为linux安装软件的方式比较多 所以没有一个通用的办法能查到某些软件是否安装
  • docker run之后 docker ps 不显示运行中的容器

    docker run 启动mysql以后 生成的对应容器直接exited 1 问题 今天在部署项目过程中 用docker run指令启动容器返回了容器id 但是用docker ps指令却不显示刚才启动的容器 问题查找 由于docker ps
  • L298N 小车应用(附代码)

    L298N L298N是目前智能小车应用很广泛的价格也比较便宜的电机驱动 用来驱动直流电机 L298N 输出A 输出B 分别接两个直流电机 电机两根线随便接 如果发现两电机方向是反着的 就调换下接线就ok了 12V供电 这个是外部电源为驱动
  • kubernetes集群实战——暴露service供外部访问的4种方法(NodePort、LoadBalancer、ExternalName和分配公有IP)

    1 service介绍 Service可以看作是一组提供相同服务的Pod对外的访问接口 借助Service 应用可以方便地实现服务发现和负载均衡 service默认只支持4层负载均衡能力 没有7层功能 可以通过Ingress实现 servi
  • android studio3.1调试

    快捷键 ctrl alt left或者ctrl alt right 回退 前进 双击快捷键 shift 全局搜索 快捷键 shift F9 开始调试 快捷键 F6 单步执行程序 快捷键 F5 单步执行程序 遇到方法时进入 快捷键 F8 调到
  • 机器学习算法——Kmeans

    1 k mean算法的原理 1 选取K个点做为初始聚集的簇心 2 分别计算每个样本点到K个簇核心的距离 这里的距离一般取欧氏距离或余弦距离 找到离该点最近的簇核心 将它归属到对应的簇 3 所有点都归属到簇之后 M个点就分为了K个簇 之后重新
  • element 时间日期选择器限制选择范围为7天

    template 部分
  • 编写递归算法,计算二叉树叶子结点的数目。

    编写递归算法 计算二叉树叶子结点的数目 编写递归算法 计算二叉树叶子结点的数目 include stdio h 包含 getchar scanf printf include malloc h malloc 动态申请空间 函数 二叉树 结点
  • 服务器的地址信息,服务器地址信息

    服务器地址信息 内容精选 换一换 可以一次添加一台服务器 也可以一次添加同一网段连续IP的多台服务器 进入任务中心可以查看状态信息 如果状态为成功 说明服务器已添加成功 如果要自定义裸金属服务器的DNS服务器信息 需要将裸金属服务器网络设置
  • 《算法导论》笔记(15) 最小生成树 部分习题

    习题23 1 11 给定图G和一棵最小生成树T 假设减少了位于T之外的某条边的权重 因为T内的边 是连接所有结点的权重最小的 那么首先将T外的减少权重的边 u v 加入T 然后在u v中寻找所有的路径 去掉路径中权重最大的边 习题23 2
  • 5、H3C交换机ACL配置

    创建 IPv4 高级 ACL 并进入 IPv4 高级 ACL 视图 switch acl number acl number name acl name 注 IPv4 高级 ACL 的编号范围为3000 3999 创建规则 switch a
  • 无法打开计算机开始菜单,开始菜单,教您开始菜单无法打开

    win10系统开始菜单不能使用该如何是好呢 最近有位用户反映在使用win10系统的过程当中 发现开始菜单没莫名其妙的就出现了打不开的情况 这该怎么办呢 不要着急 小编这就来将处理开始菜单打不开这个问题的办法来分享给你们 我们在使用电脑的过程
  • 使用Docker Compose搭建Redis主从集群

    一 本次环境使用到了docker docker compose redis 二 我们搭建的主从集群结构如图 共包含三个节点 一个主节点 两个从节点 这里我们会在同一台虚拟机中开启3个redis实例 模拟主从集群 信息如下 IP PORT 角
  • 超越nnFormer!UNETR++:高效准确的3D医学图像分割

    UNETR Delving into Efficient and Accurate 3D Medical Image Segmentation 论文链接 https arxiv org abs 2212 04497 代码链接 https g
  • Spring Boot 配置文件这样加密,才足够安全!

    1 前景 在使用Springboot时 通常很多信息都是在application yml中直接明文配置的 比如数据库链接信息 redis链接信息等等 但是这样是不安全的 所以需要对敏感数据进行加密 这样防止密码泄露 Jasypt这个库为我们