search
HomeJavajavaTutorialHow Springboot integrates Jasypt to implement configuration file encryption

Introduction to Jasypt

Jasypt is a java library that allows a developer to add basic encryption functionality to his/her project with minimal effort and does not require a deep understanding of how encryption works

High-security, standards-based encryption technology for one-way and two-way encryption. Encrypt passwords, text, numbers, binary files...

Suitable for integration into Spring-based applications, open API, for any JCE provider...

Add the following dependencies:

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

Benefits of Jasypt

Protect the security of our system. Even if the code is leaked, the absolute security of the data source can be guaranteed.

Application Scenario

Encrypt all account passwords in the configuration file, as well as the things you want to encrypt.

How to use

Use the key to encrypt or decrypt the account or password, and decrypt the ciphertext when the project is started.

Practical use

Encryption and decryption tool class

import org.jasypt.util.text.BasicTextEncryptor;
/********************************************************************************
 ** @author : ZYJ
 ** @date :2023/04/26
 ** @description :Jasypt加密解密
 *********************************************************************************/
public class Jasypt {
    public static void main(String[] args) {
        BasicTextEncryptor basicTextEncryptor = new BasicTextEncryptor();
        //加密的密钥
        basicTextEncryptor.setPassword("Jasypt");
        //密码进行加密
        String encrypt = basicTextEncryptor.encrypt("密码:123456");
        //密码进行解密
        String decrypt = basicTextEncryptor.decrypt(encrypt);
        //结果输出
        System.out.println("加密后的结果:"+encrypt);
        System.out.println("加密后的结果:"+decrypt);
    }
}

Modify the configuration file, encrypt the database account password, use ENC() to include the ciphertext, put it in brackets, use The meaning of ENC() is to decrypt and load at startup.

How Springboot integrates Jasypt to implement configuration file encryption

Configure the key in the configuration file application.yml and use the specified key to decrypt

jasypt:
  encryptor:
    password: Jasypt

However, this method is not advisable because the code leaks the account password It is no different from plain text. You can configure the key in the environment variable and load it directly as a system environment variable! !

The key is specified through the environment variable. Modify the configuration file and replace the key with the environment variable name. In the form of ${environment variable name}, find the variable value through the environment variable name at startup. That is our key! ! !

jasypt:
encryptor:
#Environment variable variable name
password: ${JASYPT_PASSWORD}

Windows environment variable mode specification

Note: After configuring environment variables in Windows, you need to close IDEA and reopen it to let it load the environment variables.

How Springboot integrates Jasypt to implement configuration file encryption

Specify the Linux environment variable method

Edit the environment variable file

vim /etc/profile

Add at the bottom

export JASYPT_PASSWORD=Jasypt

Save and exit refresh environment variables

source /etc /profile

The above is the detailed content of How Springboot integrates Jasypt to implement configuration file encryption. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
Is Java Platform Independent if then how?Is Java Platform Independent if then how?May 09, 2025 am 12:11 AM

Java is platform-independent because of its "write once, run everywhere" design philosophy, which relies on Java virtual machines (JVMs) and bytecode. 1) Java code is compiled into bytecode, interpreted by the JVM or compiled on the fly locally. 2) Pay attention to library dependencies, performance differences and environment configuration. 3) Using standard libraries, cross-platform testing and version management is the best practice to ensure platform independence.

The Truth About Java's Platform Independence: Is It Really That Simple?The Truth About Java's Platform Independence: Is It Really That Simple?May 09, 2025 am 12:10 AM

Java'splatformindependenceisnotsimple;itinvolvescomplexities.1)JVMcompatibilitymustbeensuredacrossplatforms.2)Nativelibrariesandsystemcallsneedcarefulhandling.3)Dependenciesandlibrariesrequirecross-platformcompatibility.4)Performanceoptimizationacros

Java Platform Independence: Advantages for web applicationsJava Platform Independence: Advantages for web applicationsMay 09, 2025 am 12:08 AM

Java'splatformindependencebenefitswebapplicationsbyallowingcodetorunonanysystemwithaJVM,simplifyingdeploymentandscaling.Itenables:1)easydeploymentacrossdifferentservers,2)seamlessscalingacrosscloudplatforms,and3)consistentdevelopmenttodeploymentproce

JVM Explained: A Comprehensive Guide to the Java Virtual MachineJVM Explained: A Comprehensive Guide to the Java Virtual MachineMay 09, 2025 am 12:04 AM

TheJVMistheruntimeenvironmentforexecutingJavabytecode,crucialforJava's"writeonce,runanywhere"capability.Itmanagesmemory,executesthreads,andensuressecurity,makingitessentialforJavadeveloperstounderstandforefficientandrobustapplicationdevelop

Key Features of Java: Why It Remains a Top Programming LanguageKey Features of Java: Why It Remains a Top Programming LanguageMay 09, 2025 am 12:04 AM

Javaremainsatopchoicefordevelopersduetoitsplatformindependence,object-orienteddesign,strongtyping,automaticmemorymanagement,andcomprehensivestandardlibrary.ThesefeaturesmakeJavaversatileandpowerful,suitableforawiderangeofapplications,despitesomechall

Java Platform Independence: What does it mean for developers?Java Platform Independence: What does it mean for developers?May 08, 2025 am 12:27 AM

Java'splatformindependencemeansdeveloperscanwritecodeonceandrunitonanydevicewithoutrecompiling.ThisisachievedthroughtheJavaVirtualMachine(JVM),whichtranslatesbytecodeintomachine-specificinstructions,allowinguniversalcompatibilityacrossplatforms.Howev

How to set up JVM for first usage?How to set up JVM for first usage?May 08, 2025 am 12:21 AM

To set up the JVM, you need to follow the following steps: 1) Download and install the JDK, 2) Set environment variables, 3) Verify the installation, 4) Set the IDE, 5) Test the runner program. Setting up a JVM is not just about making it work, it also involves optimizing memory allocation, garbage collection, performance tuning, and error handling to ensure optimal operation.

How can I check Java platform independence for my product?How can I check Java platform independence for my product?May 08, 2025 am 12:12 AM

ToensureJavaplatformindependence,followthesesteps:1)CompileandrunyourapplicationonmultipleplatformsusingdifferentOSandJVMversions.2)UtilizeCI/CDpipelineslikeJenkinsorGitHubActionsforautomatedcross-platformtesting.3)Usecross-platformtestingframeworkss

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools