java 访问子域名设置

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日
 * 
 */
@Component
public 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

转载于:https://my.oschina.net/u/1052192/blog/724692

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值