java Filter过滤器例外URL设置

在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;     
}     
}   

http://blog.csdn.net/Error_case/article/details/41946449

  1. import java.io.IOException;     
  2.   
  3. import javax.servlet.Filter;     
  4. import javax.servlet.FilterChain;     
  5. import javax.servlet.FilterConfig;     
  6. import javax.servlet.ServletException;     
  7. import javax.servlet.ServletRequest;     
  8. import javax.servlet.ServletResponse;     
  9. import javax.servlet.http.HttpServletRequest;     
  10. import javax.servlet.http.HttpServletResponse;     
  11. import javax.servlet.http.HttpSession;     
  12. import org.apache.commons.lang3.StringUtils;     
  13. import com.iqbon.jcms.util.KeyConstant;     
  14.     
  15. /**   
  16. * session过滤器   
  17. */    
  18.   
  19. public class SessionFilter implements Filter {     
  20.   
  21. /**   
  22. * 需要排除的页面   
  23. */    
  24.   
  25. private String excludedPages;       
  26. private String[] excludedPageArray;     
  27.   
  28. /**   
  29. * @see Filter#destroy()   
  30. */    
  31.   
  32. public void destroy() {     
  33. return;     
  34. }     
  35.     
  36. /**   
  37. * 对session进行判断当前访问是否有登录   
  38. */    
  39.   
  40. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {     
  41. boolean isExcludedPage = false;     
  42. for (String page : excludedPageArray) {//判断是否在过滤url之外     
  43. if(((HttpServletRequest) request).getServletPath().equals(page)){     
  44. isExcludedPage = true;     
  45. break;     
  46. }     
  47. }     
  48.   
  49. if (isExcludedPage) {//在过滤url之外     
  50. chain.doFilter(request, response);     
  51. else {//不在过滤url之外,判断session是否存在     
  52. HttpSession session = ((HttpServletRequest) request).getSession();     
  53. if (session == null || session.getAttribute(KeyConstant.SESSION_KEY_USER) == null) {     
  54. ((HttpServletResponse) response).sendRedirect(“/login.htm”);     
  55. else {     
  56. chain.doFilter(request, response);     
  57. }     
  58. }     
  59. }     
  60.   
  61. /**   
  62. * 初始化函数,获取需要排除在外的url   
  63. */    
  64.   
  65. public void init(FilterConfig fConfig) throws ServletException {     
  66. excludedPages = fConfig.getInitParameter(“excludedPages”);     
  67. if (StringUtils.isNotEmpty(excludedPages)) {     
  68. excludedPageArray = excludedPages.split(“,”);     
  69. }     
  70. return;     
  71. }     
  72. }   
posted @ 2016-12-07 17:34  北斗极星  阅读(37504)  评论(1编辑  收藏  举报