• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

SpringBoot自定义注解API数据加密和签名校验

springboot 搞代码 4年前 (2022-01-09) 13次浏览 已收录 0个评论

api数据数据签名(MD5,SHA1)

签名枚举类SginEnum.java

package com.jx.app.api.framework.annotation.enums;

/**  
* @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.framework.annotation.enums.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 com.jx.common.entrypt;

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.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jx.common.utils.BeanUtil;
import com.jx.common.utils.TestEntity;

/**
 * @ClassName: MD5
 * @Description: TODO(MD5加密工具)
 * @author gangyu
 * @date 2018年11月20日 下午2:12:14
 *
 */
public class MD5 {


 private static final Logger log = LoggerFactory.getLogger(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();
   log.error("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 = MessageDigest.getInstance("MD5");
            String encrStr = byteArrayToHexString(md.digest(plainText.getBytes("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 = BeanUtil.transBean2Map(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;
  map.remove("sign");
  map.remove("encrypt");
  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 (map.get(key) != null && map.get(key) != "" && (!key.equals("BTYPE") && !key.equals("SIGN"))) {
    if (key.equals("INPUT")) {
     if (map.get(key) != null) {
      mapFn((Map<String, Object>) map.get(key));
     }
    } else {
     resultMap.put(key, map.get(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{
   ma<em>本文来源[email protected]搞@^&代*@码2网</em>p = BeanUtil.transBean2Map(obj);
  }
  String sign=(String)map.get("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]));
        }
        return resultSb.toString();
    }

    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));
 }

}

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:SpringBoot自定义注解API数据加密和签名校验
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址