日期:2014-05-18  浏览次数:20894 次

过滤器如何使用
请问:
          我有个后台,但是我后台的权限是要先管理员登陆,如果没有的话,过滤器要如何拦截后台的其他管理员操作路径,并且保证没有Bug   .
         


------解决方案--------------------
web.xml
<filter>
<filter-name> adminfilter </filter-name>
<filter-class> xxx.xxx </filter-class> <!--对应后台过滤器类-->
</filter>
<filter-mapping>
<filter-name> adminfilter </filter-name>
<url-pattern> /admin/*.* </url-pattern> <!--admin是存放管理员页面的目录-->
</filter-mapping>

后台过滤器
public class LoginSession implements Filter {

public void init(FilterConfig arg0) throws ServletException {

}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//身份验证,防止盗链
HttpServletRequest myRequest = (HttpServletRequest)request;
HttpSession mySession = myRequest.getSession();
if(mySession.getAttribute( "myUserVO ")==null){
PrintWriter myWriter = response.getWriter();
myWriter.print( " <script> parent.location.href= '/AssetManageG4/login/login.jsp ' </script> ");
}
chain.doFilter(request,response);
}

public void destroy() {

}
}