| package com.matrix.system.common.interceptor; | 
|   | 
| 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; | 
|   | 
| 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 { | 
|   | 
|         // 获得请求的域名--由小程序直接传过来companyCode参数对应公司的网址 | 
|         String host = request.getHeader("companyCode"); | 
|         LogUtil.debug("当前请求域名{}", host); | 
|         if (StringUtils.isBlank(host)) { | 
|             return false; | 
|         } | 
|   | 
|         Map<String, SysCompany> companyMap = LocalCache.get("companyMap"); | 
|         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(); | 
|     } | 
|   | 
| } |