6 changed files with 241 additions and 8 deletions
@ -0,0 +1,86 @@
|
||||
package com.logpm.factory.interceptor; |
||||
|
||||
import com.alibaba.fastjson.JSONObject; |
||||
import com.logpm.factory.config.RequestWrapper; |
||||
import com.logpm.factory.props.ZbFactoryProperties; |
||||
import com.logpm.factory.zb.untils.QmSignUntil; |
||||
import lombok.AllArgsConstructor; |
||||
import lombok.extern.log4j.Log4j2; |
||||
import org.springblade.core.tool.api.R; |
||||
import org.springframework.web.servlet.HandlerInterceptor; |
||||
import org.springframework.web.servlet.ModelAndView; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.io.IOException; |
||||
import java.io.PrintWriter; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
@Log4j2 |
||||
@AllArgsConstructor |
||||
public class ZbFactoryAccountsInterceptor implements HandlerInterceptor { |
||||
|
||||
private final ZbFactoryProperties zbFactoryProperties; |
||||
|
||||
@Override |
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { |
||||
log.info("#################preHandle: 志邦权限 验证开始"); |
||||
RequestWrapper myRequestWrapper = new RequestWrapper(request); |
||||
|
||||
String method = request.getParameter("method"); |
||||
String timestamp = request.getParameter("timestamp"); |
||||
String format = request.getParameter("format"); |
||||
String appKey = request.getParameter("app_key"); |
||||
String v = request.getParameter("v"); |
||||
String sign = request.getParameter("sign"); |
||||
String signMethod = request.getParameter("sign_method"); |
||||
String customerId = request.getParameter("customerId"); |
||||
|
||||
String body = myRequestWrapper.getBody(); |
||||
JSONObject bodyJson = JSONObject.parseObject(body); |
||||
|
||||
Map<String, String> params = new HashMap<>(); |
||||
params.put("app_key",appKey); |
||||
params.put("customerId",customerId); |
||||
params.put("format",format); |
||||
params.put("method",method); |
||||
params.put("sign_method",signMethod); |
||||
params.put("timestamp",timestamp); |
||||
params.put("v",v); |
||||
|
||||
String newSign = QmSignUntil.signTopRequest(params, zbFactoryProperties.getSecretkey(), signMethod, bodyJson.toJSONString()); |
||||
if(!newSign.equals(sign)){ |
||||
returnJson(response,JSONObject.toJSONString(R.fail(203,"签名认证失败"))); |
||||
return false; |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
|
||||
private void returnJson(HttpServletResponse response, String json) { |
||||
PrintWriter writer = null; |
||||
response.setCharacterEncoding("UTF-8"); |
||||
response.setContentType("application/json"); |
||||
try { |
||||
writer = response.getWriter(); |
||||
writer.print(json); |
||||
|
||||
} catch (IOException e) { |
||||
System.out.println(e.getMessage()); |
||||
} finally { |
||||
if (writer != null) |
||||
writer.close(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { |
||||
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView); |
||||
} |
||||
|
||||
@Override |
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { |
||||
HandlerInterceptor.super.afterCompletion(request, response, handler, ex); |
||||
} |
||||
} |
@ -0,0 +1,16 @@
|
||||
package com.logpm.factory.props; |
||||
|
||||
import lombok.Data; |
||||
import org.springframework.boot.context.properties.ConfigurationProperties; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
@Data |
||||
@ConfigurationProperties(prefix = "zb") |
||||
@Component |
||||
public class ZbFactoryProperties { |
||||
|
||||
private String appKey; |
||||
private String customerid; |
||||
private String secretkey; |
||||
|
||||
} |
@ -0,0 +1,128 @@
|
||||
package com.logpm.factory.zb.untils; |
||||
|
||||
import javax.crypto.Mac; |
||||
import javax.crypto.SecretKey; |
||||
import javax.crypto.spec.SecretKeySpec; |
||||
import java.io.IOException; |
||||
import java.security.GeneralSecurityException; |
||||
import java.security.MessageDigest; |
||||
import java.util.Arrays; |
||||
import java.util.Map; |
||||
|
||||
public class QmSignUntil { |
||||
|
||||
public static String signTopRequest(Map<String, String> params, String secret, String signMethod, String body) throws Exception { |
||||
// 第一步:检查参数是否已经排序
|
||||
String[] keys = params.keySet().toArray(new String[0]); |
||||
Arrays.sort(keys); |
||||
|
||||
// 第二步:把所有参数名和参数值串在一起
|
||||
StringBuilder query = new StringBuilder(); |
||||
if ("md5".equalsIgnoreCase(signMethod)) { |
||||
query.append(secret); |
||||
} |
||||
for (String key : keys) { |
||||
String value = params.get(key); |
||||
String[] array = new String[2]; |
||||
array[0] = key; |
||||
array[1] = value; |
||||
if (areNotEmpty(array)) { |
||||
query.append(key).append(value); |
||||
} |
||||
} |
||||
|
||||
// 第三步:使用MD5/HMAC加密
|
||||
byte[] bytes; |
||||
if ("hmac".equalsIgnoreCase(signMethod)) { |
||||
bytes = encryptHMAC(query.toString(), secret); |
||||
} else { |
||||
query.append(body).append(secret); |
||||
bytes = encryptMD5(query.toString()); |
||||
} |
||||
|
||||
// 第四步:把二进制转化为大写的十六进制(正确签名应该为32大写字符串,此方法需要时使用)
|
||||
return byte2hex(bytes); |
||||
} |
||||
|
||||
public static boolean areNotEmpty(String[] values) { |
||||
boolean result = true; |
||||
if ((values == null) || (values.length == 0)) |
||||
result = false; |
||||
else { |
||||
for (String value : values) { |
||||
result &= !isEmpty(value); |
||||
} |
||||
} |
||||
return result; |
||||
} |
||||
static boolean isEmpty(String value) { |
||||
int strLen; |
||||
if ((value == null) || ((strLen = value.length()) == 0)) |
||||
return true; |
||||
for (int i = 0; i < strLen; i++) { |
||||
if (!Character.isWhitespace(value.charAt(i))) { |
||||
return false; |
||||
} |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
static byte[] encryptHMAC(String data, String secret) throws IOException { |
||||
byte[] bytes = null; |
||||
try { |
||||
SecretKey secretKey = new SecretKeySpec(secret.getBytes("utf-8"), "HmacMD5"); |
||||
Mac mac = Mac.getInstance(secretKey.getAlgorithm()); |
||||
mac.init(secretKey); |
||||
bytes = mac.doFinal(data.getBytes("utf-8")); |
||||
} catch (GeneralSecurityException gse) { |
||||
throw new IOException(gse.toString()); |
||||
} |
||||
return bytes; |
||||
} |
||||
|
||||
public static byte[] encryptMD5(String data) throws Exception { |
||||
MessageDigest md = MessageDigest.getInstance("MD5"); |
||||
|
||||
byte[] bytes = md.digest(data.getBytes("utf-8")); |
||||
return bytes; |
||||
// return encryptMD5(data.getBytes(Constants.CHARSET_UTF8));
|
||||
} |
||||
|
||||
public static String byte2hex(byte[] bytes) { |
||||
StringBuilder sign = new StringBuilder(); |
||||
for (int i = 0; i < bytes.length; i++) { |
||||
String hex = Integer.toHexString(bytes[i] & 0xFF); |
||||
if (hex.length() == 1) { |
||||
sign.append("0"); |
||||
} |
||||
sign.append(hex.toUpperCase()); |
||||
} |
||||
return sign.toString(); |
||||
} |
||||
|
||||
|
||||
// public static void main(String[] args) {
|
||||
//
|
||||
// Map<String, String> params = new HashMap<>();
|
||||
// params.put("app_key","HT");
|
||||
// params.put("customerid","HT");
|
||||
// params.put("format","json");
|
||||
// params.put("method","entry.order.create");
|
||||
// params.put("sign_method","md5");
|
||||
// params.put("timestamp","2015-04-26 00:00:07");
|
||||
// params.put("v","1.0");
|
||||
//
|
||||
// JSONObject jsonObject = new JSONObject();
|
||||
// jsonObject.put("name","name12345");
|
||||
// jsonObject.put("age",10);
|
||||
//
|
||||
// try {
|
||||
// String s = QmSignUntil.signTopRequest(params, "test", "md5", jsonObject.toJSONString());
|
||||
// System.out.println(s);
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
} |
Loading…
Reference in new issue