在web.xml中配置后台权限过滤器和struts2

      昨夜遇到这样一个问题:后台没有登陆的时候,直接访问相关/admin/下的页面,转到了admin/forwards.html,而访问xxx.action,程序正常执行,也就是说我写的过滤器没有起作用,重复测试代码很多次确定程序没问题,过滤器是从之前的项目上拿下来的,肯定也是没问题的!因夜半精神不佳,无奈只好先去休息!躺在床上又捋了一遍流程,觉得问题可能在web.xml中的配置这里!

     刚才又重新检查代码证实了昨夜的猜测!下面先看下代码!、

 

1.

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;

/**
 * 
 * @time:          Mar 30, 2009 3:27:41 PM
 * @author:        聂靖宇
 * @QQ:            6241899
 * @MSN:           [email protected]
 * @Email:         [email protected] 
 * @Description:   后台登陆FIlter
 */
public class WebMasterLoginFilter implements Filter{

	public void destroy() {
		
	}

	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest)req;
		HttpServletResponse response = (HttpServletResponse)res;
		HttpSession session = request.getSession();
		String uri = request.getRequestURI();
		//不过滤登录退出
		if((!uri.contains("/login.jsp")) 
				&& (!uri.contains("/admin/login.action")) 
				&& (!uri.contains("/admin/exit.action") 
				&& (!uri.contains("/forwards.html")) 
				&& (!uri.contains("/images/"))  
				&& (!uri.contains("/css/")))){
			if(session.getAttribute("adminInfo") == null){
				response.sendRedirect(request.getContextPath()+"/admin/forwards.html");
				return;
			}
		}
		try{
			chain.doFilter(req, res);
		}catch(IllegalStateException e){
		}
	}

	public void init(FilterConfig arg0) throws ServletException {
		
	}

}

 

2.web.xml配置

	<!-- Struts2 配置 -->
	<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>*.action</url-pattern>
	</filter-mapping>
	<filter-mapping>   
        <filter-name>struts2</filter-name>   
        <url-pattern>*.jsp</url-pattern>   
    </filter-mapping> 
	
	<!-- 后台登录过滤器 开始-->
	<filter>
		<filter-name>WebMasterLoginFilter</filter-name>
		<filter-class>com.buygift.filters.WebMasterLoginFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>WebMasterLoginFilter</filter-name>
		<url-pattern>/admin/*</url-pattern>
	</filter-mapping>
	<!-- 后台登陆过滤器 结束 -->

 

也就是说当有XXX.action的请求到来的时候,先走了struts2的过滤器,所以而我配置的权限过滤器就没执行,修改二者的代码重复测试,程序按照预期的效果执行!即:访问xxx.action转到admin/forwards.html

 

3.

<!-- 后台登录过滤器 开始-->
	<filter>
		<filter-name>WebMasterLoginFilter</filter-name>
		<filter-class>com.buygift.filters.WebMasterLoginFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>WebMasterLoginFilter</filter-name>
		<url-pattern>/admin/*</url-pattern>
	</filter-mapping>
	<!-- 后台登陆过滤器 结束 -->

	<!-- Struts2 配置 -->
	<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>*.action</url-pattern>
	</filter-mapping>
	<filter-mapping>   
        <filter-name>struts2</filter-name>   
        <url-pattern>*.jsp</url-pattern>   
    </filter-mapping> 
	

 

你可能感兴趣的:(Web,xml,jsp,servlet,qq)