package com.qxzl.client.common.filter;
import com.alibaba.fastjson.JSON;
import com.qxzl.client.common.controller.BaseController;
import com.qxzl.util.ObjectHelper;
import org.json.JSONObject;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @Description http拦截器
* @Author Created by
* @Email hccpw@sina.com
* @Date ${DATE}
*/
@Configuration
@Component
public class RequestInterceptor extends WebMvcConfigurerAdapter implements HandlerInterceptor {
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 多个拦截器组成一个拦截器链
// addPathPatterns 用于添加拦截规则
// excludePathPatterns 用户排除拦截
registry.addInterceptor(this).addPathPatterns("/**");
// registry.addInterceptor(new MyInterceptor2()).addPathPatterns("/**");
super.addInterceptors(registry);
}
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
String method = request.getMethod();
String requestURI = request.getRequestURI();
String protocol = request.getProtocol();
String serverName = request.getServerName();
int serverPort = request.getServerPort();
String remoteAddr = request.getRemoteAddr();
Map<String, String[]> parameterMap = request.getParameterMap();
String parameters = "";
try {
JSONObject jsonObject = new JSONObject(parameterMap);
parameters =jsonObject.toString();
}catch (Exception e){
e.printStackTrace();
}
String localRequest = "";
try {
String str = parameters.replaceAll("[\\]|\\[]", "");
Map<String,String> map = (Map) JSON.parse(str);
try {
String token = map.get("token");
if (!ObjectHelper.isEmpty(token)&&token.length()!=0){
request.setAttribute(BaseController.TOKEN,token);
}
}catch (Exception e){
e.printStackTrace();
}
String host = "http://127.0.0.1:8080"+requestURI;
localRequest =getGETUrl(host,map);
}catch (Exception e){
e.printStackTrace();
}
System.out.println("╔═════════════════════════════════════════════════════════════════");
System.out.println(remoteAddr+" 发起 "+method+" 请求: ");
System.out.println(requestURI);
System.out.println("参数: "+parameters);
System.out.println(localRequest);
System.out.println("╚═════════════════════════════════════════════════════════════════");
return true;// 只有返回true才会继续向下执行,返回false取消当前请求
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
}
static String getGETUrl(String URLstr, Map<String, String> map) {
if (map == null) {
map = new HashMap<String, String>();
}
StringBuilder builder = new StringBuilder();
if (URLstr.contains("?")) {
builder.append(URLstr).append("&");
} else {
builder.append(URLstr).append("?");
}
for (String str : map.keySet()) {
String value = map.get(str);
// if (RegexTool.isHaveChinese(value)){
// try {
// builder.append(str).append("=").append(URLEncoder.encode(value,"utf-8")).append("&");
builder.append(str).append("=").append(value).append("&");
// }catch (Exception e){
// }
// }else {
builder.append(str).append("=").append(value).append("&");
// }
}
String endUrl = builder.toString();
if (endUrl.endsWith("&") || endUrl.endsWith("?")) {
endUrl = endUrl.substring(0, endUrl.length() - 1);
}
return endUrl;
}
}