Procházet zdrojové kódy

配置文件中的密码使用工具类DESUtil.java加密

shuzheng před 9 roky
rodič
revize
beb85a8550

+ 2 - 2
zheng-cms/zheng-cms-dao/src/main/resources/jdbc.properties

@@ -1,9 +1,9 @@
 master.jdbc.driver=com.mysql.jdbc.Driver
 master.jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/zheng?useUnicode\=true&characterEncoding\=utf-8&autoReconnect\=true
 master.jdbc.username=root
-master.jdbc.password=123456
+master.jdbc.password=RwfC38sMIAc=
 
 slave.jdbc.driver=com.mysql.jdbc.Driver
 slave.jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/zheng?useUnicode\=true&characterEncoding\=utf-8&autoReconnect\=true
 slave.jdbc.username=root
-slave.jdbc.password=123456
+slave.jdbc.password=RwfC38sMIAc=

+ 7 - 7
zheng-cms/zheng-cms-dao/src/main/resources/redis.properties

@@ -1,7 +1,7 @@
-ip=127.0.0.1
-port=6379
-password=
-max_active=500
-max_idle=5
-max_wait=10000
-timeout=10000
+master.redis.ip=127.0.0.1
+master.redis.port=6379
+master.redis.password=z4SkijYyEBc=
+master.redis.max_active=500
+master.redis.max_idle=5
+master.redis.max_wait=10000
+master.redis.timeout=10000

+ 32 - 0
zheng-common/src/main/java/com/zheng/common/plugin/EncryptPropertyPlaceholderConfigurer.java

@@ -0,0 +1,32 @@
+package com.zheng.common.plugin;
+
+import com.zheng.common.util.DESUtil;
+import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
+
+/**
+ * 支持加密配置文件插件
+ * Created by ZhangShuzheng on 2017/2/4.
+ */
+public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
+
+	private String[] propertyNames = {
+			"master.jdbc.password", "slave.jdbc.password", "master.redis.password"
+	};
+
+	/**
+	 * 解密指定propertyName的加密属性值
+	 * @param propertyName
+	 * @param propertyValue
+	 * @return
+	 */
+	@Override
+	protected String convertProperty(String propertyName, String propertyValue) {
+		for (String p : propertyNames) {
+			if (p.equalsIgnoreCase(propertyName)) {
+				return DESUtil.getDecryptString(propertyValue);
+			}
+		}
+		return super.convertProperty(propertyName, propertyValue);
+	}
+
+}

+ 83 - 0
zheng-common/src/main/java/com/zheng/common/util/DESUtil.java

@@ -0,0 +1,83 @@
+package com.zheng.common.util;
+
+import sun.misc.BASE64Decoder;
+import sun.misc.BASE64Encoder;
+
+import javax.crypto.Cipher;
+import javax.crypto.KeyGenerator;
+import java.security.Key;
+import java.security.SecureRandom;
+
+/**
+ * DES加密算法工具类
+ * Created by ZhangShuzheng on 2017/2/4.
+ */
+public class DESUtil {
+
+	private static Key key;
+	private static String KEY_STR = "zheng";        // 密钥
+	private static String CHARSETNAME = "UTF-8";    // 编码
+	private static String ALGORITHM = "DES";        // 加密类型
+
+	static {
+		try {
+			KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM);
+			generator.init(new SecureRandom(KEY_STR.getBytes()));
+			key = generator.generateKey();
+			generator = null;
+		} catch (Exception e) {
+			throw new RuntimeException(e);
+		}
+	}
+
+	/**
+	 * 对str进行DES加密
+	 * @param str
+	 * @return
+	 */
+	public static String getEncryptString(String str) {
+		BASE64Encoder base64encoder = new BASE64Encoder();
+		try {
+			byte[] bytes = str.getBytes(CHARSETNAME);
+			Cipher cipher = Cipher.getInstance(ALGORITHM);
+			cipher.init(Cipher.ENCRYPT_MODE, key);
+			byte[] doFinal = cipher.doFinal(bytes);
+			return base64encoder.encode(doFinal);
+		} catch (Exception e) {
+			throw new RuntimeException(e);
+		}
+	}
+
+	/**
+	 * 对str进行DES解密
+	 * @param str
+	 * @return
+	 */
+	public static String getDecryptString(String str) {
+		BASE64Decoder base64decoder = new BASE64Decoder();
+		try {
+			byte[] bytes = base64decoder.decodeBuffer(str);
+			Cipher cipher = Cipher.getInstance(ALGORITHM);
+			cipher.init(Cipher.DECRYPT_MODE, key);
+			byte[] doFinal = cipher.doFinal(bytes);
+			return new String(doFinal, CHARSETNAME);
+		} catch (Exception e) {
+			throw new RuntimeException(e);
+		}
+	}
+
+	public static void main(String[] args) {
+		String[] keys = {
+			"", "123456"
+		};
+		System.out.println("key | encryptString | decryptString");
+		for (String key : keys) {
+			System.out.print(key + " | ");
+			String encryptString = getEncryptString(key);
+			System.out.print(encryptString + " | ");
+			String decryptString = getDecryptString(encryptString);
+			System.out.println(decryptString);
+		}
+	}
+
+}

