转载自:https://blog.csdn.net/hanghangde/article/details/51298221
侵删
最近做项目遇到一个Filter需要配置多个url-pattern,上网查了下资料,经测试,现总结下
一、完全错误的方式
- <filter>
- <filter-name>authority</filter-name>
- <filter-class>com.util.AuthorityFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>authority</filter-name>
- <url-pattern>/pages/cmm/*;/pages/genbill/*</url-pattern>
- </filter-mapping>
二、有一定作用,但不能实现需要的效果,只会过滤最下面配置的url-pattern。
- <filter>
- <filter-name>authority</filter-name>
- <filter-class>com.util.AuthorityFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>authority</filter-name>
- <url-pattern>/pages/cmm/*</url-pattern>
- <url-pattern>/pages/genbill/*</url-pattern>
- </filter-mapping>
三、现在给出正确的配置方式
- <filter>
- <filter-name>authority</filter-name>
- <filter-class>com.util.AuthorityFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>authority</filter-name>
- <url-pattern>/pages/genbill/*</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>authority</filter-name>
- <url-pattern>/pages/cmm/*</url-pattern>
- </filter-mapping>
在web.xml声明的一个filter中:
<!– session过滤filter –>
- <filter>
- <filter-name>SessionFilter</filter-name>
- <filter-class>
- com.iqbon.jcms.web.util.SessionFilter
- </filter-class>
- <init-param>
- <param-name>excludedPages</param-name>
- <param-value>/admin/login.do</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>SessionFilter</filter-name>
- <url-pattern>/admin/*</url-pattern>
- </filter-mapping>
可以看到url-pattern的设置里面过滤的url规则是/admin/*,如果要把/admin/login.do排除在过滤url之外。
可以结合init-param的初始化参数和HttpServletRequest的getServletPath()方法来判断。
- <init-param>
- <param-name>excludedPages</param-name>
- <param-value>/admin/login.do</param-value>
- </init-param>
其次在filter的java代码中加上判断:
- 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 javax.servlet.http.HttpSession;
- import org.apache.commons.lang3.StringUtils;
- import com.iqbon.jcms.util.KeyConstant;
- /**
- * session过滤器
- */
- public class SessionFilter implements Filter {
- /**
- * 需要排除的页面
- */
- private String excludedPages;
- private String[] excludedPageArray;
- /**
- * @see Filter#destroy()
- */
- public void destroy() {
- return;
- }
- /**
- * 对session进行判断当前访问是否有登录
- */
- public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
- boolean isExcludedPage = false;
- for (String page : excludedPageArray) {//判断是否在过滤url之外
- if(((HttpServletRequest) request).getServletPath().equals(page)){
- isExcludedPage = true;
- break;
- }
- }
- if (isExcludedPage) {//在过滤url之外
- chain.doFilter(request, response);
- } else {//不在过滤url之外,判断session是否存在
- HttpSession session = ((HttpServletRequest) request).getSession();
- if (session == null || session.getAttribute(KeyConstant.SESSION_KEY_USER) == null) {
- ((HttpServletResponse) response).sendRedirect(“/login.htm”);
- } else {
- chain.doFilter(request, response);
- }
- }
- }
- /**
- * 初始化函数,获取需要排除在外的url
- */
- public void init(FilterConfig fConfig) throws ServletException {
- excludedPages = fConfig.getInitParameter(“excludedPages”);
- if (StringUtils.isNotEmpty(excludedPages)) {
- excludedPageArray = excludedPages.split(“,”);
- }
- return;
- }
- }
最近做项目遇到一个Filter需要配置多个url-pattern,上网查了下资料,经测试,现总结下
一、完全错误的方式
- <filter>
- <filter-name>authority</filter-name>
- <filter-class>com.util.AuthorityFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>authority</filter-name>
- <url-pattern>/pages/cmm/*;/pages/genbill/*</url-pattern>
- </filter-mapping>
二、有一定作用,但不能实现需要的效果,只会过滤最下面配置的url-pattern。
- <filter>
- <filter-name>authority</filter-name>
- <filter-class>com.util.AuthorityFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>authority</filter-name>
- <url-pattern>/pages/cmm/*</url-pattern>
- <url-pattern>/pages/genbill/*</url-pattern>
- </filter-mapping>
三、现在给出正确的配置方式
- <filter>
- <filter-name>authority</filter-name>
- <filter-class>com.util.AuthorityFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>authority</filter-name>
- <url-pattern>/pages/genbill/*</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>authority</filter-name>
- <url-pattern>/pages/cmm/*</url-pattern>
- </filter-mapping>
在web.xml声明的一个filter中:
<!– session过滤filter –>
- <filter>
- <filter-name>SessionFilter</filter-name>
- <filter-class>
- com.iqbon.jcms.web.util.SessionFilter
- </filter-class>
- <init-param>
- <param-name>excludedPages</param-name>
- <param-value>/admin/login.do</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>SessionFilter</filter-name>
- <url-pattern>/admin/*</url-pattern>
- </filter-mapping>
可以看到url-pattern的设置里面过滤的url规则是/admin/*,如果要把/admin/login.do排除在过滤url之外。
可以结合init-param的初始化参数和HttpServletRequest的getServletPath()方法来判断。
- <init-param>
- <param-name>excludedPages</param-name>
- <param-value>/admin/login.do</param-value>
- </init-param>
其次在filter的java代码中加上判断:
- 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 javax.servlet.http.HttpSession;
- import org.apache.commons.lang3.StringUtils;
- import com.iqbon.jcms.util.KeyConstant;
- /**
- * session过滤器
- */
- public class SessionFilter implements Filter {
- /**
- * 需要排除的页面
- */
- private String excludedPages;
- private String[] excludedPageArray;
- /**
- * @see Filter#destroy()
- */
- public void destroy() {
- return;
- }
- /**
- * 对session进行判断当前访问是否有登录
- */
- public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
- boolean isExcludedPage = false;
- for (String page : excludedPageArray) {//判断是否在过滤url之外
- if(((HttpServletRequest) request).getServletPath().equals(page)){
- isExcludedPage = true;
- break;
- }
- }
- if (isExcludedPage) {//在过滤url之外
- chain.doFilter(request, response);
- } else {//不在过滤url之外,判断session是否存在
- HttpSession session = ((HttpServletRequest) request).getSession();
- if (session == null || session.getAttribute(KeyConstant.SESSION_KEY_USER) == null) {
- ((HttpServletResponse) response).sendRedirect(“/login.htm”);
- } else {
- chain.doFilter(request, response);
- }
- }
- }
- /**
- * 初始化函数,获取需要排除在外的url
- */
- public void init(FilterConfig fConfig) throws ServletException {
- excludedPages = fConfig.getInitParameter(“excludedPages”);
- if (StringUtils.isNotEmpty(excludedPages)) {
- excludedPageArray = excludedPages.split(“,”);
- }
- return;
- }
- }