Java Spring JDBC模板-解密密码
我正在使用SpringJDBC模板建立与数据库的连接。密码是加密格式的,希望在JDBC模板的帮助下对其进行解密。为了实现相同的功能,我知道我必须重写DriverManager数据源。我尝试实现相同的功能,但没有成功Java Spring JDBC模板-解密密码,java,spring,encryption,jdbc,Java,Spring,Encryption,Jdbc,我正在使用SpringJDBC模板建立与数据库的连接。密码是加密格式的,希望在JDBC模板的帮助下对其进行解密。为了实现相同的功能,我知道我必须重写DriverManager数据源。我尝试实现相同的功能,但没有成功 <bean id="dataSource" class="MyclassName"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <pro
<bean id="dataSource"
class="MyclassName">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
}
我知道我必须更改我自己编写的,每当需要替换变量时就会调用它
jdbc.password={base64}yourEncryptedSecret
import java.io.IOException;
import sun.misc.BASE64Decoder;
public class PropertyPlaceholderConfigurer
extends org.springframework.beans.factory.config.PropertyPlaceholderConfigurer {
@Override
protected String convertPropertyValue(final String originalValue) {
String cryptString = "{base64}";
if (originalValue.startsWith(cryptString)) {
BASE64Decoder decoder = new BASE64Decoder();
String decodedPassword = null;
try {
decodedPassword = new String(decoder.decodeBuffer(originalValue.substring(cryptString.length())));
} catch (IOException e) {
e.printStackTrace();
}
return decodedPassword;
}
return originalValue;
}
}
感谢您的帮助…我已经为我的应用程序实现了相同的功能。然而,我仍然想知道为什么问题中提出的方法不起作用。
import java.io.IOException;
import sun.misc.BASE64Decoder;
public class PropertyPlaceholderConfigurer
extends org.springframework.beans.factory.config.PropertyPlaceholderConfigurer {
@Override
protected String convertPropertyValue(final String originalValue) {
String cryptString = "{base64}";
if (originalValue.startsWith(cryptString)) {
BASE64Decoder decoder = new BASE64Decoder();
String decodedPassword = null;
try {
decodedPassword = new String(decoder.decodeBuffer(originalValue.substring(cryptString.length())));
} catch (IOException e) {
e.printStackTrace();
}
return decodedPassword;
}
return originalValue;
}
}