From 8c4955c1b842d469727e010733515d8cdb8ac6dd Mon Sep 17 00:00:00 2001 From: = <=> Date: Thu, 26 Aug 2021 00:31:16 +0800 Subject: [PATCH] =?UTF-8?q?'=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cweb/controller/HighTestController.java | 17 +- .../main/java/com/hai/config/RedisConfig.java | 110 +++++++++++++ hai-service/pom.xml | 5 + .../java/com/hai/common/security/DESUtil.java | 148 ++++++++++++++++++ .../config/HuiLianTongUnionCardConfig.java | 46 ++++++ 5 files changed, 322 insertions(+), 4 deletions(-) create mode 100644 hai-schedule/src/main/java/com/hai/config/RedisConfig.java create mode 100644 hai-service/src/main/java/com/hai/common/security/DESUtil.java create mode 100644 hai-service/src/main/java/com/hai/config/HuiLianTongUnionCardConfig.java diff --git a/hai-cweb/src/main/java/com/cweb/controller/HighTestController.java b/hai-cweb/src/main/java/com/cweb/controller/HighTestController.java index 92b99104..9a381a6f 100644 --- a/hai-cweb/src/main/java/com/cweb/controller/HighTestController.java +++ b/hai-cweb/src/main/java/com/cweb/controller/HighTestController.java @@ -7,10 +7,7 @@ import com.hai.common.exception.ErrorHelp; import com.hai.common.exception.SysCode; import com.hai.common.pay.util.sdk.WXPayConstants; import com.hai.common.utils.*; -import com.hai.config.CommonSysConst; -import com.hai.config.HuiLianTongConfig; -import com.hai.config.QianZhuConfig; -import com.hai.config.TuanYouConfig; +import com.hai.config.*; import com.hai.entity.*; import com.hai.model.HighMerchantModel; import com.hai.model.HighMerchantStoreModel; @@ -220,4 +217,16 @@ public class HighTestController { } } + @RequestMapping(value = "/test3", method = RequestMethod.GET) + @ResponseBody + @ApiOperation(value = "测试") + public ResponseData test3() { + try { + return ResponseMsgUtil.success(HuiLianTongUnionCardConfig.getToken()); + } catch (Exception e) { + log.error("HighOrderController --> getOrderById() error!", e); + return ResponseMsgUtil.exception(e); + } + } + } diff --git a/hai-schedule/src/main/java/com/hai/config/RedisConfig.java b/hai-schedule/src/main/java/com/hai/config/RedisConfig.java new file mode 100644 index 00000000..0385fb41 --- /dev/null +++ b/hai-schedule/src/main/java/com/hai/config/RedisConfig.java @@ -0,0 +1,110 @@ +package com.hai.config; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.cache.annotation.CachingConfigurerSupport; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.*; +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; + + +@Configuration +@EnableCaching //开启注解 +public class RedisConfig extends CachingConfigurerSupport { + + /** + * retemplate相关配置 + * @param factory + * @return + */ + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory factory) { + + RedisTemplate template = new RedisTemplate<>(); + // 配置连接工厂 + template.setConnectionFactory(factory); + + //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式) + Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class); + + ObjectMapper om = new ObjectMapper(); + // 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public + om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); + // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常 + om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); + jacksonSeial.setObjectMapper(om); + + // 值采用json序列化 + template.setValueSerializer(jacksonSeial); + //使用StringRedisSerializer来序列化和反序列化redis的key值 + template.setKeySerializer(new StringRedisSerializer()); + + // 设置hash key 和value序列化模式 + template.setHashKeySerializer(new StringRedisSerializer()); + template.setHashValueSerializer(jacksonSeial); + template.afterPropertiesSet(); + + return template; + } + + /** + * 对hash类型的数据操作 + * + * @param redisTemplate + * @return + */ + @Bean + public HashOperations hashOperations(RedisTemplate redisTemplate) { + return redisTemplate.opsForHash(); + } + + /** + * 对redis字符串类型数据操作 + * + * @param redisTemplate + * @return + */ + @Bean + public ValueOperations valueOperations(RedisTemplate redisTemplate) { + return redisTemplate.opsForValue(); + } + + /** + * 对链表类型的数据操作 + * + * @param redisTemplate + * @return + */ + @Bean + public ListOperations listOperations(RedisTemplate redisTemplate) { + return redisTemplate.opsForList(); + } + + /** + * 对无序集合类型的数据操作 + * + * @param redisTemplate + * @return + */ + @Bean + public SetOperations setOperations(RedisTemplate redisTemplate) { + return redisTemplate.opsForSet(); + } + + /** + * 对有序集合类型的数据操作 + * + * @param redisTemplate + * @return + */ + @Bean + public ZSetOperations zSetOperations(RedisTemplate redisTemplate) { + return redisTemplate.opsForZSet(); + } + +} \ No newline at end of file diff --git a/hai-service/pom.xml b/hai-service/pom.xml index 4360200b..73db3b71 100644 --- a/hai-service/pom.xml +++ b/hai-service/pom.xml @@ -142,6 +142,11 @@ commons-collections4 4.2 + + commons-codec + commons-codec + 1.10 + commons-logging commons-logging diff --git a/hai-service/src/main/java/com/hai/common/security/DESUtil.java b/hai-service/src/main/java/com/hai/common/security/DESUtil.java new file mode 100644 index 00000000..729eec1f --- /dev/null +++ b/hai-service/src/main/java/com/hai/common/security/DESUtil.java @@ -0,0 +1,148 @@ +package com.hai.common.security; + +import java.security.Key; +import java.security.SecureRandom; +import java.security.Security; + +import javax.crypto.Cipher; +import javax.crypto.KeyGenerator; +import javax.crypto.SecretKey; +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.DESKeySpec; + +import org.apache.commons.codec.binary.Base64; +import org.bouncycastle.jce.provider.BouncyCastleProvider; + +/** + * DES安全编码组件 + * + * @author wbw + * @version 1.0 + */ +public abstract class DESUtil { + static{ + Security.insertProviderAt(new BouncyCastleProvider(), 1); + } + /** + * 密钥算法
+ * Java 6 只支持56bit密钥
+ * Bouncy Castle 支持64bit密钥 + */ + public static final String KEY_ALGORITHM = "DES"; + + /** + * 加密/解密算法 / 工作模式 / 填充方式 + */ + public static final String CIPHER_ALGORITHM = "DES/ECB/PKCS5PADDING"; + + /** + * 转换密钥 + * + * @param key + * 二进制密钥 + * @return Key 密钥 + * @throws Exception + */ + private static Key toKey(byte[] key) throws Exception { + + // 实例化DES密钥材料 + DESKeySpec dks = new DESKeySpec(key); + + // 实例化秘密密钥工厂 + SecretKeyFactory keyFactory = SecretKeyFactory + .getInstance(KEY_ALGORITHM); + + // 生成秘密密钥 + SecretKey secretKey = keyFactory.generateSecret(dks); + + return secretKey; + } + + /** + * 解密 + * + * @param data + * 待解密数据 + * @param key + * 密钥 + * @return byte[] 解密数据 + * @throws Exception + */ + public static byte[] decrypt(byte[] data, byte[] key) throws Exception { + + // 还原密钥 + Key k = toKey(key); + + // 实例化 + Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); + + // 初始化,设置为解密模式 + cipher.init(Cipher.DECRYPT_MODE, k); + + // 执行操作 + return cipher.doFinal(data); + } + + /** + * 加密 + * + * @param data + * 待加密数据 + * @param key + * 密钥 + * @return byte[] 加密数据 + * @throws Exception + */ + public static byte[] encrypt(byte[] data, byte[] key) throws Exception { + + // 还原密钥 + Key k = toKey(key); + + // 实例化 + Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); + + // 初始化,设置为加密模式 + cipher.init(Cipher.ENCRYPT_MODE, k); + + // 执行操作 + return cipher.doFinal(data); + } + + /** + * 生成密钥
+ * Java 6 只支持56bit密钥
+ * Bouncy Castle 支持64bit密钥
+ * + * @return byte[] 二进制密钥 + * @throws Exception + */ + public static byte[] initKey() throws Exception { + + /* + * 实例化密钥生成器 + * + * 若要使用64bit密钥注意替换 将下述代码中的KeyGenerator.getInstance(CIPHER_ALGORITHM); + * 替换为KeyGenerator.getInstance(CIPHER_ALGORITHM, "BC"); + */ + KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM); + + /* + * 初始化密钥生成器 若要使用64bit密钥注意替换 将下述代码kg.init(56); 替换为kg.init(64); + */ + kg.init(56, new SecureRandom()); + + // 生成秘密密钥 + SecretKey secretKey = kg.generateKey(); + + // 获得密钥的二进制编码形式 + return secretKey.getEncoded(); + } + + public static byte[] initKey(String seed) throws Exception { + KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM); + SecureRandom secureRandom = new SecureRandom(new Base64().decode(seed)); + kg.init(secureRandom); + SecretKey secretKey = kg.generateKey(); + return secretKey.getEncoded(); + } +} \ No newline at end of file diff --git a/hai-service/src/main/java/com/hai/config/HuiLianTongUnionCardConfig.java b/hai-service/src/main/java/com/hai/config/HuiLianTongUnionCardConfig.java new file mode 100644 index 00000000..f60b621f --- /dev/null +++ b/hai-service/src/main/java/com/hai/config/HuiLianTongUnionCardConfig.java @@ -0,0 +1,46 @@ +package com.hai.config; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.hai.common.security.DESUtil; +import com.hai.common.utils.HttpsUtils; +import com.hai.common.utils.MD5Util; +import org.apache.commons.collections4.MapUtils; + +import java.util.Base64; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * 汇联通会员卡业务接口 + */ +public class HuiLianTongUnionCardConfig { + + /** + * 获取token + * @return + * @throws Exception + */ + public static JSONObject getToken() throws Exception { + Map map = new HashMap<>(); + map.put("accessCode", "6FCAE1470CEF465988351BB65ABAA8AE"); + map.put("requestId", new Date().getTime()); + map.put("method", "qgk/queryCardByMobile"); + + String signCode = "F8E91A3C"; + Map dataJson = new HashMap<>(); + dataJson.put("userMobile", "17726395120"); + System.out.println(JSONObject.toJSONString(dataJson)); + + map.put("data", new String(DESUtil.encrypt(JSONObject.toJSONString(dataJson).getBytes(),signCode.getBytes()),"ISO8859-1")); + System.out.println(MapUtils.getString(map,"data")); + + String str = (MapUtils.getString(map,"accessCode") + signCode) + (MapUtils.getString(map,"requestId") + signCode) + (MapUtils.getString(map,"method") + signCode) + (MapUtils.getString(map,"data") + signCode); + map.put("sign", MD5Util.encode(str.getBytes())); + // return HttpsUtils.doPost("http://hltgz.com:4010/api/v2/execute.json", JSON.toJSONString(map)); + return null; + } + + +}