简体   繁体   中英

PropertyPlaceholderConfigurer and PropertySourcesPlaceholderConfigurer run differently

I used the DES algorithm to encrypt the username and password in jdbc.properties, and used the PropertyPlaceholderConfigurer for decryption, but found that this class has been deprecated. So use PropertySourcesPlaceholderConfigurer to replace.

The bean has been added in spring-dao.xml, and the class is filled with the class containing the decryption method inherited from PropertySourcesPlaceholderConfigurer.

I put a breakpoint on the first line of the decryption method , and then started tomcat to send an access request. At this point, the backend should call the database. But if the decryption class inherits from PropertySourcesPlaceholderConfigurer, the first line of the decryption method will not be executed. If the decryption class inherits from PropertyPlaceholderConfigurer, the first line of the decryption method is executed. I don't know why this is the case, should I use the deprecated PropertyPlaceholderConfigurer?

spring.version: 5.2.0.RELEASE

Part of spring-dao.xml

<!--<context:property-placeholder location="classpath:jdbc.properties" />-->
<bean class="com.imooc.o2o.util.EncryptPropertySourcesPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:jdbc.properties</value>
        </list>
    </property>
    <property name="fileEncoding" value="UTF-8" />
</bean>

EncryptPropertySourcesPlaceholderConfigurer.java (My decryption algorithm class)

package com.imooc.o2o.util;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

public class EncryptPropertySourcesPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer {
    // Fields to be encrypted
    private String[] encryptPropNames = {"jdbc.username", "jdbc.password"};

    /**
     * Transform key attributes
     * @param propertyName
     * @param propertyValue
     * @return
     */
    @Override
    protected String convertProperty(String propertyName, String propertyValue) {
        if (isEncryptProp(propertyName)) {
            // Decrypting encrypted fields
            String decryptValue = DESUtil.getDecryptString(propertyValue);
            return decryptValue;
        } else {
            return propertyValue;
        }
    }

    /**
     * Whether the attribute is encrypted
     * @param propertyName
     * @return
     */
    private boolean isEncryptProp(String propertyName) {
        // If it is equal to the field to be encrypted, it has been encrypted
        for (String encryptPropertyName : encryptPropNames) {
            if (encryptPropertyName.equals(propertyName)) {
                return true;
            }
        }
        return false;
    }
}

DESUtil.java

package com.imooc.o2o.util;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import java.security.Key;
import java.security.SecureRandom;
import java.util.Base64;

/**
 * DES is a symmetric encryption algorithm. The so-called symmetric encryption algorithm is an algorithm that uses the same key for encryption and decryption.
 */
public class DESUtil {
    private static Key key;
    private static String KEY_STR = "myKey";
    private static String CHAR_SET_NAME = "UTF-8";
    private static String ALGORITHM = "DES";

    static {
        try {
            // Generate DES Algorithm Object
            KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM);
            // Apply SHA1 security policy
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            // Setting the key seed
            secureRandom.setSeed(KEY_STR.getBytes());
            // Initialize SHA1-based algorithm objects
            generator.init(secureRandom);
            // Generate a key object
            key = generator.generateKey();
            generator = null;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    // Get encrypted information
    public static String getEncryptString(String str) {
        // Based on BASE64 encoding, receive byte [] and convert to String
        Base64.Encoder encoder = Base64.getEncoder();
        try {
            // Encoded as UTF-8
            byte[] bytes = str.getBytes(CHAR_SET_NAME);
            // Get the encrypted object
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            // Initialize password information
            cipher.init(Cipher.ENCRYPT_MODE, key);
            // encryption
            byte[] doFinal = cipher.doFinal(bytes);
            // byte[] to encode a good String and return
            return encoder.encodeToString(doFinal);
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

    // Get the decrypted information
    public static String getDecryptString(String str) {
        // Based on BASE64 encoding, receive byte[] and convert to String
        Base64.Decoder decoder = Base64.getDecoder();
        try {
            // Decode string into byte []
            byte[] bytes = decoder.decode(str);
            // Get the decrypted object
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            // Initialize decryption information
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] doFinal = cipher.doFinal(bytes);
            // Returns the decrypted information
            return new String(doFinal, CHAR_SET_NAME);
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

    /**
     * Get the string to be encrypted
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(getEncryptString(""));
        System.out.println(getEncryptString(""));
    }
}

Answer my own question, maybe because I did not use Spring Boot.

The implementation logic of these two classes is different. Spring Boot use PropertySourcesPlaceholderConfigurer.

This answer is not accurate. I am a beginner and I have not figured it out in many places. Please understand if the deviation is large.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM