parent
fd2d43e8b4
commit
8c4955c1b8
@ -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<String, Object> redisTemplate(RedisConnectionFactory factory) { |
||||
|
||||
RedisTemplate<String, Object> 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<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) { |
||||
return redisTemplate.opsForHash(); |
||||
} |
||||
|
||||
/** |
||||
* 对redis字符串类型数据操作 |
||||
* |
||||
* @param redisTemplate |
||||
* @return |
||||
*/ |
||||
@Bean |
||||
public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) { |
||||
return redisTemplate.opsForValue(); |
||||
} |
||||
|
||||
/** |
||||
* 对链表类型的数据操作 |
||||
* |
||||
* @param redisTemplate |
||||
* @return |
||||
*/ |
||||
@Bean |
||||
public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) { |
||||
return redisTemplate.opsForList(); |
||||
} |
||||
|
||||
/** |
||||
* 对无序集合类型的数据操作 |
||||
* |
||||
* @param redisTemplate |
||||
* @return |
||||
*/ |
||||
@Bean |
||||
public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) { |
||||
return redisTemplate.opsForSet(); |
||||
} |
||||
|
||||
/** |
||||
* 对有序集合类型的数据操作 |
||||
* |
||||
* @param redisTemplate |
||||
* @return |
||||
*/ |
||||
@Bean |
||||
public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) { |
||||
return redisTemplate.opsForZSet(); |
||||
} |
||||
|
||||
} |
@ -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); |
||||
} |
||||
/** |
||||
* 密钥算法 <br> |
||||
* Java 6 只支持56bit密钥 <br> |
||||
* 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); |
||||
} |
||||
|
||||
/** |
||||
* 生成密钥 <br> |
||||
* Java 6 只支持56bit密钥 <br> |
||||
* Bouncy Castle 支持64bit密钥 <br> |
||||
* |
||||
* @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(); |
||||
} |
||||
} |
@ -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<String,Object> map = new HashMap<>(); |
||||
map.put("accessCode", "6FCAE1470CEF465988351BB65ABAA8AE"); |
||||
map.put("requestId", new Date().getTime()); |
||||
map.put("method", "qgk/queryCardByMobile"); |
||||
|
||||
String signCode = "F8E91A3C"; |
||||
Map<String,Object> 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; |
||||
} |
||||
|
||||
|
||||
} |
Loading…
Reference in new issue