Commit f30aa02a authored by Quxl's avatar Quxl

x

parent 32a02e25
package com.egolm.shop.common.interceptor; package com.egolm.shop.common.interceptor;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.SortedMap; import java.util.SortedMap;
import java.util.TreeMap; import java.util.TreeMap;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import com.egolm.common.GsonUtil;
import com.egolm.common.MD5Util; import com.egolm.common.GsonUtil;
import com.egolm.common.StringUtil; import com.egolm.common.MD5Util;
import com.egolm.shop.common.XException; import com.egolm.common.StringUtil;
import com.egolm.shop.common.utils.I18NUtils; import com.egolm.common.Util;
import com.egolm.shop.common.utils.ShopContstrant; import com.egolm.shop.common.XException;
import com.egolm.shop.common.utils.I18NUtils;
@Component import com.egolm.shop.common.utils.ShopContstrant;
public class OpenInterceptor extends HandlerInterceptorAdapter {
Log logger = LogFactory.getLog(OpenInterceptor.class); @Component
String encoding = "UTF-8"; public class OpenInterceptor extends HandlerInterceptorAdapter {
Log logger = LogFactory.getLog(OpenInterceptor.class);
@Autowired String encoding = "UTF-8";
private RedisTemplate<String, Object> redisTemplate;
@Value("${ignoreTimestamp:false}")
/*签名规则:1.按照参数字段名ASCII码排序;2.参数进行url拼接;3.加上签名字符串secret;4.得到字符串后进行SHA1签名;5.将签名得到的字符串加入参数列表中,字段名为sig*/ private Boolean ignoreTimestamp;
@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception { @Autowired
String uri = req.getRequestURI(); private RedisTemplate<String, Object> redisTemplate;
if((uri.indexOf("swagger") == -1 && uri.indexOf("/v2/api-docs") == -1) ) {
Map<String, String[]> paramMap = req.getParameterMap(); /*签名规则:1.按照参数字段名ASCII码排序;2.参数进行url拼接;3.加上签名字符串secret;4.得到字符串后进行SHA1签名;5.将签名得到的字符串加入参数列表中,字段名为sig*/
logger.info(GsonUtil.toJson(paramMap)); @Override
String sign = req.getParameter("sign"); public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception {
String timestamp = req.getParameter("timestamp"); String uri = req.getRequestURI();
String langID = req.getParameter("langID"); if((uri.indexOf("swagger") == -1 && uri.indexOf("/v2/api-docs") == -1) ) {
String terminal = req.getParameter("terminal"); Map<String, String[]> paramMap = req.getParameterMap();
logger.info(GsonUtil.toJson(paramMap));
if(!StringUtil.isNotBlank(sign,timestamp,langID,terminal)) { String sign = req.getParameter("sign");
throw new XException(I18NUtils.getMessage(langID, "Msg_Paramter_Empty","[sign,timestamp,langID,terminal]")) ; String timestamp = req.getParameter("timestamp");
} String langID = req.getParameter("langID");
String terminal = req.getParameter("terminal");
SortedMap<Object,Object> parameters = new TreeMap<Object,Object>();
Set<String> keys = paramMap.keySet() ; if(!StringUtil.isNotBlank(sign,timestamp,langID,terminal)) {
for (String key : keys) { throw new XException(I18NUtils.getMessage(langID, "Msg_Paramter_Empty","[sign,timestamp,langID,terminal]")) ;
String value = req.getParameter(key); }
parameters.put(key,value);
} Long timeLong = Util.objTo(timestamp, Long.class, 0L);
redisTemplate.opsForHash().put("SystemCtl1111", "test","111"); if(!ignoreTimestamp && Math.abs(timeLong - System.currentTimeMillis()) > (60L*5L)) {
System.out.println(redisTemplate.opsForHash().get("SystemCtl1111", "test")); throw new XException("时间戳无效");
}
Map<?, ?> map = (Map<?, ?>)redisTemplate.opsForHash().get(ShopContstrant.SystemCtl_Redis_Base_Key, ShopContstrant.CLIENT_MD5KEY);
System.out.println(map); SortedMap<Object,Object> parameters = new TreeMap<Object,Object>();
if(map != null) { Set<String> keys = paramMap.keySet() ;
parameters.put("md5Key", map.get("sValue1")+""); for (String key : keys) {
}else { String value = req.getParameter(key);
logger.info("从redis中获取 计算签名用的md5key 获取失败"); parameters.put(key,value);
throw new XException(I18NUtils.getMessage(langID, "Msg_Sign_failure")); }
} redisTemplate.opsForHash().put("SystemCtl1111", "test","111");
System.out.println(redisTemplate.opsForHash().get("SystemCtl1111", "test"));
logger.info("request sign >>>>>>"+sign);
String createSign = MD5Util.createSign(encoding, parameters); Map<?, ?> map = (Map<?, ?>)redisTemplate.opsForHash().get(ShopContstrant.SystemCtl_Redis_Base_Key, ShopContstrant.CLIENT_MD5KEY);
logger.info("create sign >>>>>>>"+createSign); System.out.println(map);
if(createSign.toUpperCase().equals(sign.toUpperCase())) { if(map != null) {
return super.preHandle(req, resp, handler); parameters.put("md5Key", map.get("sValue1")+"");
} else { }else {
throw new XException(I18NUtils.getMessage(langID, "Msg_Sign_failure")); logger.info("从redis中获取 计算签名用的md5key 获取失败");
} throw new XException(I18NUtils.getMessage(langID, "Msg_Sign_failure"));
}else { }
return super.preHandle(req, resp, handler); //swagger-ui.html 界面不进行验证
} logger.info("request sign >>>>>>"+sign);
} String createSign = MD5Util.createSign(encoding, parameters);
logger.info("create sign >>>>>>>"+createSign);
public static void main(String[] args) { if(createSign.toUpperCase().equals(sign.toUpperCase())) {
String a= "/A0001/20181023/2018_10_23_17_27_00_58303479.png"; return super.preHandle(req, resp, handler);
System.out.println(a.length()); } else {
} throw new XException(I18NUtils.getMessage(langID, "Msg_Sign_failure"));
} }
}else {
return super.preHandle(req, resp, handler); //swagger-ui.html 界面不进行验证
}
}
public static void main(String[] args) {
String a= "/A0001/20181023/2018_10_23_17_27_00_58303479.png";
System.out.println(a.length());
}
}
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