search
HomeJavajavaTutorialHow SpringBoot encrypts the SQL account password of the configuration file

1. Introduce dependencies into the Maven project

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

2. At the same time, you need to configure the password in the application.yml configuration file

because the tool uses this password for encryption or decryption. Therefore, you must ensure that the encrypted string in the configuration is encrypted with the same password, otherwise it will not be decrypted when the project is started.

jasypt:
    encryptor:
        password: 123456

3. You can

encrypt the original information in the test case. The method of use is very simple. The simple Demo is as follows:

@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class Test {
    @Autowired
    StringEncryptor encryptor;
 
    @Test
    public void getPass() {
        #直接调用加密的方法
        String mysql = encryptor.encrypt("mysql-username|mysql-password");
    }
}

4. Finally, just put the encrypted string in the configuration file

Such as the "mysql" string above

url: ENC(mysql==)
username: ENC(mysql==)
password: ENC(mysql=)

Pay attention to ENC ( ) is a fixed way of writing, and mysql== is your encrypted corresponding string.

In this way, you can pretend to encrypt sensitive information, hahaha, the reason why I say pretend. Many people may be confused by this issue because the encrypted password is stored in the configuration file in clear text and can be easily decrypted by anyone.

Yes, that is indeed the case. This kind of encryption method can only be said to not allow people to see the account password at once.

Even if the data is encrypted, it is meaningless after the hacker obtains access to your project, because encryption is just a false security measure. Therefore, it is the most basic and important to ensure the security of projects and servers.

OK!OK!OK!

Encrypt the configuration file

Students in development all know that, for example, project dependency information and database information are generally saved in the configuration file , and they are all plain text, so encryption processing is required. Today I will introduce the configuration of jasypt integrated with springboot encryption.

First of all, these are based on the premise that your springboot project can run normally.

Step one: Add dependencies to the pom file, as shown in the figure

A version is provided here

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

How SpringBoot encrypts the SQL account password of the configuration file

Step two: Generate the key

Find the jasypt jar package in your local maven warehouse, and open the cmd command window in that directory, as shown in the figure:

How SpringBoot encrypts the SQL account password of the configuration file

Execute

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

where Input is your plain text password, here I am demonstrating test, password is your private key, algorithm is a rule, do not change it! ! ! . After execution, as shown in the figure:

How SpringBoot encrypts the SQL account password of the configuration file

# Briefly explain, the OUTPUT here is the encrypted ciphertext (password). Here is a method to copy text using cmd (because I didn’t know how to copy using cmd at first): right-click the mouse to mark and select the content you want to copy, and then you can copy it.

Step 3: Add configuration to the springboot configuration file, as shown in the figure

How SpringBoot encrypts the SQL account password of the configuration file

Here I use the application.yml file, and the application.properties file is written like this :jasypt.encryptor.password=test.

But what is actually used is System.setProperty("jasypt.encryptor.password", "demo"); in the startup class to reassign the value between this node, which will help protect the private key again (if If anyone has a better solution, please leave a message in time and we can discuss it together.)

How SpringBoot encrypts the SQL account password of the configuration file

Note: The first password in the above picture corresponds to the password in ARGUEMENTS in the second step. , the second password corresponds to the result in OUTPUT in the second step, and the form must be added with ENC (you password), as shown in the figure.

Step 4: Decrypt the password through the command

Execute the command

java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBESTringDecryptionCLI input="nhyL4CzSQv/aPxoe7TzpOQ==" password=test algorithm=PBEWithMD5AndDES

The result is as shown below:

How SpringBoot encrypts the SQL account password of the configuration file

How SpringBoot encrypts the SQL account password of the configuration file

At the end, the integration of springboot and jasypt is completed. It seems to be quite easy to implement, haha. But one thing to note is that you must not leak your configuration file (especially the password in ARGUMENTS, which is equivalent to the private key), otherwise others can decrypt your password through the fourth step. It is recommended to put the configuration file in Configuration Center

