struts2过滤用户非法登陆

1、过滤用户非法登录(LoginFilter.java)

  1. <SPAN style="FONT-SIZE: 18px">package comm;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import javax.servlet.Filter;  
  6. import javax.servlet.FilterChain;  
  7. import javax.servlet.FilterConfig;  
  8. import javax.servlet.ServletException;  
  9. import javax.servlet.ServletRequest;  
  10. import javax.servlet.ServletResponse;  
  11. import javax.servlet.http.HttpServlet;  
  12. import javax.servlet.http.HttpServletRequest;  
  13. import javax.servlet.http.HttpServletResponse;  
  14. import javax.servlet.http.HttpSession;  
  15.   
  16. /** 
  17.  *过滤用户非法登录(过滤jsp页面) 
  18.  */  
  19. public class LoginFilter extends HttpServlet implements Filter {  
  20.     public void destroy() {  
  21.     }  
  22.   
  23.     public void doFilter(ServletRequest sRequest, ServletResponse sResponse,  
  24.             FilterChain filterChain) throws IOException, ServletException {  
  25.   
  26.         HttpServletRequest request = (HttpServletRequest) sRequest;  
  27.         HttpServletResponse response = (HttpServletResponse) sResponse;  
  28.         HttpSession session = request.getSession();  
  29.   
  30.         String contextPath = request.getContextPath();  
  31.         String url = request.getRequestURI();  
  32.   
  33.         String user = (String) session.getAttribute("userName");  
  34.   
  35.         // 非登录页面或非登录验证页面,则非法过滤   
  36.         if (url.indexOf("login.jsp") < 0  
  37.                 && url.indexOf("loginAction!check") < 0 && user == null) {// 转入登陆页面   
  38.   
  39.             // 解决:加了过滤器后,CSS不能显示问题   
  40.             if (url.indexOf(".jpg") > 0 || url.indexOf(".bmp") > 0  
  41.                     || url.indexOf(".gif") > 0 || url.indexOf(".css") > 0  
  42.                     || url.indexOf(".png") > 0) {  
  43.                 filterChain.doFilter(sRequest, sResponse);  
  44.                 return;  
  45.             }  
  46.   
  47.             response.sendRedirect(contextPath + "/login.jsp");  
  48.         } else {  
  49.             filterChain.doFilter(sRequest, sResponse);  
  50.         }  
  51.     }  
  52.   
  53.     public void init(FilterConfig arg0) throws ServletException {  
  54.   
  55.     }  
  56. }</SPAN>  
package comm;

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.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *过滤用户非法登录(过滤jsp页面)
 */
public class LoginFilter extends HttpServlet implements Filter {
	public void destroy() {
	}

	public void doFilter(ServletRequest sRequest, ServletResponse sResponse,
			FilterChain filterChain) throws IOException, ServletException {

		HttpServletRequest request = (HttpServletRequest) sRequest;
		HttpServletResponse response = (HttpServletResponse) sResponse;
		HttpSession session = request.getSession();

		String contextPath = request.getContextPath();
		String url = request.getRequestURI();

		String user = (String) session.getAttribute("userName");

		// 非登录页面或非登录验证页面,则非法过滤
		if (url.indexOf("login.jsp") < 0
				&& url.indexOf("loginAction!check") < 0 && user == null) {// 转入登陆页面

			// 解决:加了过滤器后,CSS不能显示问题
			if (url.indexOf(".jpg") > 0 || url.indexOf(".bmp") > 0
					|| url.indexOf(".gif") > 0 || url.indexOf(".css") > 0
					|| url.indexOf(".png") > 0) {
				filterChain.doFilter(sRequest, sResponse);
				return;
			}

			response.sendRedirect(contextPath + "/login.jsp");
		} else {
			filterChain.doFilter(sRequest, sResponse);
		}
	}

	public void init(FilterConfig arg0) throws ServletException {

	}
}


2、web.xml配置

  1. <SPAN style="FONT-SIZE: 18px"><?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.   <welcome-file-list>  
  8.       <welcome-file>pages/user/index.jsp</welcome-file>  
  9.   </welcome-file-list>  
  10.     
  11.     <!-- 过滤用户非法登录 -->  
  12.     <filter>       
  13.          <filter-name>loginFilter</filter-name>       
  14.          <filter-class>comm.LoginFilter</filter-class>       
  15.     </filter>       
  16.     <filter-mapping>    
  17.          <filter-name>loginFilter</filter-name>    
  18.          <url-pattern>/*</url-pattern>    
  19.     </filter-mapping>  
  20.     
  21.   <filter>  
  22.       <filter-name>struts2</filter-name>  
  23.       <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  24.   </filter>  
  25.   <filter-mapping>  
  26.       <filter-name>struts2</filter-name>  
  27.       <url-pattern>/*</url-pattern>  
  28.   </filter-mapping>  
  29. </web-app>  
  30. </SPAN>  
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
      <welcome-file>pages/user/index.jsp</welcome-file>
  </welcome-file-list>
  
    <!-- 过滤用户非法登录 -->
    <filter>     
         <filter-name>loginFilter</filter-name>     
         <filter-class>comm.LoginFilter</filter-class>     
    </filter>     
    <filter-mapping>  
         <filter-name>loginFilter</filter-name>  
         <url-pattern>/*</url-pattern>  
    </filter-mapping>
  
  <filter>
      <filter-name>struts2</filter-name>
  	  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
  	  <filter-name>struts2</filter-name>
  	  <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>


 

出处:http://blog.csdn.net/class_horse/article/details/17456927?reload

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值