package cn.autoform.web.interceptor.spring; import cn.autoform.bean.system.UserInfo; import cn.autoform.util.Des1; import cn.autoform.util.thread.Keys; import cn.autoform.util.thread.ThreadData; import cn.autoform.util.tool.ObjectUtil; import cn.autoform.web.interceptor.filter.CloneRequestFilter; import cn.autoform.web.service.login.LoginService; import com.codingapi.tx.aop.bean.TxCompensateLocal; import com.codingapi.tx.aop.bean.TxTransactionLocal; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.annotation.Resource; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.net.URLDecoder; import java.util.*; /** * 初始化线程参数 * @author WangYX * */ @Service("initParamInterceptor") @Slf4j public class InitParamInterceptor implements HandlerInterceptor{ /** * 初始化参数key */ private static final List PARAM_KEY = new ArrayList<>(); /** * 访问对外接口参数 */ public static final Map COOKIE_KEY = new HashMap<>(); @Resource private LoginService loginService; static{ // 表单id PARAM_KEY.add(Keys.FORM_ID); // 租户id PARAM_KEY.add(Keys.TENANT_ID); // 页码 PARAM_KEY.add(Keys.PAGE_NUM); // 页条数 PARAM_KEY.add(Keys.PAGE_SIZE); // 数据存储标识 PARAM_KEY.add(Keys.IO_FLAG); // tenantId COOKIE_KEY.put("tenantId",Keys.TENANT_ID); // userName COOKIE_KEY.put("userName",Keys.USER_NAME); // openId COOKIE_KEY.put("openId",Keys.OPEN_ID); // comanyId COOKIE_KEY.put("companyId",Keys.COMPANY_ID); // serviceId COOKIE_KEY.put("serviceId",Keys.SERVICE_ID); // token COOKIE_KEY.put("token",Keys.TOKEN); // dataSourceId COOKIE_KEY.put("dataSourceId",Keys.DATA_SOURCE_ID); // 国际化 COOKIE_KEY.put("Language", Keys.USER_LANGUAGE); } @SuppressWarnings("unchecked") @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { final CloneRequestFilter.BodyReaderHttpServletRequestWrapper requestWrapper = new CloneRequestFilter.BodyReaderHttpServletRequestWrapper(request); Map paramMap = requestWrapper.getParameterMap(); PARAM_KEY.stream().forEach(key->{ String[] val = paramMap.get(key.getKey()); if(ObjectUtil.empty(val) || val.length == 0) return; ThreadData.set(key , key.format(val[0])); }); //取出cookie添加到线程中 Cookie[] cookie = request.getCookies(); if(cookie!=null){ for(Cookie cook : cookie){ if(!ObjectUtil.empty(COOKIE_KEY.get(cook.getName()))){ String str = URLDecoder.decode(cook.getValue(),"UTF-8").replaceAll(" ","+"); ThreadData.set(COOKIE_KEY.get(cook.getName()),Des1.desDecrypt(str,Des1.SECRETKEY)); if(Objects.equals("Language", cook.getName())){ com.iemsoft.framework.cloud.core.thread.ThreadData.set( com.iemsoft.framework.cloud.core.thread.Keys.USER_LANGUAGE, Des1.desDecrypt(str,Des1.SECRETKEY) ); } } } } loginService.getUserInfo(new UserInfo.LogInInfo(ThreadData.get(Keys.TOKEN),ThreadData.get(Keys.OPEN_ID))); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // 清空线程参数 TxCompensateLocal.clear(); TxTransactionLocal.clear(); ThreadData.clear(); com.iemsoft.framework.cloud.core.thread.ThreadData.clear(); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // 清空线程参数 TxCompensateLocal.clear(); TxTransactionLocal.clear(); ThreadData.clear(); com.iemsoft.framework.cloud.core.thread.ThreadData.clear(); } }