Spring Boot加密属性文件数据

项目中敏感信息一般需要进行加密处理,比如数据库密码,Spring Boot内置不提供加密支持,不能加密属性文件的数据,在官方文档中提供了自定义Environment和Spring Cloud Vault两种解决方案。另外,可以使用jasypt-spring-boot

Jasypt Spring Boot

集成jasypt-spring-boot

有三种方式集成jasypt-spring-boot:

  • 项目中如使用了@SpringBootApplication或@EnableAutoConfiguration,简单地添加jasypt-spring-boot-starter到classpath将在整个Spring环境中启用加密属性
<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>2.1.0</version>
</dependency>
  • 添加jasypt-spring-boot到classpath,添加@EnableEncryptableProperties到main Configuration class将在整个Spring环境中启用加密属性
<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot</artifactId>
    <version>2.1.0</version>
</dependency>
@Configuration
@EnableEncryptableProperties
public class MyApplication {
    ...
}
  • 添加jasypt-spring-boot到classpath,使用@EncrytablePropertySource声明独立的加密属性文件
@Configuration
@EncryptablePropertySource(name = "EncryptedProperties", value = "classpath:encrypted.properties")
public class MyApplication {
    ...
}

或者使用@EncryptablePropertySources:

@Configuration
@EncryptablePropertySources({@EncryptablePropertySource("classpath:encrypted.properties"),
        @EncryptablePropertySource("file:/path/to/encrypted2.properties")})
public class MyApplication {
    ....
}

@EncryptablePropertySource也支持YAML文件。

加密配置

Key Required Default Value
jasypt.encryptor.password True -
jasypt.encryptor.algorithm False PBEWithMD5AndDES
jasypt.encryptor.bean False jasyptStringEncryptor
jasypt.encryptor.keyObtentionIterations False 1000
jasypt.encryptor.poolSize False 1
jasypt.encryptor.providerName False null
jasypt.encryptor.saltGeneratorClassname False org.jasypt.salt.RandomSaltGenerator
jasypt.encryptor.stringOutputType False base64
jasypt.encryptor.proxyPropertySources False false
jasypt.encryptor.property.prefix False ENC(
jasypt.encryptor.property.suffix False )

默认,加密算法为PBEWithMD5AndDES,加解密bean name为jasyptStringEncryptor,加密的密码使用ENC()包裹。
所有这些属性都可在属性文件中配置,但加密密码不应存储在属性文件中,而应使用系统属性、命令行参数传入,只要名称为jasypt.encryptor.password即可:

java -jar jasypt-spring-boot-demo.jar --jasypt.encryptor.password=password
或
java -Djasypt.encryptor.password=password -jar jasypt-spring-boot-demo.jar

也可以在application.properties 或 application.yml中使用环境变量:

jasypt.encryptor.password=${JASYPT_ENCRYPTOR_PASSWORD:}

配置文件示例:

spring:
  jpa:
    database-platform: org.hibernate.dialect.PostgreSQLDialect
    hibernate:
      ddl-auto: update
    properties:
      hibernate:
        default_schema: heroes
        format_sql: true
        jdbc:
          lob:
            non_contextual_creation: true
    show-sql: true
  datasource:
    platform: postgresql
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5432/postgres
    username: hero
    password: ENC(a3Ehaf0f/S1Rt6JfOGfQ+w==)
    initialization-mode: never
jasypt:
  encryptor:
    algorithm: PBEWithMD5AndDES
    password: 1qefhQH7mRR4LADVettR
    stringOutputType: base64
    property:
      prefix: ENC(
      suffix: )

生成加密的密码

使用CLI工具JasyptPBEStringEncryptionCLI生成加密密码,如下:

java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="password" password=secretkey algorithm=PBEWithMD5AndDES

执行后,输出如下:

----ENVIRONMENT-----------------

Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.191-b12 

----ARGUMENTS-------------------

