侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

java 访问子域名设置

2024-05-07 星期二 / 0 评论 / 0 点赞 / 57 阅读 / 3761 字

1 nds 泛域名解析 :CNAME*默认package cn.com.platform.filter;import java.io.IOException;import javax.ser

1 nds 泛域名解析 :CNAME * 默认

package cn.com.platform.filter;import java.io.IOException;import javax.servlet.FilterChain;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.lang3.StringUtils;import org.springframework.stereotype.Component;import org.springframework.web.filter.OncePerRequestFilter;/** *  *  * [@author](http://my.oschina.net/arthor) linwk 2016年8月1日 *  */@Componentpublic class DomainFielter extends OncePerRequestFilter {	private static final String DOMAIN = "xxx.com";	private static final String SEP = "/";	// 一些不需要配置的二级域名 前后都加逗号 直接字符判断不用转成数组	private static final String EXCEPT_DOMAIN = ",manage,test,";	@Override	public void destroy() {		// TODO Auto-generated method stub	}	/**	 * 域名管理设置cname *指向根域名 C:/WINDOWS/system32/drivers/etc/hosts文件中增加 127.0.0.1	 * xxx.com index.xxx.com jc.xxx.com test.xxx.com	 */	@Override	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {		String domainName = "";		String serverName = request.getServerName();		String path = request.getRequestURI();		int end = serverName.indexOf(DOMAIN);		if (end != -1 && end != 0) {			domainName = serverName.substring(0, end - 1);		} else {			domainName = "www";		}				// 是资源的数据,不进行域名转换 或者 在EXCEPT_DOMAIN里面的不需要转换		if (StringUtils.indexOf(path, "assets") > -1 || StringUtils.indexOf(EXCEPT_DOMAIN, "," + domainName + ",") > -1) {			filterChain.doFilter(request, response);			return;		}		// http://username.test.com/article/1633		String distPage = getDistPage(path);		String id = getId(path);		if (domainName != null && !"".equals(domainName) && !"www".equals(domainName)) {			// http://blog.test.com/article/1633 ==>			// http://www.test.com/blog/article.htm?id=1633			if (id != null && !id.equals("")) {				request.getRequestDispatcher(SEP + domainName + SEP + distPage + ".htm" + "?id=" + id).forward(request, response);				return;			} else {				// http://blog.test.com ==> http://www.test.com/blog/				request.getRequestDispatcher(SEP + domainName + SEP).forward(request, response);				return;			}		}		filterChain.doFilter(request, response);	}	private static String getDistPage(String path) {		if (path == null && "".equals(path)) {			return null;		}		String args[] = path.split("/");		if (args.length < 3) {			return null;		}		return args[1];	}	private static String getId(String path) {		if (path == null && "".equals(path)) {			return null;		}		String args[] = path.split("/");		if (args.length < 3) {			return null;		}		return args[2];	}	public static void main(String[] args) {		System.out.println(DomainFielter.getId("/article/1533"));	}}

配置:web.xml<!-- 域名Filter 定义 --><filter><filter-name>domainFielter</filter-name><filter-class>cn.com.xxx.filter.DomainFielter</filter-class><init-param><param-name>targetBeanName</param-name><param-value>domainFielter</param-value></init-param></filter><filter-mapping><filter-name>domainFielter</filter-name><url-pattern>/*</url-pattern></filter-mapping>

效果:访问http://jc.xxx.com/ 等同于http://www.xxx.com/jc

广告 广告

评论区