Home>Article>Java> SpringBoot integrates Jasypt security framework and configuration file content encryption (code example)

SpringBoot integrates Jasypt security framework and configuration file content encryption (code example)

不言
不言 forward
2019-02-19 15:56:53 6049browse

The content this article brings to you is about SpringBoot integrating Jasypt security framework and configuration file content encryption (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Our yml or properties configuration files in the SpringBoot project are all in clear text, which is relatively less secure. We all know that the configuration file contains some database connection username and password, some third-party keys and other information. So let’s be careful and use encryption.

The Jasypt security framework is used here.

One: Introduce the jar package into pom. key

  com.github.ulisesbocchio jasypt-spring-boot-starter 2.0.0 

Three: Create one of our toolkits

Code sample: JasyptUtils.java

# 配置文件加密key jasypt: encryptor: password: panther

Four: How to use

The above toolkit has a main method, fill in the key you configured, then fill in the value you need to encrypt, and run it directly.

Similar to the database connection in the configuration file

package com.zhuang.common.utils; import org.jasypt.encryption.pbe.PooledPBEStringEncryptor; import org.jasypt.encryption.pbe.StandardPBEByteEncryptor; import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig; /** * @Created with Intellij IDEA * @Author : payne * @Date : 2018/5/18 - 10:37 * @Copyright (C), 2018-2018 * @Descripition : Jasypt安全框架加密类工具包 */ public class JasyptUtils { /** * Jasypt生成加密结果 * * @param password 配置文件中设定的加密密码 jasypt.encryptor.password * @param value 待加密值 * @return */ public static String encryptPwd(String password, String value) { PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor(); encryptOr.setConfig(cryptOr(password)); String result = encryptOr.encrypt(value); return result; } /** * 解密 * * @param password 配置文件中设定的加密密码 jasypt.encryptor.password * @param value 待解密密文 * @return */ public static String decyptPwd(String password, String value) { PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor(); encryptOr.setConfig(cryptOr(password)); String result = encryptOr.decrypt(value); return result; } public static SimpleStringPBEConfig cryptOr(String password) { SimpleStringPBEConfig config = new SimpleStringPBEConfig(); config.setPassword(password); config.setAlgorithm(StandardPBEByteEncryptor.DEFAULT_ALGORITHM); config.setKeyObtentionIterations("1000"); config.setPoolSize("1"); config.setProviderName("SunJCE"); config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator"); config.setStringOutputType("base64"); return config; } public static void main(String[] args) { // 加密 System.out.println(encryptPwd("panther", "root")); // 解密 System.out.println(decyptPwd("panther", "GfP4qfnrJeqMvzN1nOemIQ==")); } }

The above is the detailed content of SpringBoot integrates Jasypt security framework and configuration file content encryption (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete