gpt4 book ai didi

java - PropertyPlaceholderConfigurer 和 PropertySourcesPlaceholderConfigurer 的运行方式不同

转载 作者:行者123 更新时间:2023-12-01 19:37:56 26 4
gpt4 key购买 nike

我使用DES算法对jdbc.properties中的用户名和密码进行加密,并使用PropertyPlaceholderConfigurer进行解密,但发现该类已被弃用。所以使用PropertySourcesPlaceholderConfigurer来代替。

spring-dao.xml中已经添加了bean,并且类中填充了包含继承自PropertySourcesPlaceholderConfigurer的解密方法的类。

我在解密方法的第一行打了断点,然后启动tomcat发送访问请求。此时,后端应该调用数据库。 但是如果解密类继承自PropertySourcesPlaceholderConfigurer,则解密方法的第一行不会被执行。如果解密类继承自PropertyPlaceholderConfigurer,则执行解密方法的第一行。我不知道为什么会这样,是否应该使用已弃用的PropertyPlaceholderConfigurer?

spring.版本:5.2.0.RELEASE

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(我的解密算法类)

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

最佳答案

回答我自己的问题,可能是因为我没有使用Spring Boot。

这两个类的实现逻辑是不同的。 Spring Boot 使用 PropertySourcesPlaceholderConfigurer。

这个答案不准确。我是初学者,很多地方没搞明白。偏差较大请谅解。

关于java - PropertyPlaceholderConfigurer 和 PropertySourcesPlaceholderConfigurer 的运行方式不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59194639/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com