SpringBoot⾃定义注解API数据加密和签名校验api数据数据签名(MD5,SHA1)
签名枚举类SginEnum.java
package com.jx.app.api.ums;
/**
* @ClassName: SginEnum
* @Description: TODO(这是⼀个签名枚举类)
* @author gangyu
* @date 2018年11⽉20⽇下午4:30:44
*/
public enum SginEnum {
//0不需要签名,1使⽤MD5数据加密 2 使⽤SHA数据加密
ANY(0), MD5(1), SHA1(2);
private final int value;
private SginEnum(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
签名注解类SginAnot.java
/**
* @Title: SginAnot.java
* @Package com.jxkj.app.api.framework
* @Description: TODO(⽤⼀句话描述该⽂件做什么)
* @author gangyu
* @date 2018年11⽉20⽇下午4:07:58
* @version V1.0
*/
package com.jx.app.api.framework.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import com.jx.app.api.ums.SginEnum;
/**
* @ClassName: SginAnot
* @Description: TODO(签名验证注解)
* @author gangyu
* @date 2018年11⽉20⽇下午4:07:58
*
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface SginAnot {
SginEnum type() default SginEnum.ANY;//默认不需要签名
}
加密⼯具类MD5.java
package pt;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import org.apachemons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jxmon.utils.BeanUtil;
import com.jxmon.utils.TestEntity;
/**
* @ClassName: MD5
* @Description: TODO(MD5加密⼯具)
* @author gangyu
* @date 2018年11⽉20⽇下午2:12:14
*
*/
public class MD5 {
private static final Logger log = Logger(MD5.class);
public static String PRIVATE_KEY = "这是你的密钥";
private static final String hexDigIts[] = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"}; public static String encrypt(String plainText) {
try {
return encrypt(plainText,true);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
<("MD5加密异常:",e);
return null;
}
}
/**
* @Title: encrypt
* @Description: TODO(16位或32位密码)
* @param @param
* plainText
* @param @param
* flag true为32位,false为16位
* @throws UnsupportedEncodingException
*/
public static String encrypt(String plainText, boolean flag) throws UnsupportedEncodingException { try {
if (StringUtils.isEmpty(plainText)) {
return null;
}
MessageDigest md = Instance("MD5");
String encrStr = byteArrayToHexString(md.Bytes("UTF-8")));
if (flag)
return encrStr;
else
return encrStr.substring(8, 24);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
@SuppressWarnings("unchecked")
public static String encrypt(Object obj){
if(obj==null){
return null;
}
Map<String, Object> map = new HashMap<String,Object>();
if(obj instanceof Map){
map=(Map<String, Object>) obj;
}else{
map = ansBean2Map(obj);
}
return encrypt(map,true);
}
/**
* @Title: encrypt
* @Description: TODO(16位或32位密码)
* @param @param
* plainText
* @param @param
* flag true为32位,false为16位
* @throws UnsupportedEncodingException
*/
public static String encrypt(Map<String, Object> map, boolean flag) {
String param = null;
String result = BeanUtil.mapOrderStr(map);
if (StringUtils.isEmpty(result)) {
return null;
}
param = encrypt(encrypt(result)+PRIVATE_KEY);
if (flag) {
return param;
} else {
param = param.substring(8, 24);
}
return param;
}
public static Map<String, Object> resultMap = new HashMap<String, Object>();
@SuppressWarnings("unchecked")
public static Map<String, Object> mapFn(Map<String, Object> map) {
for (String key : map.keySet()) {
if ((key) != null && (key) != "" && (!key.equals("BTYPE") && !key.equals("SIGN"))) { if (key.equals("INPUT")) {
if ((key) != null) {
mapFn((Map<String, Object>) (key));
}
} else {
resultMap.put(key, (key));
}
}
}
return resultMap;
}
@SuppressWarnings("unchecked")
public static boolean check(Object obj){
Map<String,Object> map=new HashMap<String,Object>();
if(obj==null){
return false;
}
if(obj instanceof Map){
map=(Map<String, Object>) obj;
}else{
map = ansBean2Map(obj);
}
String sign=(("sign");
if(sign==null){
return false;
}
String str=encrypt(obj);
return sign.equals(str)?true:false;
}
public static String byteArrayToHexString(byte b[]){
StringBuffer resultSb = new StringBuffer();
for(int i = 0; i < b.length; i++){
resultSb.append(byteToHexString(b[i]));
}
String();
}
public static String byteToHexString(byte b){
int n = b;
if(n < 0){
n += 256;
}
int d1 = n / 16;
int d2 = n % 16;
return hexDigIts[d1] + hexDigIts[d2];
}
public static void main(String[] args) throws UnsupportedEncodingException { TestEntity test = new TestEntity();
test.setId("1");
test.setAge("20");
test.setClaes("你好");
test.setName("gyu");
test.setCreateTime("2018-11-20");
test.setSign("5189bd815c3850395f30779d0e59229e");
System.out.println("MD5验签成功:"+check(test));
}
}
SHA加密⼯具类SHA1.java
package pt;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import org.apachemons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jxmon.utils.BeanUtil;
/**
springframework和springboot* @ClassName: Sha1Util
* @Description: TODO(SHA加密)
* @author gangyu
* @date 2018年11⽉20⽇下午2:08:36
*
*/
public class Sha1 {
private static final Logger log = Logger(Sha1.class);
public static String encrypt(String str){
if (null == str || 0 == str.length()){
return null;
}
char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f'};
try {
MessageDigest mdTemp = Instance("SHA1");
mdTemp.update(new Bytes("iso8859-1"), "utf-8").getBytes()); byte[] md = mdTemp.digest();
int j = md.length;
char[] buf = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
buf[k++] = hexDigits[byte0 & 0xf];
}
return new String(buf);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
<("SHA1加密异常:",e);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
<("SHA1加密异常:",e);
}
return str;
}
@SuppressWarnings("unchecked")
public static String encrypt(Object obj) {
if(obj==null){
return null;
}
Map<String, Object> map = new HashMap<String,Object>();
if(obj instanceof Map){
map=(Map<String, Object>) obj;
map = ansBean2Map(obj);
}
String result = BeanUtil.mapOrderStr(map);
if (StringUtils.isEmpty(result)) {
return null;
}
return encrypt(result);
}
@SuppressWarnings("unchecked")
public static boolean check(Object obj){
Map<String,Object> map=new HashMap<String,Object>(); if(obj==null){
return false;
}
if(obj instanceof Map){
map=(Map<String, Object>) obj;
}else{
map = ansBean2Map(obj);
}
String sign=(("sign");
if(sign==null){
return false;
}
String str=encrypt(obj);
return sign.equals(str)?true:false;
}
}
返回数据对称加密DES.java
package pt;
import java.security.Key;
import java.util.Map;
pto.Cipher;
pto.SecretKeyFactory;
pto.spec.DESedeKeySpec;
pto.spec.IvParameterSpec;
le.gson.Gson;
import com.jxmon.utils.Base64;
/**
* @ClassName: Des
* @Description: TODO(对称加密⼯具)
* @author gangyu
* @date 2018年11⽉20⽇下午1:18:49
*/
public class DES {
// 密钥
private final static String secretKey = "123456789987654321"; // 向量
private final static String iv = "ggboy123";
// 加解密统⼀使⽤的编码⽅式
private final static String encoding = "utf-8";
/**
* @Title: encode
* @Description: TODO(加密)
* @param String
* @author gangyu2
* @date 2018年11⽉20⽇下午1:19:19
*/
public static String encode(String plainText){
Key deskey = null;
DESedeKeySpec spec;
try {
spec = new Bytes());
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论