Spring Boot集成Jasypt安全框架

简介:

  Jasypt安全框架提供了Spring的集成,主要是实现

PlaceholderConfigurerSupport类或者其子类。


   在Sring 3.1之后,则推荐使用PropertySourcesPlaceholderConfigurer类作为属性替换配置类,这里Spring集成Jasypt则使用Jasypt对属性替换配置类的实现。EncryptablePropertySourcesPlaceholderConfigurer。

   

   在Spring中集成比较容易,而且Jasypt官方也给出了配置Bean的方式和使用Jasypt标签的XML方式,而Spring boot集成就稍微有点不一样,需要创建一个自动配置类,并且创建一个注入PlaceholderConfigurerSupport的jasypt实现了的Bean .


    

   下面是一个使用示例:

    

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
import  org.jasypt.encryption.pbe.StandardPBEByteEncryptor;
import  org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import  org.jasypt.spring31.properties.EncryptablePropertySourcesPlaceholderConfigurer;
import  org.springframework.boot.autoconfigure.AutoConfigureOrder;
import  org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import  org.springframework.boot.autoconfigure.condition.SearchStrategy;
import  org.springframework.context.annotation.Bean;
import  org.springframework.context.annotation.Configuration;
import  org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import  org.springframework.core.Ordered;
import  org.springframework.core.io.ClassPathResource;
 
/**
  * Author : secondriver
  * Date   : 2016/5/26
  */
@Configuration
@AutoConfigureOrder (Ordered.HIGHEST_PRECEDENCE)
public  class  EncryptPropertyPlaceholderAutoConfiguration {
 
     private  static  final  String SECURITY_PROPERTIES_FILE =  "security.properties" ;
 
     @Bean
     @ConditionalOnMissingBean (search = SearchStrategy.CURRENT)
     public  static  PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
         StandardPBEStringEncryptor encryptor =  new  StandardPBEStringEncryptor();
         encryptor.setAlgorithm(StandardPBEByteEncryptor.DEFAULT_ALGORITHM);
         encryptor.setPassword( "security" );
         EncryptablePropertySourcesPlaceholderConfigurer
                 configurer =  new  EncryptablePropertySourcesPlaceholderConfigurer(encryptor);
         configurer.setLocation( new  ClassPathResource(SECURITY_PROPERTIES_FILE));
         return  configurer;
     }
}


  配置文件的写入和Spring XML的基本类似。application.yml相当于applicationContext.xml,security.properties就是要进行属性替换的配置文件。


  application.yml:

1
2
3
4
5
spring:
   datasource:
     url: jdbc:mysql: / / localhost: 3306 / abc?useSSL = false
     username: root
     password: ${jdbc.password}


  security.properties:

  

1
jdbc.password=ENC(jWgGELCkuxRuCI2Aqa6cF9VCxYpuKEZr)



   创建数据源的时候在使用属性参数时,会对ENC()中的内容进行解密,达到认证成功,创建数据源完成。



本文转自 secondriver 51CTO博客,原文链接:http://blog.51cto.com/aiilive/1784180,如需转载请自行联系原作者

相关文章
|
3月前
|
安全 算法 Java
在Spring Boot中应用Jasypt以加密配置信息。
通过以上步骤,可以在Spring Boot应用中有效地利用Jasypt对配置信息进行加密,这样即使配置文件被泄露,其中的敏感信息也不会直接暴露给攻击者。这是一种在不牺牲操作复杂度的情况下提升应用安全性的简便方法。
916 10
|
5月前
|
前端开发
SpringBoot2.3.1集成Knife4j接口文档
SpringBoot2.3.1集成Knife4j接口文档
540 44
|
4月前
|
JSON 分布式计算 大数据
springboot项目集成大数据第三方dolphinscheduler调度器
springboot项目集成大数据第三方dolphinscheduler调度器
243 3
|
4月前
|
缓存 JSON 前端开发
第07课:Spring Boot集成Thymeleaf模板引擎
第07课:Spring Boot集成Thymeleaf模板引擎
499 0
第07课:Spring Boot集成Thymeleaf模板引擎
|
4月前
|
Java 关系型数据库 MySQL
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
505 2
|
4月前
|
分布式计算 Java 大数据
springboot项目集成dolphinscheduler调度器 可拖拽spark任务管理
springboot项目集成dolphinscheduler调度器 可拖拽spark任务管理
267 2
|
分布式计算 大数据 Java
springboot项目集成大数据第三方dolphinscheduler调度器 执行/停止任务
springboot项目集成大数据第三方dolphinscheduler调度器 执行/停止任务
75 0
|
4月前
|
存储 人工智能 Java
Springboot集成AI Springboot3 集成阿里云百炼大模型CosyVoice2 实现Ai克隆语音(未持久化存储)
本项目基于Spring Boot 3.5.3与Java 17,集成阿里云百炼大模型CosyVoice2实现音色克隆与语音合成。内容涵盖项目搭建、音色创建、音频合成、音色管理等功能,适用于希望快速掌握Spring Boot集成语音AI技术的开发者。需提前注册阿里云并获取API Key。
|
分布式计算 Java 大数据
springboot项目集成dolphinscheduler调度器 项目管理
springboot项目集成dolphinscheduler调度器 项目管理
113 0
下一篇