The above is the detailed content of How SpringBoot encrypts the SQL account password of the configuration file. 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
Java Platform Independence: Compatibility with different OSJava Platform Independence: Compatibility with different OSMay 13, 2025 am 12:11 AM

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunondifferentoperatingsystemswithoutmodification.TheJVMcompilesJavacodeintoplatform-independentbytecode,whichittheninterpretsandexecutesonthespecificOS,abstractingawayOS

What features make java still powerfulWhat features make java still powerfulMay 13, 2025 am 12:05 AM

Javaispowerfulduetoitsplatformindependence,object-orientednature,richstandardlibrary,performancecapabilities,andstrongsecurityfeatures.1)PlatformindependenceallowsapplicationstorunonanydevicesupportingJava.2)Object-orientedprogrammingpromotesmodulara

Top Java Features: A Comprehensive Guide for DevelopersTop Java Features: A Comprehensive Guide for DevelopersMay 13, 2025 am 12:04 AM

The top Java functions include: 1) object-oriented programming, supporting polymorphism, improving code flexibility and maintainability; 2) exception handling mechanism, improving code robustness through try-catch-finally blocks; 3) garbage collection, simplifying memory management; 4) generics, enhancing type safety; 5) ambda expressions and functional programming to make the code more concise and expressive; 6) rich standard libraries, providing optimized data structures and algorithms.

Is Java Truly Platform Independent? How 'Write Once, Run Anywhere' WorksIs Java Truly Platform Independent? How 'Write Once, Run Anywhere' WorksMay 13, 2025 am 12:03 AM

JavaisnotentirelyplatformindependentduetoJVMvariationsandnativecodeintegration,butitlargelyupholdsitsWORApromise.1)JavacompilestobytecoderunbytheJVM,allowingcross-platformexecution.2)However,eachplatformrequiresaspecificJVM,anddifferencesinJVMimpleme

Demystifying the JVM: Your Key to Understanding Java ExecutionDemystifying the JVM: Your Key to Understanding Java ExecutionMay 13, 2025 am 12:02 AM

TheJavaVirtualMachine(JVM)isanabstractcomputingmachinecrucialforJavaexecutionasitrunsJavabytecode,enablingthe"writeonce,runanywhere"capability.TheJVM'skeycomponentsinclude:1)ClassLoader,whichloads,links,andinitializesclasses;2)RuntimeDataAr

Is java still a good language based on new features?Is java still a good language based on new features?May 12, 2025 am 12:12 AM

Javaremainsagoodlanguageduetoitscontinuousevolutionandrobustecosystem.1)Lambdaexpressionsenhancecodereadabilityandenablefunctionalprogramming.2)Streamsallowforefficientdataprocessing,particularlywithlargedatasets.3)ThemodularsystemintroducedinJava9im

What Makes Java Great? Key Features and BenefitsWhat Makes Java Great? Key Features and BenefitsMay 12, 2025 am 12:11 AM

Javaisgreatduetoitsplatformindependence,robustOOPsupport,extensivelibraries,andstrongcommunity.1)PlatformindependenceviaJVMallowscodetorunonvariousplatforms.2)OOPfeatureslikeencapsulation,inheritance,andpolymorphismenablemodularandscalablecode.3)Rich

Top 5 Java Features: Examples and ExplanationsTop 5 Java Features: Examples and ExplanationsMay 12, 2025 am 12:09 AM

The five major features of Java are polymorphism, Lambda expressions, StreamsAPI, generics and exception handling. 1. Polymorphism allows objects of different classes to be used as objects of common base classes. 2. Lambda expressions make the code more concise, especially suitable for handling collections and streams. 3.StreamsAPI efficiently processes large data sets and supports declarative operations. 4. Generics provide type safety and reusability, and type errors are caught during compilation. 5. Exception handling helps handle errors elegantly and write reliable software.

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 Article

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment