修改mybatisGenerator配置文件,为model增加可序列化接口
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
package com.micro.fast.common.util;
|
||||
|
||||
import sun.misc.BASE64Decoder;
|
||||
import sun.misc.BASE64Encoder;
|
||||
|
||||
import javax.crypto.*;
|
||||
import javax.crypto.KeyGenerator;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
/**
|
||||
* AES加解密工具类
|
||||
* create by lishouyu 2017/08/21
|
||||
* email 18332763730@163.com
|
||||
*
|
||||
* last edit lishouyu 2017/08/21
|
||||
* email 18332763730@163.com
|
||||
*
|
||||
*/
|
||||
public class AESUtil {
|
||||
//编码规则
|
||||
private static final String encodeRules = "li";
|
||||
|
||||
/**
|
||||
* 加密
|
||||
* 1.构造密钥生成器
|
||||
* 2.根据ecnodeRules规则初始化密钥生成器
|
||||
* 3.产生密钥
|
||||
* 4.创建和初始化密码器
|
||||
* 5.内容加密
|
||||
* 6.返回字符串
|
||||
* @param content
|
||||
* @return
|
||||
*/
|
||||
public static String AESEncode(String content){
|
||||
try{
|
||||
//1.构造密钥生成器,指定为AES算法,不区分大小写
|
||||
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
|
||||
//2.根据encodeRules规则初始化密钥生成器,生成一个128位的随机源,
|
||||
//根据传入的字节数组
|
||||
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
|
||||
secureRandom.setSeed(encodeRules.getBytes());
|
||||
keyGenerator.init(128,secureRandom);
|
||||
//3.产生原始对称密钥
|
||||
SecretKey original_secretKey = keyGenerator.generateKey();
|
||||
//4.获取原始对称密钥的字节数组
|
||||
byte[] encoded = original_secretKey.getEncoded();
|
||||
//5.根据字节数组生成AES密钥
|
||||
SecretKey key = new SecretKeySpec(encoded, "AES");
|
||||
//6.根据指定算法AES自成密码器
|
||||
Cipher cipher = Cipher.getInstance("AES");
|
||||
//7.初始化密码器第一个参数为加密或者解密操作,第二个参数为使用的key
|
||||
cipher.init(Cipher.ENCRYPT_MODE,key);
|
||||
//8.获取加密内容的字节数组,要设置编码为utf8不然内容中如果有中文的英文的混合中文就会解密乱码
|
||||
byte[] byte_encode = content.getBytes("utf8");
|
||||
//9.根据密码器的初始化方式--加密:将数据加密
|
||||
byte[] bytes_AES = cipher.doFinal(byte_encode);
|
||||
//10.将加密后的数据转换为字符串
|
||||
//这里的Base64Encoder中会找不到包
|
||||
//解决方法
|
||||
//在项目的Build path 中县一簇jre System Library ,再添加哭JRE System Library,重新变异后就一切正常了
|
||||
String AES_encode = new String(new BASE64Encoder().encode(bytes_AES));
|
||||
//11.将字符串返回
|
||||
return AES_encode;
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchPaddingException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvalidKeyException e) {
|
||||
e.printStackTrace();
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
} catch (BadPaddingException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalBlockSizeException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
//如果有错误就返回null
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 1.构造密钥生成器
|
||||
* 2.根据encodeRules初始化密钥生成器
|
||||
* 3.产生密钥
|
||||
* 4.创建和初始化密码器
|
||||
* 5.根据密钥对内容解密
|
||||
* 6.返回字符串
|
||||
* @param content
|
||||
* @return
|
||||
*/
|
||||
public static String AESDecode(String content){
|
||||
try {
|
||||
//1.构造密钥生成器,指定为AES算法,不区分大小写
|
||||
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
|
||||
//2.根据encodeRules规则初始化密钥生成器
|
||||
//生成一个128位的随机数,根据传入的字节数组
|
||||
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
|
||||
secureRandom.setSeed(encodeRules.getBytes());
|
||||
keyGenerator.init(128,secureRandom);
|
||||
//3.产生原始对称密钥
|
||||
SecretKey original_secretKey = keyGenerator.generateKey();
|
||||
//4.获取原始对称密钥的字节数组
|
||||
byte[] encoded = original_secretKey.getEncoded();
|
||||
//5.根据字节数组生成AES密钥
|
||||
SecretKey aes = new SecretKeySpec(encoded, "AES");
|
||||
//6.根据指定算法AES自称密码器
|
||||
Cipher cipher = Cipher.getInstance("AES");
|
||||
//7.初始化密码器,第一个参数为加密或者是解密操作,第二个参数为使用的key
|
||||
cipher.init(Cipher.DECRYPT_MODE,aes);
|
||||
//8.将加密并编码后的内容解密成字节数组
|
||||
byte[] byte_content = new BASE64Decoder().decodeBuffer(content);
|
||||
//9.解密
|
||||
byte[] bytes = cipher.doFinal(byte_content);
|
||||
String AES_decode = new String(bytes,"utf-8");
|
||||
return AES_decode;
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchPaddingException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvalidKeyException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (BadPaddingException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalBlockSizeException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String[] keys = new String[]{"studyj2e"};
|
||||
System.out.println("key | AESEncode | AESDecode");
|
||||
for (String key : keys) {
|
||||
System.out.print(key+" | ");
|
||||
String aesEncode = AESEncode(key);
|
||||
System.out.print(aesEncode+" | ");
|
||||
String aesDecode = AESDecode(aesEncode);
|
||||
System.out.println(aesDecode);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.micro.fast.common.util;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.MessageDigest;
|
||||
|
||||
/**
|
||||
* MD5加密类
|
||||
* create by lishouyu 2017/08/21
|
||||
* email 18332763730@163.com
|
||||
*
|
||||
* last edit lishouyu 2017/08/21
|
||||
* email 18332763730@163.com
|
||||
*
|
||||
*/
|
||||
public class MD5Util {
|
||||
/**
|
||||
* 对字节流进行单向MD5加密
|
||||
* @param bytes 字节数组
|
||||
* @return String类型返回值
|
||||
*/
|
||||
private static String MD5EncodeUtf8(byte[] bytes) {
|
||||
//用于加密的字符
|
||||
char md5String[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'A', 'B', 'C', 'D', 'E', 'F'};
|
||||
try {
|
||||
//信息摘要是安全的单向哈希函数,它接受任意大小的数据,并输出固定长度的哈希值
|
||||
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
|
||||
//MessageDigest 对象通过使用update方法处理数据,使用指定的byte数组更新摘要
|
||||
messageDigest.update(bytes);
|
||||
//摘要更新之后。通过调用digest()计算哈希值
|
||||
byte[] digest = messageDigest.digest();
|
||||
// 把密文转换成十六进制的字符串形式
|
||||
int j = digest.length;
|
||||
char str[] = new char[j * 2];
|
||||
int k = 0;
|
||||
for (byte byte0 : digest) { // i = 0
|
||||
str[k++] = md5String[byte0 >>> 4 & 0xf]; // 5
|
||||
str[k++] = md5String[byte0 & 0xf]; // F
|
||||
}
|
||||
return new String(str);
|
||||
} catch (Exception e) {
|
||||
//发生异常的时候返回为空
|
||||
return null;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 对字符串进行Md5加密
|
||||
* @param string 传入的字符串参数
|
||||
* @return String型返回结果
|
||||
*/
|
||||
public static String MD5EncodeStrUtf8(String string){
|
||||
byte[] bytes = new byte[0];
|
||||
try {
|
||||
bytes = string.getBytes("utf8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return MD5EncodeUtf8(bytes);
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,8 @@
|
||||
<property name="endingDelimiter" value="`"/>
|
||||
<!-- 为生成的Java模型创建一个toString方法 -->
|
||||
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>
|
||||
<!-- 对生成的pojo集成serializable接口-->
|
||||
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" ></plugin>
|
||||
<!-- optional,旨在创建class时,对注释进行控制 -->
|
||||
<commentGenerator>
|
||||
<property name="suppressDate" value="true"/>
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.micro.fast.common.util;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class MD5UtilTest {
|
||||
@Test
|
||||
public void MD5EncodeStrUtf8() throws Exception {
|
||||
MD5Util.MD5EncodeStrUtf8("李守余");
|
||||
Assert.assertEquals("7F8D114165AB73DBDAFF9EF4A85A5E0A",MD5Util.MD5EncodeStrUtf8("李守余"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user