Commit e3e30701 authored by zhangc's avatar zhangc

commit

parent ad00f8bc
......@@ -3,7 +3,7 @@ package com.jz.dm.gateway.common.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
......
package com.jz.dm.gateway.common.util;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.*;
......@@ -12,7 +12,6 @@ import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
public class RSAUtils {
public static final String CHARSET = "UTF-8";
......@@ -34,7 +33,7 @@ public class RSAUtils {
KeyPair keyPair = kpg.generateKeyPair();
//得到公钥
Key publicKey = keyPair.getPublic();
String publicKeyStr = Base64.encodeBase64URLSafeString(publicKey.getEncoded());
String publicKeyStr =Base64.encodeBase64URLSafeString(publicKey.getEncoded());
//得到私钥
Key privateKey = keyPair.getPrivate();
System.out.println("私钥格式:"+privateKey.getFormat());
......
package com.jz.dm.gateway.common.util;
import com.alibaba.fastjson.PropertyNamingStrategy;
import com.jz.common.exception.SignatureException;
import com.jz.dm.gateway.common.exception.SignatureException;
import com.jz.dm.gateway.model.signtype.Md5;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
......
......@@ -10,6 +10,7 @@ import com.jz.dm.gateway.model.GatewayResponse;
import com.jz.dm.gateway.model.enums.GatewayResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jz.dm.gateway.constant.Constants;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
......@@ -151,8 +152,7 @@ public class SignatureFilter extends AbstractFilter {
*/
@Override
public int getOrder() {
//return Constants.FILTER_ORDER_0;
return 0;
return Constants.FILTER_ORDER_0;
}
@Override
......
package com.jz.dm.gateway.model.signtype;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* MD5加密算法
*
* @author Admin
* @version $Id: Md5Utils.java 2014年9月3日 下午4:01:08 $
*/
public class Md5 {
private static Logger log = LoggerFactory.getLogger(Md5.class);
private static final String SIGN_TYPE = "MD5";
private static final String CHARSET_NAME = "UTF-8";
private static final String salt = "3zsAa6W9gfSMMhPSlQTdWFUSHY3LS8Vb";
/**
* MD5加密
*
* @param data
* @return
* @throws NoSuchAlgorithmException
*/
public static String encrypt(byte[] data) {
try {
MessageDigest md5 = MessageDigest.getInstance(SIGN_TYPE);
md5.update(data);
return byte2hex(md5.digest());
} catch (NoSuchAlgorithmException e) {
log.debug("md5 加密异常", e);
}
return "";
}
/**
* MD5加密
*
* @param str
* @return
* @throws NoSuchAlgorithmException
*/
public static String encrypt(String str) {
try {
MessageDigest md5 = MessageDigest.getInstance(SIGN_TYPE);
md5.update((str + salt).getBytes(CHARSET_NAME));
return byte2hex(md5.digest());
} catch (Exception e) {
log.debug("md5 加密异常", e);
}
return null;
}
/**
* MD5加盐加密
*
* @param str
* @param salt
* @return
* @throws NoSuchAlgorithmException
*/
public static String encrypt(String str, String salt) {
try {
MessageDigest md5 = MessageDigest.getInstance(SIGN_TYPE);
md5.update((str + salt).getBytes(CHARSET_NAME));
return byte2hex(md5.digest());
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.debug("md5 加密异常", e);
}
}
return "";
}
public static String encrypt(String str, String salt, String charset) {
try {
MessageDigest md5 = MessageDigest.getInstance(SIGN_TYPE);
md5.update((str + salt).getBytes(charset));
return byte2hex(md5.digest());
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.debug("md5 加密异常", e);
}
}
return "";
}
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 byte[] hex2byte(String str) {
if (str == null) {
return null;
}
str = str.trim();
int len = str.length();
if (len <= 0 || len % 2 == 1) {
return null;
}
byte[] b = new byte[len / 2];
try {
for (int i = 0; i < str.length(); i += 2) {
b[(i / 2)] = (byte) Integer.decode("0x" + str.substring(i, i + 2)).intValue();
}
return b;
} catch (Exception e) {
}
return null;
}
/**
* 给TOP请求做MD5签名。
*
* @param sortedParams 所有字符型的TOP请求参数
* @param secret 签名密钥
* @return 签名
* @throws IOException
*/
public static String signRequestNew(Map<String, String> sortedParams, String secret) throws IOException {
// 第一步:把字典按Key的字母顺序排序
List<String> keys = new ArrayList<String>(sortedParams.keySet());
Collections.sort(keys);
// 第二步:把所有参数名和参数值串在一起
StringBuilder query = new StringBuilder();
for (int i = 0; i < keys.size(); i++) {
String key = keys.get(i);
String value = sortedParams.get(key);
if (!StringUtils.isEmpty(key) && !StringUtils.isEmpty(value) && !"sign".equals(key)) {
query.append(key).append("=").append(value);
}
}
log.info("获取当APP请求参数,签名前值为:" + query.toString());
return Md5.encrypt(query.toString(), secret);
}
public static void main(String[] args) {
System.out.println(encrypt("200128008012abc@123", ""));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment