| | |
| | | package com.matrix.system.common.interceptor; |
| | | |
| | | import com.alibaba.fastjson.TypeReference; |
| | | import com.matrix.core.tools.LogUtil; |
| | | import com.matrix.core.tools.StringUtils; |
| | | import com.matrix.core.tools.WebUtil; |
| | | import com.matrix.system.common.bean.SysCompany; |
| | | import com.matrix.system.common.init.InitWebContainer; |
| | | import com.matrix.system.common.init.LocalCache; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.web.servlet.ModelAndView; |
| | | import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; |
| | |
| | | |
| | | /** |
| | | * 域名与公司对应绑定拦截 |
| | | * |
| | | * @author JIANGYOUYAO |
| | | * @email 935090232@qq.com |
| | | * @date 2017年11月29日 |
| | |
| | | public class HostInterceptor extends HandlerInterceptorAdapter { |
| | | |
| | | |
| | | public static final String ATTR_COMPANY = "company"; |
| | | public static final String ATTR_COMPANY = "company"; |
| | | |
| | | @Override |
| | | public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) |
| | | throws Exception { |
| | | } |
| | | @Override |
| | | public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) |
| | | throws Exception { |
| | | } |
| | | |
| | | /** |
| | | * 通用拦截器 |
| | | */ |
| | | @Override |
| | | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception { |
| | | SysCompany company= WebUtil.getSessionAttribute(ATTR_COMPANY); |
| | | /** |
| | | * 通用拦截器 |
| | | */ |
| | | @Override |
| | | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception { |
| | | |
| | | if (company != null) { |
| | | LogUtil.debug("匹配到公司{}", company.getComName()); |
| | | return true; |
| | | } else { |
| | | // 获得请求的域名--由小程序直接传过来companyCode参数对应公司的网址 |
| | | String host = request.getHeader("companyCode"); |
| | | LogUtil.debug("当前请求域名{}", host); |
| | | @SuppressWarnings("unchecked") |
| | | Map<String, SysCompany> companyMap = (Map<String, SysCompany>) WebUtil.getServletContext() |
| | | .getAttribute("companyMap"); |
| | | if(Objects.nonNull(companyMap)){ |
| | | Set<String> hostSet = companyMap.keySet(); |
| | | for (String key : hostSet) { |
| | | LogUtil.debug("-- 匹配公司key={},host={} 匹配结果={}", key , host, key.contains(host)); |
| | | if (key.contains(host)) { |
| | | // 查到公司后存到sesssion中 |
| | | WebUtil.setSessionAttribute(ATTR_COMPANY, companyMap.get(key)); |
| | | return true; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | LogUtil.debug("没有匹配到对应的公司"); |
| | | return false; |
| | | } |
| | | // 获得请求的域名--由小程序直接传过来companyCode参数对应公司的网址 |
| | | String host = request.getHeader("companyCode"); |
| | | LogUtil.debug("当前请求域名{}", host); |
| | | if (StringUtils.isBlank(host)) { |
| | | return false; |
| | | } |
| | | |
| | | Map<String, SysCompany> companyMap = LocalCache.get("companyMap",new TypeReference<Map<String, SysCompany>>(){}); |
| | | SysCompany company=companyMap.get(host); |
| | | if(Objects.nonNull(company)){ |
| | | // 查到公司后存到sesssion中 |
| | | LogUtil.debug("匹配到公司{}", company.getComName()); |
| | | WebUtil.setSessionAttribute(ATTR_COMPANY, company); |
| | | return true; |
| | | } |
| | | LogUtil.debug("没有匹配到对应的公司"); |
| | | return false; |
| | | |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取当前域名对于的公司ID |
| | | * @return |
| | | */ |
| | | public static Long getCompanyId() { |
| | | return ((SysCompany) WebUtil.getSessionAttribute(ATTR_COMPANY)).getComId(); |
| | | } |
| | | /** |
| | | * 获取当前域名对于的公司ID |
| | | * |
| | | * @return |
| | | */ |
| | | public static Long getCompanyId() { |
| | | return ((SysCompany) WebUtil.getSessionAttribute(ATTR_COMPANY)).getComId(); |
| | | } |
| | | |
| | | } |