Spring Boot集成jasypt对配置文件application.properties的属性值进行加密

1、引入jasypt依赖

		<dependency>
		    <groupId>com.github.ulisesbocchio</groupId>
		    <artifactId>jasypt-spring-boot-starter</artifactId>
		    <version>3.0.3</version>
		</dependency>

2、在application.properties中添加密钥配置:jasypt.encryptor.password=密钥值

jasypt.encryptor.password=testEncryptPassword

3、对明文进行加密

3.1、添加类注解StringEncryptor

3.2、注入StringEncryptor

3.3、调用StringEncryptor.encrypt进行加密

3.4、代码示例:

TestService.java:

package com.zjr.donny.helloworld.service;

public interface TestService {
	String jasyptEncrypt(String text);
}

TestServiceImpl.java:

package com.zjr.donny.helloworld.service.impl;

import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import com.zjr.donny.helloworld.service.TestService;

@Service
@EnableEncryptableProperties
public class TestServiceImpl implements TestService {
	@Autowired
	private StringEncryptor stringEncryptor;

	@Override
	public String jasyptEncrypt(String text) {
		String encryptedValue = stringEncryptor.encrypt(text);
		System.out.println(text + " 加密值:" + encryptedValue);
		return encryptedValue;
	}

}

在Controller中使用TestService:

package com.zjr.donny.helloworld.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zjr.donny.helloworld.service.TestService;


@RestController
public class TestController {
	@Autowired
	private TestService testService;

	@RequestMapping("/encrypt/{text}")
	public String encrypt(@PathVariable String text) {
		return testService.jasyptEncrypt(text);
	}

}

启动springboot后,在地址栏车输入需要加密密码

如:http://localhost:8080/encrypt/8080

(加密spring.datasource.driver-class-name的值com.mysql.cj.jdbc.Driver ),结果为:fN6wrENNHnIPNunoN6694iCjYmP0hcobB66zesRFJrlxryONtEtGDUMzEw5fS/SmUKeB29HNwzIQ78jKoebj9Q==

4、把加密后的值替换到配置文件application.properties,用“ENC(”作为前缀,“)”作为后缀包起来

spring.datasource.driver-class-name=ENC(fN6wrENNHnIPNunoN6694iCjYmP0hcobB66zesRFJrlxryONtEtGDUMzEw5fS/SmUKeB29HNwzIQ78jKoebj9Q==)

如果需要修改前后缀的值,在application.properties添加配置:

例:

jasypt.encryptor.property.prefix=HELLO[
jasypt.encryptor.property.suffix=]

5、使用自定义的加密方法

5.1、对步骤3中的TestServiceImpl.java添加Bean,实现StringEncryptor接口,重写里面的encrypt与decrypt方法

TestServiceImpl.java:

package com.zjr.donny.helloworld.service.impl;

import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import com.zjr.donny.helloworld.service.TestService;

@Service
@EnableEncryptableProperties
public class TestServiceImpl implements TestService {
	@Bean("myStringEncryptor")
	public StringEncryptor getStringEncryptor(Environment environment) {
		String defaultPassword = "xxxx";
		String jasyptPassword = environment.getRequiredProperty("jasypt.encryptor.password") == null ? defaultPassword
				: environment.getRequiredProperty("jasypt.encryptor.password");
		return new StringEncryptor() {
			@Override
			public String encrypt(String s) {
				return jasyptPassword + s;
			}

			@Override
			public String decrypt(String s) {
				System.err.println("jasypt password is " + jasyptPassword);
				return s.substring(jasyptPassword.length());
			}
		};
	}

	@Autowired
	private StringEncryptor stringEncryptor;

	@Override
	public String jasyptEncrypt(String text) {
		String encryptedValue = stringEncryptor.encrypt(text);
		System.out.println(text + " 加密值:" + encryptedValue);
		return encryptedValue;
	}

}

5.2、application.properties里添加配置,指定上一步5.1的类为加密类

spring.datasource.driver-class-name=ENC(testEncryptPasswordcom.mysql.cj.jdbc.Driver)

jasypt.encryptor.password=testEncryptPassword
jasypt.encryptor.bean=myStringEncryptor

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
对于外部的application.properties中的jdbc配置进行加密,可以采用如下方法: 首先,需要引入加密工具,例如Jasypt(Java simplified encryption)等。在项目的pom.xml文件中添加对应的依赖项。 然后,在应用的配置文件(如application.properties)中配置数据库连接信息,如下所示: ``` spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=myusername spring.datasource.password=mysecretpassword ``` 接下来,在代码中使用Jasypt对这些敏感信息进行加密和解密。可以创建一个Encryptor类,负责加密和解密操作。 ```java import org.jasypt.encryption.pbe.PBEStringEncryptor; import org.jasypt.encryption.pbe.StandardPBEStringEncryptor; public class Encryptor { private static final String ENCRYPT_PASSWORD = "yourEncryptionPassword"; public static void main(String[] args) { // 加密 System.out.println("Encrypted password: " + encrypt("mysecretpassword")); // 解密 System.out.println("Decrypted password: " + decrypt("encryptedPassword")); } private static String encrypt(String value) { PBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); encryptor.setPassword(ENCRYPT_PASSWORD); return encryptor.encrypt(value); } private static String decrypt(String value) { PBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); encryptor.setPassword(ENCRYPT_PASSWORD); return encryptor.decrypt(value); } } ``` 在配置文件中使用加密后的,如下所示: ``` spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=myusername spring.datasource.password=ENC(encryptedPassword) ``` 当应用启动时,会自动调用Encryptor类中的encrypt方法对密码进行加密,并将加密后的密码存储在application.properties中。在使用密码时,会自动调用Encryptor类中的decrypt方法进行解密。这样就可以保护数据库密码,避免了明文存储的安全隐患。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一码归一码归一码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值