Spring 中的.properties 属性(jdbc.password)加密

 

 

1、复写processProperties方法实现 

 

package com.*.*;

import java.util.Properties;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; 

public class PropertyPlaceholderConfigurerTest extends PropertyPlaceholderConfigurer {
	  protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) 
              throws BeansException { 
		  String password = props.getProperty("jdbc.password"); 
		  String username = props.getProperty("jdbc.username"); 
		  try {
		  if (password != null) { 
              //解密jdbc.password属性值,并重新设置 
		//Crypt.decrypt:自己编写的对应的解密方法(本文采用aes算法,aes网上有很多就不写出来了)
		props.setProperty("jdbc.password", Crypt.decrypt(password));
		  		} 
		  if (username != null) { 
	        props.setProperty("jdbc.username", Crypt.decrypt(username));
		  } 
		  super.processProperties(beanFactory, props);
		  } catch (Exception e) {
				e.printStackTrace();
			} 
	  }
}

  

<!-- 不加密时候使用使用spring自带的 -->


2、xml配置

<!-- 不加密时候使用使用spring自带的 -->

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="locations">
     <list>
 <value>classpath:jdbc.properties</value>   <value>classpath:roles.properties</value>
     </list>
  </property>
</bean>

 

<!-- 加密时候使用自己复写过的 -->

<bean id="propertyConfigurer"
class="com.*.*.PropertyPlaceholderConfigurerTest">
  <property name="locations">
    <list>
 <value>classpath:jdbc.properties</value>
    <value>classpath:roles.properties</value>
    </list>
  </property>
</bean>

 

3、原文

jdbc.username=F4B2DEB6F8FA201C5783A959CE1C3180
jdbc.password=F4B2DEB6F8FA201C5783A959CE1C3180

 

转载于:https://my.oschina.net/u/2376377/blog/827208

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于外部的application.propertiesjdbc配置进行加密,可以采用如下方法: 首先,需要引入加密工具,例如Jasypt(Java simplified encryption)等。在项目的pom.xml文件添加对应的依赖项。 然后,在应用的配置文件(如application.properties配置数据库连接信息,如下所示: ``` spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=myusername spring.datasource.password=mysecretpassword ``` 接下来,在代码使用Jasypt对这些敏感信息进行加密和解密。可以创建一个Encryptor类,负责加密和解密操作。 ```java import org.jasypt.encryption.pbe.PBEStringEncryptor; import org.jasypt.encryption.pbe.StandardPBEStringEncryptor; public class Encryptor { private static final String ENCRYPT_PASSWORD = "yourEncryptionPassword"; public static void main(String[] args) { // 加密 System.out.println("Encrypted password: " + encrypt("mysecretpassword")); // 解密 System.out.println("Decrypted password: " + decrypt("encryptedPassword")); } private static String encrypt(String value) { PBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); encryptor.setPassword(ENCRYPT_PASSWORD); return encryptor.encrypt(value); } private static String decrypt(String value) { PBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); encryptor.setPassword(ENCRYPT_PASSWORD); return encryptor.decrypt(value); } } ``` 在配置文件使用加密后的值,如下所示: ``` spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=myusername spring.datasource.password=ENC(encryptedPassword) ``` 当应用启动时,会自动调用Encryptor类的encrypt方法对密码进行加密,并将加密后的密码存储在application.properties。在使用密码时,会自动调用Encryptor类的decrypt方法进行解密。这样就可以保护数据库密码,避免了明文存储的安全隐患。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值