<filter>
<filter-name>sessionFilter</filter-name>
<filter-class>SessionFilter</filter-class>
<init-param>
<param-name>exclusions</param-name>
<param-value>/login*</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>sessionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
private Set<String> excludesPattern;
protected PatternMatcher pathMatcher = new ServletPathMatcher();
public static final String PARAM_NAME_EXCLUSIONS = "exclusions";
public static String contextPath = "";
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpSession session = httpRequest.getSession();
String url = httpRequest.getRequestURI();
if (isExclusion(url)|) {
chain.doFilter(request, response);
return;
}
}
@Override
public void init(FilterConfig config) throws ServletException {
String exclusions = config.getInitParameter(PARAM_NAME_EXCLUSIONS);
if (exclusions != null && exclusions.trim().length() != 0) {
excludesPattern = new HashSet<String>(Arrays.asList(exclusions.split("\\s*,\\s*")));
}
getContextPath(config.getServletContext());
logger.info(contextPath);
}
public boolean isExclusion(String requestURI) {
if (excludesPattern == null) {
return false;
}
if(requestURI.equals(contextPath)){
return true;
}
if (contextPath != null && requestURI.startsWith(contextPath)) {
requestURI = requestURI.substring(contextPath.length());
if (!requestURI.startsWith("/")) {
requestURI = "/" + requestURI;
}
}
for (String pattern : excludesPattern) {
if (pathMatcher.matches(pattern, requestURI)) {
return true;
}
}
return false;
}
public void getContextPath(ServletContext context) {
if (context.getMajorVersion() == 2 && context.getMinorVersion() < 5) {
return;
}
try {
String path = context.getContextPath();
if (path == null || path.length() == 0) {
path = "/";
}
contextPath = path;
} catch (NoSuchMethodError error) {
error.printStackTrace();
}
}