|
|
@@ -1,5 +1,7 @@
|
|
|
package org.springblade.common.filter.utils;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.crypto.SecureUtil;
|
|
|
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
|
|
|
@@ -24,6 +26,7 @@ import javax.crypto.SecretKey;
|
|
|
import javax.servlet.*;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
+import javax.servlet.http.Part;
|
|
|
import java.io.IOException;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
import java.sql.Struct;
|
|
|
@@ -39,23 +42,14 @@ import static cn.hutool.core.util.StrUtil.*;
|
|
|
@Component
|
|
|
public class HttpUtil {
|
|
|
|
|
|
- @Value("${crypto.charset}")
|
|
|
- private String cryptoCharset = "UTF-8";
|
|
|
@Value("${crypto.key}")
|
|
|
private String cryptoKey;
|
|
|
- @Value("${crypto.signKey}")
|
|
|
- private String signKey;
|
|
|
|
|
|
public void modifyHttpData(ServletRequest request, ServletResponse response, FilterChain chain) throws Exception {
|
|
|
HttpServletRequest originalRequest = (HttpServletRequest) request;
|
|
|
String url = originalRequest.getRequestURI();
|
|
|
- if (url.indexOf("put-file") > 0) {
|
|
|
- chain.doFilter(request,response);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if (HttpMethod.POST.matches(originalRequest.getMethod())) {
|
|
|
- //只处理post请求
|
|
|
+ if (HttpMethod.POST.matches(originalRequest.getMethod()) && url.indexOf("put-file") < 0) {
|
|
|
+ //只处理不是上传文件的post请求
|
|
|
this.handelPost(request,response,chain,originalRequest);
|
|
|
}else{
|
|
|
//其他请求直接通过
|
|
|
@@ -88,59 +82,11 @@ public class HttpUtil {
|
|
|
private void handelPost(ServletRequest request, ServletResponse response, FilterChain chain,HttpServletRequest originalRequest) throws Exception {
|
|
|
//密文
|
|
|
String originalRequestBody = ServletUtil.getBody(request);
|
|
|
- String modifyRequestBody = "{}";
|
|
|
- if (!isBlank(originalRequestBody)) {
|
|
|
- //报文不为空,解密,再验签
|
|
|
- modifyRequestBody = SecureUtil.aes(cryptoKey.getBytes(cryptoCharset)).decryptStr(originalRequestBody);
|
|
|
- }
|
|
|
- Boolean flag = this.handelSign(originalRequest, modifyRequestBody);
|
|
|
- if (!flag) {
|
|
|
- throw new RuntimeException("签名异常");
|
|
|
- }
|
|
|
- //验签通过
|
|
|
- if (!isBlank(originalRequestBody)) {
|
|
|
+ if (StrUtil.isNotBlank(originalRequestBody)) {
|
|
|
//报文不为空,解密
|
|
|
+ String modifyRequestBody = SecureUtil.aes(cryptoKey.getBytes()).decryptStr(originalRequestBody);
|
|
|
request = this.modifyRequestBodyAndContentType(originalRequest, modifyRequestBody, null);
|
|
|
}
|
|
|
chain.doFilter(request, response);
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * 校验签名
|
|
|
- * @param originalRequest
|
|
|
- * @param modifyRequestBody
|
|
|
- * @return
|
|
|
- */
|
|
|
- private Boolean handelSign(HttpServletRequest originalRequest,String modifyRequestBody){
|
|
|
- try {
|
|
|
- //请求头签名
|
|
|
- String sign = ServletUtil.getHeader(originalRequest, "sign", cryptoCharset);
|
|
|
- if (isBlank(sign)) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- //后端签名校验
|
|
|
- TreeMap<String,Object> signMap = JSON.parseObject(modifyRequestBody, TreeMap.class);
|
|
|
- signMap.put("signKey", signKey);
|
|
|
- String valStr = "";
|
|
|
- for (String key : signMap.keySet()) {
|
|
|
- valStr = valStr + signMap.get(key);
|
|
|
- }
|
|
|
- return DigestUtil.md5Hex(valStr).equals(sign);
|
|
|
- } catch (Exception e) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static void main(String[] args) {
|
|
|
- String keyStr = "cyzh-ldt";
|
|
|
- byte[] keys;
|
|
|
- try {
|
|
|
- keys = keyStr.getBytes("UTF-8");
|
|
|
- System.out.println(Base64Utils.encodeToString(Arrays.copyOf(keys, 16)));
|
|
|
- } catch (UnsupportedEncodingException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
}
|