Placeholders (placeholder) are often used in spring configuration files to load resource files, and often some resource files are stored in clear text, such as JDBC configuration information, which is not always displayed in clear form from a system security standpoint. Today contacted Jasypt, looked up some information to study the next. Jasypt is an open source project on SourceForge.net and is a Java library. Read more about Google yourself.
The first step is to join the Jasypt dependency. Here we use Maven.
<dependency> <groupId>org.jasypt</groupId> <artifactId>jasypt</artifactId> <vers Ion>1.8</version></dependency>
Encryption:
@Testpublic void Encrypt () {//create dongle standardpbestringencryptor encryptor = new Standardpbestringencryptor (); Configuration environmentstringpbeconfig config = new Environmentstringpbeconfig (); Config.setalgorithm ("Pbewithmd5anddes");//Cryptographic Algorithm Config.setpassword ("Fuyung");//System attribute value Encryptor.setconfig (config); String plaintext = "root"; PlainText String ciphertext = encryptor.encrypt (plaintext); Encryption System.out.println (plaintext + ":" + ciphertext);//Run Result: root:8y9g4kizquchb78mmjnkhw==}
Decrypt:
@Testpublic void Decrypt () {Standardpbestringencryptor encryptor = new Standardpbestringencryptor (); Environmentstringpbeconfig config = new Environmentstringpbeconfig (); Config.setalgorithm ("Pbewithmd5anddes"); Config.setpassword ("Fuyung"); Encryptor.setconfig (config); String ciphertext = "8y9g4kizquchb78mmjnkhw==";//ciphertext//decrypt string plaintext = Encryptor.decrypt (ciphertext); Decrypt System.out.println (ciphertext + ":" + plaintext);//Run Result: 8y9g4kizquchb78mmjnkhw==: Root}
Integrated with spring. Add the following code to the spring configuration file:
<bean id= "Propertyconfigure" class= " Org.jasypt.spring.properties.EncryptablePropertyPlaceholderConfigurer "> < constructor-arg ref= "Configurationencryptor"/> <property name= " Systempropertiesmodename " value=" System_properties_mode_override "/> < Property name= "Ignoreresourcenotfound" value= "true"/> <property Name= "Locations" > <list> <value>classpath:jdbc.properties</value> </list> </property></bean>< Bean id= "Configurationencryptor" class= " Org.jasypt.encryption.pbe.StandardPBEStringEncryptor "> <property name= "config" ref= "environmentvariablesconfiguration"/></bean><bean id= " Environmentvariablesconfiguration " class=" Org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig "> <property name = "algorithm" value= "pbewithmd5anddes"/> <property name= "password" Value= "CLM"/></bean>
Check out the Jdbc.properties file:
Jdbc.username = ENC (KPKWMXAX2LMUQQKKPCULPTIMXZNTDXXW)
Jdbc.password = ENC (wg/u1ymqoznh4wyp7hpttjl0v1kgflic)
Remember to add the enc prefix to your ciphertext and wrap it up with (). Why write this, look at the source code will know:
This will automatically decrypt when the spring container is started and the encrypted value is read.
To encrypt a database configuration file using Jasypt