| package com.matrix.system.common.interceptor; | 
|   | 
| import com.matrix.core.constance.MatrixConstance; | 
| 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 org.springframework.stereotype.Component; | 
| import org.springframework.web.servlet.HandlerInterceptor; | 
| import org.springframework.web.servlet.ModelAndView; | 
| import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; | 
|   | 
| import javax.servlet.http.HttpServletRequest; | 
| import javax.servlet.http.HttpServletResponse; | 
| import java.util.Map; | 
| import java.util.Objects; | 
| import java.util.Set; | 
|   | 
| /** | 
|  * 域名与公司对应绑定拦截 | 
|  * @author JIANGYOUYAO | 
|  * @email 935090232@qq.com | 
|  * @date 2017年11月29日 | 
|  */ | 
| @Component | 
| public class HostInterceptor extends HandlerInterceptorAdapter { | 
|   | 
|   | 
|     public static final String ATTR_COMPANY = "company"; | 
|   | 
|     @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); | 
|   | 
|         if (company != null) { | 
|             LogUtil.debug("匹配到公司{}", company.getComName()); | 
|             return true; | 
|         } else { | 
|         // 获得请求的域名 | 
|         String host = WebUtil.getRequest().getServerName(); | 
|         //判断是否为debug模式 | 
|         if(MatrixConstance.DEBUG){ | 
|             String debugHost=request.getHeader("debugHost"); | 
|             if(StringUtils.isNotBlank(debugHost)){ | 
|                 host=debugHost; | 
|                 WebUtil.getSession().removeAttribute(ATTR_COMPANY); | 
|                 LogUtil.debug("debugHost={}",host); | 
|             } | 
|         } | 
|         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; | 
|     } | 
|   | 
|   | 
|   | 
|   | 
|     /** | 
|      * 获取当前域名对于的公司ID | 
|      * @return | 
|      */ | 
|     public static Long getCompanyId() { | 
|         return ((SysCompany) WebUtil.getSessionAttribute(ATTR_COMPANY)).getComId(); | 
|     } | 
|   | 
| } |