+ 160 - 160
zheng-common/src/main/java/com/zheng/common/util/RedisUtil.java

@@ -15,165 +15,165 @@ import java.util.concurrent.locks.ReentrantLock;
  */
 public class RedisUtil {
 
-    protected static ReentrantLock lockPool = new ReentrantLock();
-    protected static ReentrantLock lockJedis = new ReentrantLock();
-
-    private static Logger logger = LoggerFactory.getLogger(RedisUtil.class);
-
-    //Redis服务器IP
-    private static String IP = PropertiesFileUtil.getInstance("redis").get("ip");
-
-    //Redis的端口号
-    private static int PORT = PropertiesFileUtil.getInstance("redis").getInt("port");
-
-    //访问密码
-    private static String PASSWORD = PropertiesFileUtil.getInstance("redis").get("password");
-    //可用连接实例的最大数目,默认值为8;
-    //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
-    private static int MAX_ACTIVE = PropertiesFileUtil.getInstance("redis").getInt("max_active");
-
-    //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
-    private static int MAX_IDLE = PropertiesFileUtil.getInstance("redis").getInt("max_idle");
-
-    //等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;
-    private static int MAX_WAIT = PropertiesFileUtil.getInstance("redis").getInt("max_wait");
-
-    //超时时间
-    private static int TIMEOUT = PropertiesFileUtil.getInstance("redis").getInt("timeout");
-
-    //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
-    private static boolean TEST_ON_BORROW = true;
-
-    private static JedisPool jedisPool = null;
-
-    /**
-     * redis过期时间,以秒为单位
-     */
-    public final static int EXRP_HOUR = 60 * 60;            //一小时
-    public final static int EXRP_DAY = 60 * 60 * 24;        //一天
-    public final static int EXRP_MONTH = 60 * 60 * 24 * 30;    //一个月
-
-    /**
-     * 初始化Redis连接池
-     */
-    private static void initialPool() {
-        try {
-            JedisPoolConfig config = new JedisPoolConfig();
-            config.setMaxTotal(MAX_ACTIVE);
-            config.setMaxIdle(MAX_IDLE);
-            config.setMaxWaitMillis(MAX_WAIT);
-            config.setTestOnBorrow(TEST_ON_BORROW);
-            //jedisPool = new JedisPool(config, IP.split(",")[0], PORT, TIMEOUT, PASSWORD);
-            jedisPool = new JedisPool(config, IP.split(",")[0], PORT, TIMEOUT);
-        } catch (Exception e) {
-            logger.error("First create JedisPool error : " + e);
-            try {
-                //如果第一个IP异常,则访问第二个IP
-                JedisPoolConfig config = new JedisPoolConfig();
-                config.setMaxTotal(MAX_ACTIVE);
-                config.setMaxIdle(MAX_IDLE);
-                config.setMaxWaitMillis(MAX_WAIT);
-                config.setTestOnBorrow(TEST_ON_BORROW);
-                //jedisPool = new JedisPool(config, IP.split(",")[1], PORT, TIMEOUT, PASSWORD);
-                jedisPool = new JedisPool(config, IP.split(",")[1], PORT, TIMEOUT);
-            } catch (Exception e2) {
-                logger.error("Second create JedisPool error : " + e2);
-            }
-        }
-    }
-
-    /**
-     * 在多线程环境同步初始化
-     */
-    private static synchronized void poolInit() {
-        if (jedisPool == null) {
-            initialPool();
-        }
-    }
-
-
-    /**
-     * 同步获取Jedis实例
-     * @return Jedis
-     */
-    public synchronized static Jedis getJedis() {
-        if (jedisPool == null) {
-            poolInit();
-        }
-        Jedis jedis = null;
-        try {
-            if (jedisPool != null) {
-                jedis = jedisPool.getResource();
-            }
-        } catch (Exception e) {
-            logger.error("Get jedis error : " + e);
-        } finally {
-            returnResource(jedis);
-        }
-        return jedis;
-    }
-
-    /**
-     * 释放jedis资源
-     * @param jedis
-     */
-    public static void returnResource(final Jedis jedis) {
-        if (jedis != null && jedisPool != null) {
-            jedisPool.returnResource(jedis);
-        }
-    }
-
-    /**
-     * 设置 String
-     * @param key
-     * @param value
-     */
-    public synchronized static void set(String key, String value) {
-        try {
-            value = StringUtils.isEmpty(value) ? "" : value;
-            getJedis().set(key, value);
-        } catch (Exception e) {
-            logger.error("Set key error : " + e);
-        }
-    }
-
-    /**
-     * 设置 过期时间
-     * @param key
-     * @param value
-     * @param seconds 以秒为单位
-     */
-    public synchronized static void set(String key, String value, int seconds) {
-        try {
-            value = StringUtils.isEmpty(value) ? "" : value;
-            getJedis().setex(key, seconds, value);
-        } catch (Exception e) {
-            logger.error("Set keyex error : " + e);
-        }
-    }
-
-    /**
-     * 获取String值
-     * @param key
-     * @return value
-     */
-    public synchronized static String get(String key) {
-        if (getJedis() == null || !getJedis().exists(key)) {
-            return null;
-        }
-        return getJedis().get(key);
-    }
-
-    /**
-     * 删除值
-     * @param key
-     */
-    public synchronized static void remove(String key) {
-        try {
-            getJedis().del(key);
-        } catch (Exception e) {
-            logger.error("Remove keyex error : " + e);
-        }
-    }
+	protected static ReentrantLock lockPool = new ReentrantLock();
+	protected static ReentrantLock lockJedis = new ReentrantLock();
+
+	private static Logger logger = LoggerFactory.getLogger(RedisUtil.class);
+
+	// Redis服务器IP
+	private static String IP = PropertiesFileUtil.getInstance("redis").get("master.redis.ip");
+
+	// Redis的端口号
+	private static int PORT = PropertiesFileUtil.getInstance("redis").getInt("master.redis.port");
+
+	// 访问密码
+	private static String PASSWORD = DESUtil.getDecryptString(PropertiesFileUtil.getInstance("redis").get("master.redis.password"));
+	// 可用连接实例的最大数目,默认值为8;
+	// 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
+	private static int MAX_ACTIVE = PropertiesFileUtil.getInstance("redis").getInt("master.redis.max_active");
+
+	// 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
+	private static int MAX_IDLE = PropertiesFileUtil.getInstance("redis").getInt("master.redis.max_idle");
+
+	// 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;
+	private static int MAX_WAIT = PropertiesFileUtil.getInstance("redis").getInt("master.redis.max_wait");
+
+	// 超时时间
+	private static int TIMEOUT = PropertiesFileUtil.getInstance("redis").getInt("master.redis.timeout");
+
+	// 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
+	private static boolean TEST_ON_BORROW = true;
+
+	private static JedisPool jedisPool = null;
+
+	/**
+	 * redis过期时间,以秒为单位
+	 */
+	public final static int EXRP_HOUR = 60 * 60;            //一小时
+	public final static int EXRP_DAY = 60 * 60 * 24;        //一天
+	public final static int EXRP_MONTH = 60 * 60 * 24 * 30;    //一个月
+
+	/**
+	 * 初始化Redis连接池
+	 */
+	private static void initialPool() {
+		try {
+			JedisPoolConfig config = new JedisPoolConfig();
+			config.setMaxTotal(MAX_ACTIVE);
+			config.setMaxIdle(MAX_IDLE);
+			config.setMaxWaitMillis(MAX_WAIT);
+			config.setTestOnBorrow(TEST_ON_BORROW);
+			//jedisPool = new JedisPool(config, IP.split(",")[0], PORT, TIMEOUT, PASSWORD);
+			jedisPool = new JedisPool(config, IP.split(",")[0], PORT, TIMEOUT);
+		} catch (Exception e) {
+			logger.error("First create JedisPool error : " + e);
+			try {
+				//如果第一个IP异常,则访问第二个IP
+				JedisPoolConfig config = new JedisPoolConfig();
+				config.setMaxTotal(MAX_ACTIVE);
+				config.setMaxIdle(MAX_IDLE);
+				config.setMaxWaitMillis(MAX_WAIT);
+				config.setTestOnBorrow(TEST_ON_BORROW);
+				//jedisPool = new JedisPool(config, IP.split(",")[1], PORT, TIMEOUT, PASSWORD);
+				jedisPool = new JedisPool(config, IP.split(",")[1], PORT, TIMEOUT);
+			} catch (Exception e2) {
+				logger.error("Second create JedisPool error : " + e2);
+			}
+		}
+	}
+
+	/**
+	 * 在多线程环境同步初始化
+	 */
+	private static synchronized void poolInit() {
+		if (jedisPool == null) {
+			initialPool();
+		}
+	}
+
+
+	/**
+	 * 同步获取Jedis实例
+	 * @return Jedis
+	 */
+	public synchronized static Jedis getJedis() {
+		if (jedisPool == null) {
+			poolInit();
+		}
+		Jedis jedis = null;
+		try {
+			if (jedisPool != null) {
+				jedis = jedisPool.getResource();
+			}
+		} catch (Exception e) {
+			logger.error("Get jedis error : " + e);
+		} finally {
+			returnResource(jedis);
+		}
+		return jedis;
+	}
+
+	/**
+	 * 释放jedis资源
+	 * @param jedis
+	 */
+	public static void returnResource(final Jedis jedis) {
+		if (jedis != null && jedisPool != null) {
+			jedisPool.returnResource(jedis);
+		}
+	}
+
+	/**
+	 * 设置 String
+	 * @param key
+	 * @param value
+	 */
+	public synchronized static void set(String key, String value) {
+		try {
+			value = StringUtils.isEmpty(value) ? "" : value;
+			getJedis().set(key, value);
+		} catch (Exception e) {
+			logger.error("Set key error : " + e);
+		}
+	}
+
+	/**
+	 * 设置 过期时间
+	 * @param key
+	 * @param value
+	 * @param seconds 以秒为单位
+	 */
+	public synchronized static void set(String key, String value, int seconds) {
+		try {
+			value = StringUtils.isEmpty(value) ? "" : value;
+			getJedis().setex(key, seconds, value);
+		} catch (Exception e) {
+			logger.error("Set keyex error : " + e);
+		}
+	}
+
+	/**
+	 * 获取String值
+	 * @param key
+	 * @return value
+	 */
+	public synchronized static String get(String key) {
+		if (getJedis() == null || !getJedis().exists(key)) {
+			return null;
+		}
+		return getJedis().get(key);
+	}
+
+	/**
+	 * 删除值
+	 * @param key
+	 */
+	public synchronized static void remove(String key) {
+		try {
+			getJedis().del(key);
+		} catch (Exception e) {
+			logger.error("Remove keyex error : " + e);
+		}
+	}
 
 }

