CAS 界面根据不同的域名显示不同的界面
Posted yg_zhang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CAS 界面根据不同的域名显示不同的界面相关的知识,希望对你有一定的参考价值。
概要
在实际需求中,客户想通过不同的域名显示不同的登录界面,比如输入 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 界面根据不同的域名显示不同的界面的主要内容,如果未能解决你的问题,请参考以下文章
如何根据不同设备类型快速地实现网页界面的响应式布局,只需一句代码立马实现。