algorithm: PBEWithMD5AndDES
input: hero
password: 1qefhQH7mRR4LADVettR

----OUTPUT----------------------

a3Ehaf0f/S1Rt6JfOGfQ+w==

生成后,使用ENC(加密的密码)替换明文密码即可。

自定义Environment

待续

Spring Cloud Vault

待续

原文地址:http://blog.51cto.com/7308310/2338146

时间: 2024-08-09 22:20:28

Spring Boot加密属性文件数据的相关文章

Spring读取加密属性文件处理

引言:Spring框架俨然已经是目前Java WEB项目开发的一个宠儿,更有人将Spring, Struts,和Hibernage称之为Java WEB项目开发的3件利器.Spring的依赖.注入.AOP及和其它框架的很好集成(如:hibername.ibatis.struts等)确实给web项目开发带来了诸多便利性,但是任何一种框架都不能完全满足个性化需求开发,spring亦是如此.现有一个项目是基于spring.struts和ibtatis的,其中数据库连接池使用的是proxool,领导要求

Spring Boot使用redis实现数据缓存

基于Spring Boot 1.5.2.RELEASE版本,一方面验证与Redis的集成方法,另外了解使用方法. 集成方法 配置依赖 修改pom.xml,增加如下内容. <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> 配置Redis

springboot(十七):使用Spring Boot上传文件

上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个Spring Boot上传文件的小案例. 1.pom包配置 我们使用Spring Boot最新版本1.5.9.jdk使用1.8.tomcat8.0. <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId>

Spring Boot 上传文件

1.上传文件 Spring Boot 上传文件也是使用 MultipartFile 类,和 Spring MVC 其实差不多,参考文章:https://www.cnblogs.com/jwen1994/p/11182923.html HTML <form enctype="multipart/form-data" method="post" action="/upload"> 文件:<input type="file&

Spring Boot(十七):使用 Spring Boot 上传文件

上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个 Spring Boot 上传文件的小案例. 1.pom 包配置 我们使用 Spring Boot 版本 2.1.0.jdk 1.8.tomcat 8.0. <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId&

spring boot application.properties文件外部配置

spring boot application.properties文件外部配置 官方文档 原文地址:https://www.cnblogs.com/lwmp/p/9836791.html

spring boot 使用POI导出数据到Excel表格

摘自:https://www.cnblogs.com/hopeofthevillage/p/12099807.html spring boot 使用POI导出数据到Excel表格 2019-12-26 00:17  全me村的希望  阅读(42)  评论(0)  编辑收藏 在spring boot 的项目经常碰到将数据导出到Excel表格的需求,而POI技术则对于java操作Excel表格提供了API,POI中对于多种类型的文档都提供了操作的接口,但是其对于Excel表格的操作无疑是最强大的.

Spring Boot 嵌入式 Tomcat 文件上传、url 映射虚拟路径

1.Java web 应用开发完成后如果是导入外置的 Tomcat 的 webapps 目录的话,那么上传的文件可以直接的放在应用的 web 目录下去就好了,浏览器可以很方便的进行访问. 2.Spring Boot 默认使用嵌入式 Tomcat ,将来打包成可执行 Jar 文件进行部署,显然打成 jar 包后,总不可能再将上传的文件放在 resources 目录下去了. 3.Spring Boot 于是提供了 url 地址匹配本地虚拟路径的功能: 1)上传文件到服务器,服务器将文件保存到了本地,

Spring Boot项目属性配置

接着上面的入门教程,我们来学习下Spring Boot的项目属性配置. 1.配置项目内置属性 属性配置主要是在application.properties文件里配置的(编写时有自动提示)这里我们将server的端口变为8888,路径加上HelloWorld: 在DeomApplication.java的页面时点击运行按钮,打开浏览器输入:http://localhost:8888/HelloWorld/hello 此时,控制台的输出信息也可以看到端口变成8888了: 之前的url已无效: 更改后