码迷,mamicode.com
首页 > 其他好文 > 详细

CAS 界面根据不同的域名显示不同的界面

时间:2018-12-13 19:36:40      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:current   映射   pos   ping   tps   sse   efi   cWeb   mis   

 

 

概要

在实际需求中,客户想通过不同的域名显示不同的登录界面,比如输入 manage.aps.cn 显示运维管理登录,business.aps.cn 显示业务管理登录。

实现方法

1.准备两套登录UI

在cas目录 WEB-INF/view/jsp目录下整两份UI。

 技术分享图片

修改属性文件

技术分享图片

查看一下 business.properties 

技术分享图片

 

 cas 默认显示界面是在cas.properties 文件中配置,配置完成后,不能在变更了。

技术分享图片

 

2.代码实现

我的思路是通过根据域名动态改变 这个basename。在过滤器中获取域名,并动态改变basename。

修改源码:

org.springframework.web.servlet.view.ResourceBundleViewResolver

package org.springframework.web.servlet.view;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.web.servlet.View;

public class ResourceBundleViewResolver extends AbstractCachingViewResolver
  implements Ordered, InitializingBean, DisposableBean
{
  public static final String DEFAULT_BASENAME = "views";
  private int order = 2147483647;

  private  ThreadLocal<String>  baseNameLocal= new ThreadLocal<>();

  private ClassLoader bundleClassLoader = Thread.currentThread().getContextClassLoader();
  private String defaultParentView;
  private Locale[] localesToInitialize;
  private final Map<Locale, BeanFactory> localeCache = new HashMap();

  private final Map<List<ResourceBundle>, ConfigurableApplicationContext> bundleCache = new HashMap();

  public void setOrder(int order)
  {
    this.order = order;
    
  }

  public int getOrder() {
    return this.order;
  }

  public void setBasename(String basename)
  {
    this.baseNameLocal.set(basename);
  }

  

  public void setBundleClassLoader(ClassLoader classLoader)
  {
    this.bundleClassLoader = classLoader;
  }

  protected ClassLoader getBundleClassLoader()
  {
    return this.bundleClassLoader;
  }

  public void setDefaultParentView(String defaultParentView)
  {
    this.defaultParentView = defaultParentView;
  }

  public void setLocalesToInitialize(Locale[] localesToInitialize)
  {
    this.localesToInitialize = localesToInitialize;
  }

  public void afterPropertiesSet()
    throws BeansException
  {
    if (this.localesToInitialize != null)
      for (Locale locale : this.localesToInitialize)
        initFactory(locale);
  }

  protected View loadView(String viewName, Locale locale)
    throws Exception
  {
    BeanFactory factory = initFactory(locale);
    try {
      return (View)factory.getBean(viewName, View.class);
    }
    catch (NoSuchBeanDefinitionException ex) {
    }
    return null;
  }

  protected synchronized BeanFactory initFactory(Locale locale)
    throws BeansException
  {
    
    List<ResourceBundle> bundles = new LinkedList();
    
    String baseName="manage";
    
    if(this.baseNameLocal.get()!=null){
        baseName=this.baseNameLocal.get();
    }
    
    ResourceBundle bundle = getBundle(baseName, locale);
    bundles.add(bundle);

    GenericWebApplicationContext factory = new GenericWebApplicationContext();
    factory.setParent(getApplicationContext());
    factory.setServletContext(getServletContext());

    PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(factory);
    reader.setDefaultParentBean(this.defaultParentView);
    reader.registerBeanDefinitions(bundle);

    factory.refresh();



    return factory;
  }

  protected ResourceBundle getBundle(String basename, Locale locale)
    throws MissingResourceException
  {
    return ResourceBundle.getBundle(basename, locale, getBundleClassLoader());
  }

  public void destroy()
    throws BeansException
  {
    for (ConfigurableApplicationContext factory : this.bundleCache.values()) {
      factory.close();
    }
    this.localeCache.clear();
    this.bundleCache.clear();
  }
}

增加一个过滤器,根据域名修改 页面视图目录。

package com.redxun.cas;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.servlet.view.ResourceBundleViewResolver;

public class ViewSelectFilter implements Filter{

    @Override
    public void destroy() {
        
    }
    

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest)req;
        ResourceBundleViewResolver resolver=(ResourceBundleViewResolver) SpringContextHolder.getBean("viewResolver");
        String serverName=request.getServerName();
        String pre=serverName.substring(0,serverName.indexOf("."));
        resolver.setBasename(pre);

        chain.doFilter(req, res);
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        
    }

}

修改 cas-servlet.xml配置。

<bean id="viewResolver"  class="org.springframework.web.servlet.view.ResourceBundleViewResolver"
        p:order="0">
    <property name="basename" value="${cas.viewResolver.basename}" />
    <property name="cache" value="false"></property>
  </bean>

配置过滤器

<filter>
       <filter-name>ViewSelectFilter</filter-name>
       <filter-class>com.redxun.cas.ViewSelectFilter</filter-class>
   </filter>
  
  <filter-mapping>
        <filter-name>ViewSelectFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

测试效果

在host添加域名映射

192.168.1.77 manage.aps.cn
192.168.1.77 business.aps.cn

测试效果如下:

访问:manage.aps.cn 

技术分享图片

business.aps.cn

技术分享图片

 

CAS 界面根据不同的域名显示不同的界面

标签:current   映射   pos   ping   tps   sse   efi   cWeb   mis   

原文地址:https://www.cnblogs.com/yg_zhang/p/10115346.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!