+ 10 - 1
zheng-common/src/main/resources/applicationContext-jdbc.xml

@@ -15,7 +15,16 @@
           http://www.springframework.org/schema/aop/spring-aop.xsd">
 
 	<!-- 引入jdbc配置文件 -->
-	<context:property-placeholder location="classpath:jdbc.properties" />
+	<!--<context:property-placeholder location="classpath:jdbc.properties" />-->
+	<!-- 配置进行解密  -->
+	<bean id="propertyConfigurer" class="com.zheng.common.plugin.EncryptPropertyPlaceholderConfigurer">
+		<property name="locations">
+			<list>
+				<value>classpath:jdbc.properties</value>
+				<value>classpath:redis.properties</value>
+			</list>
+		</property>
+	</bean>
 	
 	<!-- 主库数据源 -->
 	<bean id="masterDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">

+ 2 - 2
zheng-pay/zheng-pay-dao/src/main/resources/jdbc.properties

@@ -1,9 +1,9 @@
 master.jdbc.driver=com.mysql.jdbc.Driver
 master.jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/zheng?useUnicode\=true&characterEncoding\=utf-8&autoReconnect\=true
 master.jdbc.username=root
-master.jdbc.password=123456
+master.jdbc.password=RwfC38sMIAc=
 
 slave.jdbc.driver=com.mysql.jdbc.Driver
 slave.jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/zheng?useUnicode\=true&characterEncoding\=utf-8&autoReconnect\=true
 slave.jdbc.username=root
