spring boot 拦截器
在interceptor包下创建ApiInterceptor和WebConfig
ApiInterceptor
package com.media.weappapi.interceptor;
import com.media.common.po.config.TCmsSite;
import com.media.common.util.MD5Util;
import com.media.common.util.MsgUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.Map.Entry;
@Component
public class ApiInterceptor implements HandlerInterceptor {
private static Logger logger = LoggerFactory.getLogger(ApiInterceptor.class);
public static final String SIGNATURE = "signature";
public static final String SITEID = "siteId";
public static final String APISIGN = "apiSign";
private Map<String,String> apiSignMap = new HashMap<String, String>();
{
apiSignMap.put(SITEID, APISIGN);
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
//开发者模式时runType参数添加test不进行拦截
if ("test".equals(request.getParameter("runType"))) {
return true;
}
logger.info("—————————————————进入拦截模式————————————————————————————");
//判断是否是文件上传,文件上传的请求不拦截
// String contentType=request.getHeader("Content-Type");
// logger.info("contentType:"+contentType);
// if(StringUtils.isNotBlank(contentType) && contentType.startsWith("multipart")){
// return true;
// }
String signature = request.getParameter(SIGNATURE);
logger.info(request.toString()+"\t" +"signature:"+"\t"+signature);
logger.info(request.toString()+"\t" +"url:"+"\t"+request.getRequestURI());
String siteId = request.getParameter("siteId");
String currentTimeMillis = request.getParameter("currentTimeMillis");
if (StringUtils.isBlank(siteId)) {
response.getWriter().println(MsgUtil.fail("站点id不能为空"));
return false;
}
if (StringUtils.isBlank(currentTimeMillis)) {
response.getWriter().println(MsgUtil.fail("时间戳不能为空"));
return false;
}
// 判断签名是否正确
StringBuffer sbf = new StringBuffer();
sbf.append(siteId);
sbf.append(currentTimeMillis);
logger.info("paramList加密前参数:" + sbf.toString());
String createSign = MD5Util.getMD5String(sbf.toString());
logger.info("paramList加密后:" + createSign);
if(!(signature.equals(createSign))){
logger.info(MsgUtil.fail("签名错误"));
response.getWriter().append(MsgUtil.fail("签名错误"));
}else{
logger.info(MsgUtil.success("签名正确"));
}
return (signature.equals(createSign));
}
}
WebConfig
package com.media.weappapi.interceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @author :wx
* @date :2019/3/68 16:03
* @description:api接口工程拦截配置
*/
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Bean
public HandlerInterceptor getApiInterceptor() {
return new ApiInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
InterceptorRegistration interceptorRegistration = registry.addInterceptor(getApiInterceptor());
interceptorRegistration.excludePathPatterns("/static/**");
interceptorRegistration.excludePathPatterns("/error");
interceptorRegistration.excludePathPatterns("/root/**");
interceptorRegistration.excludePathPatterns("/server/**");
interceptorRegistration.addPathPatterns("/**");
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2022-03-03 时序图