Browse Source

添加拦截器,切换租户tenant信息

demon 8 years ago
parent
commit
16423fc650

+ 11 - 1
src/main/java/com/yihu/hos/ESBApplication.java

@ -1,20 +1,30 @@
package com.yihu.hos;
import com.yihu.hos.interceptor.WebMvcInterceptor;
import com.yihu.hos.web.framework.constant.ServiceFlowConstant;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import javax.jms.Queue;
@SpringBootApplication
public class ESBApplication {
public class ESBApplication extends WebMvcConfigurerAdapter {
    public static void main(String[] args) throws Exception {
        SpringApplication application = new SpringApplication(ESBApplication.class);
        application.run(args);
    }
    // 增加拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new WebMvcInterceptor());
    }
    @Bean
    public Queue queue() {
        return new ActiveMQQueue(ServiceFlowConstant.FLOW_EVENT_QUEUE);

+ 5 - 1
src/main/java/com/yihu/hos/common/CommonPageController.java

@ -1,6 +1,8 @@
package com.yihu.hos.common;
import com.yihu.hos.common.constants.ContextAttributes;
import com.yihu.hos.system.model.SystemUser;
import com.yihu.hos.tenant.model.TenantSession;
import com.yihu.hos.tenant.service.AuthenticateService;
import com.yihu.hos.web.framework.util.controller.BaseController;
import org.apache.commons.io.IOUtils;
@ -114,8 +116,10 @@ public class CommonPageController extends BaseController {
        HttpSession session = request.getSession();
        SystemUser user = (SystemUser) session.getAttribute("userInfo");
        model.addAttribute("userName", user.getUserName());
        TenantSession tenantSession = (TenantSession) session.getAttribute(ContextAttributes.TENANT_SESSION);
        model.addAttribute("userName", user.getUserName());
        model.addAttribute("tenant",tenantSession.getTenant());
        //获取菜单
        String menu = "[{id: 1, text: '任务管理',icon:'${staticRoot}/images/index/menu2_icon.png'},\n" +
                "        {id: 11, pid: 1, text: '任务跟踪', url: '${contextRoot}/datacollect/trackJob',targetType:'1'},\n" +

+ 31 - 0
src/main/java/com/yihu/hos/interceptor/WebMvcInterceptor.java

@ -0,0 +1,31 @@
package com.yihu.hos.interceptor;
import com.yihu.hos.common.constants.ContextAttributes;
import com.yihu.hos.tenant.model.TenantSession;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 *  web 请求拦截
 * @author HZY
 * @vsrsion 1.0
 * Created at 2016/12/26.
 */
public class WebMvcInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        HttpSession session = request.getSession();
        TenantSession tenantSession = (TenantSession) session.getAttribute(ContextAttributes.TENANT_SESSION);
        if (tenantSession!=null) {
            LocalContext.getContext().setAttachment(ContextAttributes.SCHEMA, tenantSession.getSchema());
            LocalContext.getContext().setAttachment(ContextAttributes.TENANT_NAME, tenantSession.getTenant());
        }
        return true;
    }
}

+ 1 - 1
src/main/webapp/WEB-INF/ehr/jsp/common/index.jsp

@ -33,7 +33,7 @@
    <div position="top">
        <div class="m-index-top">
            <div style="float: right;padding-right: 8px">
                <div class="m-index-top-name">欢迎您! ${userName}</div>
                <div class="m-index-top-name">欢迎您! ${userName} 【tenant: ${tenant}】</div>
                <div style="height:40px;">
                    <div id="btnIndexLogout"></div>
                    <div id="btnIndexPassword"></div>