-slave.jdbc.password=123456
+slave.jdbc.password=RwfC38sMIAc=

+ 7 - 7
zheng-pay/zheng-pay-dao/src/main/resources/redis.properties

@@ -1,7 +1,7 @@
-ip=127.0.0.1
-port=6379
-password=
-max_active=500
-max_idle=5
-max_wait=10000
-timeout=10000
+master.redis.ip=127.0.0.1
+master.redis.port=6379
+master.redis.password=z4SkijYyEBc=
+master.redis.max_active=500
+master.redis.max_idle=5
+master.redis.max_wait=10000
+master.redis.timeout=10000

+ 2 - 2
zheng-ucenter/zheng-ucenter-dao/src/main/resources/jdbc.properties

@@ -1,9 +1,9 @@
 master.jdbc.driver=com.mysql.jdbc.Driver
 master.jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/zheng?useUnicode\=true&characterEncoding\=utf-8&autoReconnect\=true
 master.jdbc.username=root
-master.jdbc.password=123456
+master.jdbc.password=RwfC38sMIAc=
 
 slave.jdbc.driver=com.mysql.jdbc.Driver
 slave.jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/zheng?useUnicode\=true&characterEncoding\=utf-8&autoReconnect\=true
 slave.jdbc.username=root
-slave.jdbc.password=123456
+slave.jdbc.password=RwfC38sMIAc=

+ 7 - 7
zheng-ucenter/zheng-ucenter-dao/src/main/resources/redis.properties

@@ -1,7 +1,7 @@
-ip=127.0.0.1
-port=6379
-password=
-max_active=500
-max_idle=5
-max_wait=10000
-timeout=10000
+master.redis.ip=127.0.0.1
+master.redis.port=6379
+master.redis.password=z4SkijYyEBc=
+master.redis.max_active=500
+master.redis.max_idle=5
+master.redis.max_wait=10000
+master.redis.timeout=10000

+ 2 - 2
zheng-upms/zheng-upms-dao/src/main/resources/jdbc.properties

@@ -1,9 +1,9 @@
 master.jdbc.driver=com.mysql.jdbc.Driver
 master.jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/zheng?useUnicode\=true&characterEncoding\=utf-8&autoReconnect\=true
 master.jdbc.username=root
-master.jdbc.password=123456
+master.jdbc.password=RwfC38sMIAc=
 
 slave.jdbc.driver=com.mysql.jdbc.Driver
 slave.jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/zheng?useUnicode\=true&characterEncoding\=utf-8&autoReconnect\=true
 slave.jdbc.username=root
-slave.jdbc.password=123456
+slave.jdbc.password=RwfC38sMIAc=

+ 7 - 7
zheng-upms/zheng-upms-dao/src/main/resources/redis.properties

@@ -1,7 +1,7 @@
-ip=127.0.0.1
-port=6379
-password=
-max_active=500
-max_idle=5
-max_wait=10000
-timeout=10000
+master.redis.ip=127.0.0.1
+master.redis.port=6379
+master.redis.password=z4SkijYyEBc=
+master.redis.max_active=500
+master.redis.max_idle=5
+master.redis.max_wait=10000
+master.redis.timeout=10000

+ 2 - 2
zheng-wechat-mp/zheng-wechat-mp-dao/src/main/resources/jdbc.properties

@@ -1,9 +1,9 @@
 master.jdbc.driver=com.mysql.jdbc.Driver
 master.jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/zheng?useUnicode\=true&characterEncoding\=utf-8&autoReconnect\=true
 master.jdbc.username=root
-master.jdbc.password=123456
+master.jdbc.password=RwfC38sMIAc=
 
 slave.jdbc.driver=com.mysql.jdbc.Driver
 slave.jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/zheng?useUnicode\=true&characterEncoding\=utf-8&autoReconnect\=true
 slave.jdbc.username=root
-slave.jdbc.password=123456
+slave.jdbc.password=RwfC38sMIAc=

+ 7 - 7
zheng-wechat-mp/zheng-wechat-mp-dao/src/main/resources/redis.properties

@@ -1,7 +1,7 @@
-ip=127.0.0.1
-port=6379
-password=
-max_active=500
-max_idle=5
-max_wait=10000
-timeout=10000
+master.redis.ip=127.0.0.1
+master.redis.port=6379
+master.redis.password=z4SkijYyEBc=
+master.redis.max_active=500
+master.redis.max_idle=5
+master.redis.max_wait=10000
+master.redis.timeout=10000