package com.hnyl.filter;
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 javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
public class ManagerFilter implements Filter {
Logger logger = Logger.getLogger(ManagerFilter.class);
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
String url = request.getServletPath();
logger.info("------请求URL地址:" + url + "------");
String[] urlAttr = new String[] { "/users/usersPage.do",
"/login/systemOut. do",// 请把所有的后缀都改成你当前项目的后缀
"/login/toLoginPage.do" };
if (!inGroup(urlAttr, url)) {
if (request.getSession().getAttribute("user") == null) {
response.sendRedirect("../login/toLoginPage.do");
return;
}
}
chain.doFilter(servletRequest, servletResponse);
}
private boolean inGroup(String[] so, String tar) {
for (int a = 0; a < so.length; a++) {
if (ifEquals(so[a], tar)) {
return true;
}
}
return false;
}
private boolean ifEquals(String ff, String tt) {
if (ff.endsWith(".do")) {
return ff.equals(tt);
} else {
String temp = tt;
temp = temp.substring(1, temp.length());
String packageName = "/";
if (temp.indexOf("/") >= 0) {
packageName = temp.substring(0, temp.indexOf("/"));
}
return packageName.equals(ff);
}
}
public void destroy() {
}
<session-timeout>30</session-timeout>
</session-config>
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 javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
public class ManagerFilter implements Filter {
Logger logger = Logger.getLogger(ManagerFilter.class);
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
String url = request.getServletPath();
logger.info("------请求URL地址:" + url + "------");
String[] urlAttr = new String[] { "/users/usersPage.do",
"/login/systemOut. do",// 请把所有的后缀都改成你当前项目的后缀
"/login/toLoginPage.do" };
if (!inGroup(urlAttr, url)) {
if (request.getSession().getAttribute("user") == null) {
response.sendRedirect("../login/toLoginPage.do");
return;
}
}
chain.doFilter(servletRequest, servletResponse);
}
private boolean inGroup(String[] so, String tar) {
for (int a = 0; a < so.length; a++) {
if (ifEquals(so[a], tar)) {
return true;
}
}
return false;
}
private boolean ifEquals(String ff, String tt) {
if (ff.endsWith(".do")) {
return ff.equals(tt);
} else {
String temp = tt;
temp = temp.substring(1, temp.length());
String packageName = "/";
if (temp.indexOf("/") >= 0) {
packageName = temp.substring(0, temp.indexOf("/"));
}
return packageName.equals(ff);
}
}
public void destroy() {
}
}
//web
<filter>
<filter-name>managerFilter</filter-name>
<filter-class>com.hnyl.filter.ManagerFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>managerFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<session-timeout>30</session-timeout>
</session-config>