struts2自定义Filter

在struts2中自定义filter 防止用户通过手动输入网址进行相应操作
SessionFilter.java
public class SessionFilter implements Filter {
	
	public FilterConfig config;
	
	public void destroy() {
		// TODO Auto-generated method stub
		this.config=null;
	}
	public static boolean isContains(String container, String[] regx) {  
        boolean result = false;  
  
        for (int i = 0; i < regx.length; i++) {  
            if (container.indexOf(regx[i]) != -1) {  
                return true;  
            }  
        }  
        return result;  
    }  
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {  
        HttpServletRequest hrequest = (HttpServletRequest)request;  
        HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper((HttpServletResponse) response);  
          
        String logonStrings = config.getInitParameter("logonStrings");         
        String includeStrings = config.getInitParameter("includeStrings");      
        String redirectPath = hrequest.getContextPath() + config.getInitParameter("redirectPath"); 
        String disabletestfilter = config.getInitParameter("disabletestfilter");
          
        if (disabletestfilter.toUpperCase().equals("Y")) {    
            chain.doFilter(request, response);  
            return;  
        }  
        String[] logonList = logonStrings.split(";");  
        String[] includeList = includeStrings.split(";");  
          
        if (!this.isContains(hrequest.getRequestURI(), includeList)) {
            chain.doFilter(request, response);  
            return;  
        }  
          
        if (this.isContains(hrequest.getRequestURI(), logonList)) {
            chain.doFilter(request, response);  
            return;  
        }  
          
        String user = ( String ) hrequest.getSession().getAttribute("name");
        if (user == null) {  
            wrapper.sendRedirect(redirectPath);  
            return;  
        }else {  
            chain.doFilter(request, response);  
            return;  
        }  
    }  
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		config = filterConfig;  
	}

}
在web.xml中的配置
<filter>  
	    <filter-name>SessionFilter</filter-name>  
	    <filter-class>filter.SessionFilter</filter-class>  
	    <init-param>  
	        <param-name>logonStrings</param-name><!-- 对登录页面不进行过滤 -->  
	        <param-value>/project/index.jsp;login.action</param-value>  
	    </init-param>  
	    <init-param>  
	        <param-name>includeStrings</param-name><!-- 只对指定过滤参数后缀进行过滤 -->  
	        <param-value>.action;.jsp</param-value>  
	    </init-param>  
	    <init-param>  
	        <param-name>redirectPath</param-name><!-- 未通过跳转到登录界面 -->  
	        <param-value>/</param-value>  
	    </init-param>  
	    <init-param>  
	        <param-name>disabletestfilter</param-name><!-- Y:过滤无效 -->  
	        <param-value>N</param-value>  
	    </init-param>  
	</filter>  
	<filter-mapping>  
	    <filter-name>SessionFilter</filter-name>  
	    <url-pattern>/*</url-pattern>  
	</filter-mapping>
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值