Forráskód Böngészése

接口验签加密算法修改

xushengqiang 4 éve
szülő
commit
402d1e417b

+ 8 - 7
src/main/java/cn/efunbox/audio/controller/ChannelController.java

@@ -19,10 +19,7 @@ import org.springframework.web.bind.annotation.RestController;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.sql.Timestamp;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 /**
  * Created by yao on 17-9-26.
@@ -52,9 +49,13 @@ public class ChannelController {
             size = "0";
         Map<String,Object> map = new HashMap<>();
         List<Channel> list = null;
-        if(idChannel!=null && idChannel.length()>0)
-            list = channelService.SearchById(Long.valueOf(idChannel));
-        else if(name!=null && name.length()>0)
+        if(idChannel!=null && idChannel.length()>0){
+            Channel channel = channelService.SearchById(Long.valueOf(idChannel));
+            if (Objects.nonNull(channel)) {
+                list = new ArrayList<>();
+                list.add(channel);
+            }
+        } else if(name!=null && name.length()>0)
             list = channelService.SearchByName(name);
         else if(idFather!=null && idFather.length()>0)
             list = channelService.SearchByIdFather(Long.valueOf(idFather));

+ 55 - 4
src/main/java/cn/efunbox/audio/controller/DeviceController.java

@@ -24,6 +24,7 @@ import org.springframework.web.bind.annotation.RestController;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import java.nio.charset.StandardCharsets;
 import java.sql.Timestamp;
 import java.util.*;
 
@@ -143,8 +144,8 @@ public class DeviceController {
             HttpUtil.responseApiCode(request, response, ApiCode.RECORD_EXIST);
             return;
         }
-        List<Channel> channelList = channelService.SearchById(Long.valueOf(idChannel));
-        if(channelList==null || channelList.size()<1){
+        Channel channel = channelService.SearchById(Long.valueOf(idChannel));
+        if(Objects.isNull(channel)){
             HttpUtil.responseApiCode(request, response, ApiCode.PARAMETER_ERROR);
             return;
         }
@@ -209,8 +210,58 @@ public class DeviceController {
 
 
 
-        List<Channel> channelList = channelService.SearchById(Long.valueOf(idChannel));
-        if(channelList==null || channelList.size()<1){
+        Channel channel = channelService.SearchById(Long.valueOf(idChannel));
+        if(Objects.isNull(channel)){
+            HttpUtil.responseApiCode(request, response, ApiCode.CHANNEL_NOT_FOUND_ERROR);
+            return;
+        }
+
+        Device device = deviceService.findToken(Long.valueOf(idChannel), idDevice);
+
+        if(Objects.isNull(device)){
+            HttpUtil.responseApiCode(request, response, ApiCode.NO_REGISTER_FAIL);
+            return;
+        }
+
+        device.setIaas_eid("");
+        device.setIaas_name("");
+        device.setIaas_uid("");
+        device.setIaas_token("");
+        device.setIaas_pwd("");
+        device.setTokenOld("");
+        HttpUtil.responseOkData(request, response, device);
+
+        return;
+    }
+
+
+    @RequestMapping(value = "/v2/token",method = RequestMethod.GET)
+    public void tokenV2(HttpServletRequest request, HttpServletResponse response){
+        String idChannel = request.getParameter("idChannel");
+        String idDevice = request.getParameter("idDevice");
+        String ts = request.getParameter("ts");
+        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);
+        param.put("ts",ts);
+        String signStr = Encrypt.createHMACSHA256(param,signKey);
+
+        String decode = new String(Base64.getDecoder().decode(sign), StandardCharsets.UTF_8);
+
+
+        if (!signStr.equalsIgnoreCase(decode)) {
+            HttpUtil.responseApiCode(request, response, ApiCode.SIGN_FAIL);
+            return;
+        }
+
+        Channel channel = channelService.SearchById(Long.valueOf(idChannel));
+        if(Objects.isNull(channel)){
             HttpUtil.responseApiCode(request, response, ApiCode.CHANNEL_NOT_FOUND_ERROR);
             return;
         }

+ 4 - 7
src/main/java/cn/efunbox/audio/controller/RightsController.java

@@ -20,10 +20,7 @@ import org.springframework.web.bind.annotation.RestController;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.sql.Timestamp;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 /**
  * Created by yao on 17-9-26.
@@ -135,13 +132,13 @@ public class RightsController {
             HttpUtil.responseApiCode(request, response, ApiCode.RECORD_EXIST);
             return;
         }
-        List<Channel> cList = channelService.SearchById(rights.getIdChannel());
+        Channel channel = channelService.SearchById(rights.getIdChannel());
         List<Grouping> gList = groupingService.SearchById(rights.getIdGroup());
-        if(cList==null || cList.size()<1 || gList==null || gList.size()<1){
+        if(Objects.isNull(channel) || gList==null || gList.size()<1){
             HttpUtil.responseApiCode(request, response, ApiCode.PARAMETER_ERROR);
             return;
         }
-        rights.setNameChannel(cList.get(0).getName());
+        rights.setNameChannel(channel.getName());
         rights.setNameGroup(gList.get(0).getName());
         rights.setStatus(Status.ONLINE.getCode());
         rights.setCreated(new Timestamp(new Date().getTime()));

+ 3 - 3
src/main/java/cn/efunbox/audio/impl/ChannelServiceImpl.java

@@ -40,9 +40,9 @@ public class ChannelServiceImpl implements ChannelService {
     }
 
     @Override
-    public List<Channel> SearchById(Long id){
-        List<Channel> list = channelRepo.findById(id);
-        return list;
+    public Channel SearchById(Long id){
+        Channel channel = channelRepo.findOne(id);
+        return channel;
     }
 
     @Override

+ 1 - 1
src/main/java/cn/efunbox/audio/service/ChannelService.java

@@ -15,7 +15,7 @@ public interface ChannelService {
 
     public Page<Channel> SearchAll(int page, int size);
 
-    public List<Channel> SearchById(Long id);
+    public Channel SearchById(Long id);
 
     public List<Channel> SearchByIdFather(Long idFather);
 

+ 77 - 2
src/main/java/cn/efunbox/audio/utils/Encrypt.java

@@ -1,10 +1,14 @@
 package cn.efunbox.audio.utils;
 
+import com.alibaba.fastjson.JSONObject;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.codec.binary.Hex;
 import org.apache.commons.lang.StringUtils;
 
+import javax.crypto.Mac;
+import javax.crypto.spec.SecretKeySpec;
 import java.io.UnsupportedEncodingException;
+import java.nio.charset.StandardCharsets;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.util.*;
@@ -113,12 +117,83 @@ public class Encrypt {
         return stringBuffer.toString();
     }
 
+    public static String createHMACSHA256(Map<String, String> paramMap, String signKey) {
+
+        List<String> sortedKeys = new ArrayList<>();
+        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.deleteCharAt(buff.length() - 1);
+
+        try {
+            return HMACSHA256(buff.toString(),signKey);
+        } catch (Exception e) {
+            throw new RuntimeException("签名错误");
+        }
+    }
+
+    public static String HMACSHA256(String data, String key) throws Exception {
+
+        Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
+
+        SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
+
+        sha256_HMAC.init(secret_key);
+
+        byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
+
+        StringBuilder sb = new StringBuilder();
+
+        for (byte item : array) {
+
+            sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
+
+        }
+
+        return sb.toString().toUpperCase();
+
+    }
+
 
     public static void main(String[] args) {
         Map<String,String> param = new HashMap<>();
         param.put("idChannel","1000");
-        param.put("idDevice","acf76362e77441fd8329384345d54156");
-        String sign = Encrypt.createSHA256Sign(param,"IhOTiTyMLDNNLFuP");
+        param.put("idDevice","test");
+        param.put("ts",System.currentTimeMillis() / 1000 + "");
+        System.out.println(JSONObject.toJSONString(param));
+//        String sign = Encrypt.createHMACSHA256(param,"IhOTiTyMLDNNLFuP");
+        String sign = Encrypt.createHMACSHA256(param,"AzaSB2RR0boUz1WQ");
         log.info(sign);
+
+        String encode = Base64.getEncoder().encodeToString(sign.getBytes(StandardCharsets.UTF_8));
+
+        log.info(encode);
+
+        String decode = new String(Base64.getDecoder().decode(encode), StandardCharsets.UTF_8);
+        log.info(decode);
+
+
     }
 }