Explorar el Código

获取用户token 添加签名验证

xushengqiang hace 6 años
padre
commit
3a39fce394

+ 19 - 1
src/main/java/cn/efunbox/audio/controller/DeviceController.java

@@ -9,9 +9,11 @@ import cn.efunbox.audio.service.ChannelService;
 import cn.efunbox.audio.service.DeviceService;
 import cn.efunbox.audio.utils.ApiCode;
 import cn.efunbox.audio.utils.HttpUtil;
+import cn.efunbox.audio.utils.SecurityUtils;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.data.domain.Page;
@@ -62,6 +64,8 @@ public class DeviceController {
     private String terminal;
     @Value("${iaas.user.audio.entryType}")
     private String entryType;
+    @Value("${interface.sign.key}")
+    private String signKey;
 
     @RequestMapping(value = "/feign")
     public void Feigh(HttpServletRequest request, HttpServletResponse response,
@@ -186,10 +190,24 @@ public class DeviceController {
     public void token(HttpServletRequest request, HttpServletResponse response){
         String idChannel = request.getParameter("idChannel");
         String idDevice = request.getParameter("idDevice");
-        if(idChannel==null || idDevice==null){
+        String sign = request.getParameter("sign");
+
+        if(StringUtils.isBlank(idChannel) || StringUtils.isBlank(idDevice) || StringUtils.isBlank(sign)){
             HttpUtil.responseApiCode(request, response, ApiCode.PARAMETER_ERROR);
             return;
         }
+        Map<String,String> param = new HashMap<>();
+        param.put("idChannel",idChannel);
+        param.put("idDevice",idDevice);
+        String test = SecurityUtils.createMD5Sign(param, signKey);
+
+        if (!sign.equalsIgnoreCase(test)) {
+            HttpUtil.responseApiCode(request, response, ApiCode.SIGN_FAIL);
+            return;
+        }
+
+
+
 
         List<Channel> channelList = channelService.SearchById(Long.valueOf(idChannel));
         if(channelList==null || channelList.size()<1){

+ 3 - 1
src/main/java/cn/efunbox/audio/utils/ApiCode.java

@@ -34,7 +34,9 @@ public class ApiCode extends AbstractApiCode {
     public static final int _C_OPERATION_FAIL = 550;
     public static final ApiCode OPERATION_FAIL = new ApiCode("操作失败", 550);
     public static final int _C_NO_REGISTER_FAIL = 560;
-    public static final ApiCode NO_REGISTER_FAIL  = new ApiCode("设备未注册", 560);
+    public static final ApiCode NO_REGISTER_FAIL  = new ApiCode("设备未注册", _C_NO_REGISTER_FAIL);
+    public static final int _C_SIGN_FAIL = 561;
+    public static final ApiCode SIGN_FAIL  = new ApiCode("签名错误", _C_SIGN_FAIL);
 
     public final static int     _C_FILE_TO_BIG = 405;
     public final static ApiCode FILE_TO_BIG    = new ApiCode("文件过大", _C_FILE_TO_BIG);

+ 121 - 0
src/main/java/cn/efunbox/audio/utils/SecurityUtils.java

@@ -0,0 +1,121 @@
+package cn.efunbox.audio.utils;
+
+import org.apache.commons.lang.StringUtils;
+
+import java.io.UnsupportedEncodingException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.*;
+
+/**
+ * SecurityUtils
+ * Created by xusq on 2018/9/11.
+ */
+public class SecurityUtils {
+
+    private SecurityUtils(){}
+
+    public static final String MD5 = "MD5";
+
+    public static final String SIGN = "sign";
+
+    public static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
+
+    public static String createMD5Sign(Map<String, String> paramMap, String signKey) {
+
+        List<String> sortedKeys = new ArrayList<String>();
+        for (Map.Entry<String, String> entry : paramMap.entrySet()) {
+            if (SIGN.equals(entry.getKey())) {
+                continue;
+            }
+
+            sortedKeys.add(entry.getKey());
+        }
+
+        if (sortedKeys.size() == 0) {
+            // 没有参数
+            return "";
+        }
+
+        Collections.sort(sortedKeys);
+
+        StringBuffer buff = new StringBuffer("");
+        for (String key : sortedKeys) {
+            String val = paramMap.get(key);
+            if (StringUtils.isBlank(val)) {
+                continue;
+            }
+
+            buff.append(key).append("=").append(val).append("&");
+        }
+
+        buff.append("key=").append(signKey);
+
+        try {
+            return SecurityUtils.MD5(buff.toString(), null);
+        } catch (Exception e) {
+            throw new RuntimeException("签名错误");
+        }
+    }
+
+
+    /**
+     * MD5摘要算法
+     *
+     * @param content
+     *            摘要内容
+     * @param charset
+     *            字符集,默认为UTF-8
+     * @return
+     * @throws Exception
+     */
+    public static String MD5(String content, String charset){
+        if (charset == null) {
+            charset = "UTF-8";
+        }
+
+        MessageDigest digest = null;
+        try {
+            digest = MessageDigest.getInstance(MD5);
+        } catch (NoSuchAlgorithmException e) {
+            e.printStackTrace();
+        }
+        try {
+            digest.update(content.getBytes(charset)); // 使用指定的字节更新摘要
+        } catch (UnsupportedEncodingException e) {
+            e.printStackTrace();
+        }
+        byte[] md = digest.digest(); // 获得密文
+
+        return byteToHexString(md);
+    }
+
+    // 把密文转换成十六进制的字符串形式
+    public static String byteToHexString(byte[] bytes) {
+        StringBuilder sb = new StringBuilder();
+        for (byte b : bytes) {
+            sb.append(byteToHexString(b));
+        }
+
+        return sb.toString();
+    }
+
+    public static String byteToHexString(byte ib) {
+        char[] ob = new char[2];
+        ob[0] = hexDigits[(ib >>> 4) & 0X0f];
+        ob[1] = hexDigits[ib & 0X0F];
+        return new String(ob);
+    }
+
+    public static void main(String[] args) {
+
+        Map<String,String> param = new HashMap<>();
+
+        param.put("idChannel","1001");
+        param.put("idDevice","10011");
+        String test = SecurityUtils.createMD5Sign(param, "IhOTiTyMLDNNLFuP");
+        System.out.println(test);
+
+
+    }
+}

+ 3 - 1
src/main/resources/application-dev.properties

@@ -64,4 +64,6 @@ ali.oss.endpoint=oss-cn-beijing.aliyuncs.com
 ali.oss.accessKeyId=LTAIUFvd17IXLBQ4
 ali.oss.accessKeySecret=YEm1VebbntRIGmV8s8N33LQfOoC2sA
 aliyun.oss.file.prefix=audio/album/
-efunbox.oss.img.url=http://efunimgs.ai160.com
+efunbox.oss.img.url=http://efunimgs.ai160.com
+
+interface.sign.key=IhOTiTyMLDNNLFuP

+ 3 - 1
src/main/resources/application-prd.properties

@@ -64,4 +64,6 @@ ali.oss.endpoint=oss-cn-beijing.aliyuncs.com
 ali.oss.accessKeyId=LTAIUFvd17IXLBQ4
 ali.oss.accessKeySecret=YEm1VebbntRIGmV8s8N33LQfOoC2sA
 aliyun.oss.file.prefix=audio/album/
-efunbox.oss.img.url=http://efunimgs.ai160.com
+efunbox.oss.img.url=http://efunimgs.ai160.com
+
+interface.sign.key=IhOTiTyMLDNNLFuP