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

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

不言
Release: 2019-02-19 15:56:53
forward
6168 people have browsed it

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

<!-- Jasypt加密 -->
<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>2.0.0</version>
    </dependency>
Copy after login

Three: Create one of our toolkits

Code sample: JasyptUtils.java

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

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=="));
    }

}
Copy after login

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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template