Java tutorial
/** * Copyright (c) 2005-2012 https://github.com/zhangkaitao * * Licensed under the Apache License, Version 2.0 (the "License"); */ package cn.guoyukun.spring.web.interceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.AntPathMatcher; import org.springframework.util.PathMatcher; import org.springframework.util.StringUtils; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import cn.guoyukun.spring.jpa.Constants; /** * ?Interceptor * <p/> * Filter getParameternull ?Interceptor * <p/> * 1?ctx---->request.contextPath * 2?currentURL---->?? * <p>User: * <p>Date: 13-1-22 ?4:35 * <p>Version: 1.0 */ public class SetCommonDataInterceptor extends HandlerInterceptorAdapter { // @SuppressWarnings("unused") private static final Logger LOG = LoggerFactory.getLogger(SetCommonDataInterceptor.class); private final PathMatcher pathMatcher = new AntPathMatcher(); private static final String[] DEFAULT_EXCLUDE_PARAMETER_PATTERN = new String[] { "\\&\\w*page.pn=\\d+", "\\?\\w*page.pn=\\d+", "\\&\\w*page.size=\\d+" }; private String[] excludeParameterPatterns = DEFAULT_EXCLUDE_PARAMETER_PATTERN; private String[] excludeUrlPatterns = null; public void setExcludeParameterPatterns(String[] excludeParameterPatterns) { this.excludeParameterPatterns = excludeParameterPatterns; } public void setExcludeUrlPatterns(final String[] excludeUrlPatterns) { this.excludeUrlPatterns = excludeUrlPatterns; } @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (isExclude(request)) { return true; } if (request.getAttribute(Constants.CONTEXT_PATH) == null) { request.setAttribute(Constants.CONTEXT_PATH, request.getContextPath()); } if (request.getAttribute(Constants.CURRENT_URL) == null) { request.setAttribute(Constants.CURRENT_URL, extractCurrentURL(request, true)); } if (request.getAttribute(Constants.NO_QUERYSTRING_CURRENT_URL) == null) { request.setAttribute(Constants.NO_QUERYSTRING_CURRENT_URL, extractCurrentURL(request, false)); } if (request.getAttribute(Constants.BACK_URL) == null) { request.setAttribute(Constants.BACK_URL, extractBackURL(request)); } return true; } private boolean isExclude(final HttpServletRequest request) { if (excludeUrlPatterns == null) { return false; } for (String pattern : excludeUrlPatterns) { if (pathMatcher.match(pattern, request.getServletPath())) { return true; } } return false; } private String extractCurrentURL(HttpServletRequest request, boolean needQueryString) { String url = request.getRequestURI(); String queryString = request.getQueryString(); if (!StringUtils.isEmpty(queryString)) { queryString = "?" + queryString; for (String pattern : excludeParameterPatterns) { queryString = queryString.replaceAll(pattern, ""); } if (queryString.startsWith("&")) { queryString = "?" + queryString.substring(1); } } if (!StringUtils.isEmpty(queryString) && needQueryString) { url = url + queryString; } return getBasePath(request) + url; } /** * ? * 1?request.parameterBackURL * 2??header referer * * @param request * @return */ private String extractBackURL(HttpServletRequest request) { String url = request.getParameter(Constants.BACK_URL); //Filter getParameternull ?Interceptor if (StringUtils.isEmpty(url)) { url = request.getHeader("Referer"); } if (!StringUtils.isEmpty(url) && (url.startsWith("http://") || url.startsWith("https://"))) { return url; } if (!StringUtils.isEmpty(url) && url.startsWith(request.getContextPath())) { url = getBasePath(request) + url; } LOG.info("BACKURL:{}", url); return url; } private String getBasePath(HttpServletRequest req) { StringBuffer baseUrl = new StringBuffer(); String scheme = req.getScheme(); int port = req.getServerPort(); //String servletPath = req.getServletPath (); //String pathInfo = req.getPathInfo (); baseUrl.append(scheme); // http, https baseUrl.append("://"); baseUrl.append(req.getServerName()); if ((scheme.equals("http") && port != 80) || (scheme.equals("https") && port != 443)) { baseUrl.append(':'); baseUrl.append(req.getServerPort()); } return baseUrl.toString(); } }