SHA1工具类
package com.pbh.util;
import java.security.MessageDigest;
public class MD5 {
public final static String signature(String s) {
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };
try {
byte[] strTemp = s.getBytes("utf-8");
//使用MD5创建MessageDigest对象
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(strTemp);
byte[] md = mdTemp.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte b = md[i];
//System.out.println((int)b);
str[k++] = hexDigits[b >> 4 & 0xf];
str[k++] = hexDigits[b & 0xf];
}
return new String(str);
} catch (Exception e) {return null;}
}
}
MD5加密工具类
package com.pbh.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.pbh.config.GlobalConfig;
/**
*
* Title:SHA1
* Description:使用SHA1加密
* @author PanBaihui
* @date 2017年8月16日 下午2:03:29
*/
public class SHA1 {
private static String[] HexCode = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
private static String appKey = "1234567894521E34SDF994ASDES3";
/**
*
* @Title: isTrueSign
* @Description: TODO(验证APP签名正确性)
* @param entity
* @param sign 不签名字段
* @return boolean 返回类型
* @throws
* 注意使用该方法的entity不能有除属性外其他的get方法否则签名验证失败
*/
public static
boolean isTrueSign(T entity,String sign) {
try {
String appSign = EntityFieldUtil.getFieldValue(entity, sign).toString();
String serverSign = buildRequestMysign(EntityFieldUtil.getEntityFieldMap(entity,sign));
return appSign.equals(serverSign);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 生成签名结果
* @param sPara 要签名的数组
* @return 签名结果字符串
*/
public static String buildRequestMysign(Map
sPara) {
String prestr = createLinkString(sPara); //把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
return signature(prestr,appKey);
}
/**
* 把数组所有元素排序,并按照“参数=参数值”的模式用“&”字符拼接成字符串
* @param params 需要排序并参与字符拼接的参数组
* @return 拼接后字符串
*/
public static String createLinkString(Map
params) {
List
keys = new ArrayList
(params.keySet()); Collections.sort(keys); String prestr = ""; for (int i = 0; i < keys.size(); i++) { String key = keys.get(i); String value = params.get(key).toString(); if (i == keys.size() - 1) {//拼接时,不包括最后一个&字符 prestr = prestr + key + "=" + value; } else { prestr = prestr + key + "=" + value + "&"; } } return prestr; } /** * 根据参数和key签名 * @param inStr * @param key * @return */ public static String signature(String inStr,String key) { MessageDigest md = null; String outStr = null; try { inStr = MD5.signature(inStr + key); md = MessageDigest.getInstance("SHA-1"); byte[] digest = md.digest(inStr.getBytes()); //outStr = bytetoString(digest); outStr = byteArrayToHexString(digest); } catch (NoSuchAlgorithmException nsae) { nsae.printStackTrace(); } return outStr; } /** * 将byte[]组转成string * @param digest * @return */ public static String bytetoString(byte[] digest) { String str = ""; String tempStr = ""; for (int i = 1; i < digest.length; i++) { tempStr = (Integer.toHexString(digest[i] & 0xff)); if (tempStr.length() == 1) { str = str + "0" + tempStr; } else { str = str + tempStr; } } return str.toLowerCase(); } public static String byteArrayToHexString(byte[] b) { String result = ""; for (int i = 0; i < b.length; i++) { result = result + byteToHexString(b[i]); } return result; } public static String byteToHexString(byte b) { int n = b; if (n < 0) { n = 256 + n; } int d1 = n / 16; int d2 = n % 16; return HexCode[d1] + HexCode[d2]; } }
泛型bean操作类
package com.pbh.util;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
/**
*
* @ClassName: EntityFieldUtil
* @Description: TODO(获取实体属性值不为空的属性map)
* @author PanBaihui
* @date 2017年8月19日 下午11:29:18
*
*/
public class EntityFieldUtil {
/**
*
* @Title: getEntityFieldMap
* @Description: TODO(得到类中属性值不为空的属性map(属性为key,值为value))
* @param entity
* @param sign 非签名字段
* @throws Exception
* @return Map
返回类型
* 该类可以返回包括父类的所有属性集合
*/
public static
Map
getEntityFieldMap(T entity,
String sign) throws Exception {
@SuppressWarnings("rawtypes")
Class bean = (Class) entity.getClass();
// 得到类中属性值不为空的属性map(属性为key,值为value)
Map
map = new HashMap
(); Method[] methods = bean.getMethods();// 获取所有方法 for (int i = 0; i < methods.length; i++) { Method method = methods[i]; if (method.getName().startsWith("get")) {// 获取所有get方法 if (!method.getName().equals("getClass")) {// 去除getClass方法 if (method.invoke(entity) != null) {// 去除值为null的属性 map.put(getFieldName(method.getName(), "get"),method.invoke(entity)); } } } } map.remove(sign);// 去除非签名字段 return map; } /** * * @Title: getFieldValue * @Description: TODO(获取类属性值) * @param entity * @param fieldName * @throws Exception * @return Object 返回类型 * 该类不支持父类属性的获取 */ public static
Object getFieldValue(T entity, String fieldName) throws Exception { // 得到类对象 @SuppressWarnings("rawtypes") Class userCla = (Class) entity.getClass(); Field[] fs = userCla.getDeclaredFields (); for ( int i = 0 ; i < fs. length ; i++){ Field f = fs[i]; f.setAccessible(true); // 设置些属性是可以访问的 if (f.getName().equals(fieldName)) { return f.get(entity); } } return null; } /** * * @Title: getFieldName * @Description: TODO(根据get或set方法返回属性名) * @param methodName * @param replace * @return String 返回类型 */ public static String getFieldName(String methodName, String replace) { methodName = methodName.replace(replace, ""); return methodName.substring(0, 1).toLowerCase() + methodName.substring(1); } }
spring断言扩展类
package com.pbh.util;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
*
* Title:AssertUtil
* Description:扩展Assert类,用于校验参数返回同一错误Message
* @author PanBaihui
* @date 2017年8月17日 下午3:33:27
*/
public class AssertUtil extends Assert{
private static final String errorMessage = "params is error";
/**
* expression为false抛出异常
* @param expression
*/
public static void isTrueParams(boolean expression) {
if (!expression) {
throw new IllegalArgumentException(errorMessage);
}
}
/**
* object不为空抛出异常
* @param object
*/
public static void isNullParams(Object object) {
if (object != null) {
throw new IllegalArgumentException(errorMessage);
}
}
/**
* object为空抛出异常
* @param object
*/
public static void notNullParams(Object object) {
if (object == null) {
throw new IllegalArgumentException(errorMessage);
}
}
/**
* 字符串Length为0抛出异常
* @param text
*/
public static void hasLengthParams(String text) {
if (!StringUtils.hasLength(text)) {
throw new IllegalArgumentException(errorMessage);
}
}
/**
* 字符串去空后Length为0抛出异常
* @param text
*/
public static void hasTextParams(String text) {
if (!StringUtils.hasText(text)) {
throw new IllegalArgumentException(errorMessage);
}
}
/**
* Integer去空后Length为0抛出异常
* @param text
*/
public static void hasTextParams(Integer text) {
if (!StringUtils.hasText(text.toString())) {
throw new IllegalArgumentException(errorMessage);
}
}
}
使用代码
//验证签名
AssertUtil.isTrueParams(SHA1.isTrueSign(entity, "sign"));