Browse Source

新增鹏元征信 用户名、密码加密

liuam 7 years ago
parent
commit
cb55787a70

+ 3 - 2
src/main/java/com/uas/credit/Application.java

@@ -1,5 +1,6 @@
 package com.uas.credit;
 
+import com.uas.credit.config.EncryptablePropertyPlaceholderConfigurer;
 import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -23,7 +24,7 @@ public class Application {
     @Bean
     @Profile("prod")
     public PropertyPlaceholderConfigurer prod(ApplicationContext applicationContext) {
-        PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
+        PropertyPlaceholderConfigurer configurer = new EncryptablePropertyPlaceholderConfigurer();
         Resource resource = applicationContext.getResource("classpath:prod.properties");
         configurer.setLocation(resource);
         return configurer;
@@ -32,7 +33,7 @@ public class Application {
     @Bean
     @Profile("dev")
     public PropertyPlaceholderConfigurer dev(ApplicationContext applicationContext) {
-        PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
+        PropertyPlaceholderConfigurer configurer = new EncryptablePropertyPlaceholderConfigurer();
         Resource resource = applicationContext.getResource("classpath:dev.properties");
         configurer.setLocation(resource);
         return configurer;

+ 40 - 0
src/main/java/com/uas/credit/config/EncryptablePropertyPlaceholderConfigurer.java

@@ -0,0 +1,40 @@
+package com.uas.credit.config;
+
+import com.uas.credit.support.Des;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.BeanInitializationException;
+import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
+import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
+
+import java.util.Properties;
+
+/**
+ * 将加密的 userId password 进行解密
+ * @author liuam
+ * @since 2018/6/25 0025 下午 17:26
+ */
+public class EncryptablePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
+    private static final String key = "10101010";
+
+    protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props)
+            throws BeansException {
+        try {
+            Des des = new Des();
+            String userId = props.getProperty("pyconfig.userId");
+            if (userId != null) {
+                props.setProperty("pyconfig.userId", des.decrypt(userId, key));
+            }
+
+            String password = props.getProperty("pyconfig.password");
+            if (password != null) {
+                props.setProperty("pyconfig.password", des.decrypt(password, key));
+            }
+            // 自定义完成后调用父类方法
+            super.processProperties(beanFactory, props);
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new BeanInitializationException(e.getMessage());
+        }
+    }
+
+}

+ 78 - 0
src/main/java/com/uas/credit/support/Des.java

@@ -0,0 +1,78 @@
+package com.uas.credit.support;
+
+import javax.crypto.Cipher;
+import javax.crypto.SecretKey;
+import javax.crypto.SecretKeyFactory;
+import javax.crypto.spec.DESKeySpec;
+import javax.crypto.spec.IvParameterSpec;
+
+/**
+ * 鹏远征信 用户名、密码加密工具类
+ * @author liuam
+ * @since 2018/6/25 0025 下午 17:12
+ */
+public class Des {
+
+    private byte[] desKey;
+
+    public Des() {
+    }
+
+    public String decrypt(String message, String key) throws Exception {
+        byte[] bytesrc = this.convertHexString(message);
+        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
+        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
+        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
+        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
+        IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
+        cipher.init(2, secretKey, iv);
+        byte[] retByte = cipher.doFinal(bytesrc);
+        return new String(retByte);
+    }
+
+    public byte[] encrypt(String message, String key) throws Exception {
+        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
+        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
+        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
+        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
+        IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
+        cipher.init(1, secretKey, iv);
+        return cipher.doFinal(message.getBytes("UTF-8"));
+    }
+
+    public byte[] convertHexString(String ss) {
+        byte[] digest = new byte[ss.length() / 2];
+
+        for(int i = 0; i < digest.length; ++i) {
+            String byteString = ss.substring(2 * i, 2 * i + 2);
+            int byteValue = Integer.parseInt(byteString, 16);
+            digest[i] = (byte)byteValue;
+        }
+
+        return digest;
+    }
+
+    public String toHexString(byte[] b) {
+        StringBuffer hexString = new StringBuffer();
+
+        for(int i = 0; i < b.length; ++i) {
+            String plainText = Integer.toHexString(255 & b[i]);
+            if(plainText.length() < 2) {
+                plainText = "0" + plainText;
+            }
+
+            hexString.append(plainText);
+        }
+
+        return hexString.toString();
+    }
+
+    public byte[] getDesKey() {
+        return this.desKey;
+    }
+
+    public void setDesKey(byte[] desKey) {
+        this.desKey = desKey;
+    }
+
+}

+ 1 - 0
src/main/java/com/uas/credit/util/QueryConditionUtil.java

@@ -7,6 +7,7 @@ import java.util.ArrayList;
 import java.util.List;
 
 /**
+ * 企业查询,个人查询公用方法
  * @author liuam
  * @since 2018/6/25 0025 下午 16:19
  */

+ 2 - 2
src/main/resources/dev.properties

@@ -8,10 +8,10 @@ pyconfig.pathZip=/rest/query/report/zip
 pyconfig.pathUnZip=/rest/query/report/unzip
 
 #认证信息,用户id
-pyconfig.userId=ytznwsqueryn2
+pyconfig.userId=a157071291c5e6d078ec5949595aa369
 
 #认证信息,密码
-pyconfig.password={MD5}Ctp7/RnkBEzsBh+aNa3cNA==
+pyconfig.password=f260c52f4d23e26f1617abe3441902f8e91b7bc954d9d252c33980485e6586ff
 
 #是否测试模式
 pyconfig.test=true

+ 2 - 2
src/main/resources/prod.properties

@@ -8,10 +8,10 @@ pyconfig.pathZip=/rest/query/report/zip
 pyconfig.pathUnZip=/rest/query/report/unzip
 
 #认证信息,用户id
-pyconfig.userId=ytznwsqueryn2
+pyconfig.userId=a157071291c5e6d078ec5949595aa369
 
 #认证信息,密码
-pyconfig.password={MD5}Ctp7/RnkBEzsBh+aNa3cNA==
+pyconfig.password=f260c52f4d23e26f1617abe3441902f8e91b7bc954d9d252c33980485e6586ff
 
 #是否测试模式
 pyconfig.test=false

+ 15 - 0
src/main/test/NormalTest.java

@@ -0,0 +1,15 @@
+import org.junit.Test;
+
+/**
+ * @author liuam
+ * @since 2018/6/25 0025 下午 17:10
+ */
+
+public class NormalTest {
+
+    @Test
+    public void test() throws Exception {
+
+    }
+
+}

+ 19 - 0
src/main/test/SpringBootTestApplication.java

@@ -0,0 +1,19 @@
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+/**
+ * @author liuam
+ * @since 2018/6/25 0025 下午 17:08
+ */
+@RunWith(SpringRunner.class)
+@SpringBootTest(classes = SpringBootTestApplication.class)
+public class SpringBootTestApplication {
+
+    @Test
+    public void contextLoad() {
+
+    }
+
+}