Servlet实现Filter过滤拦截

xiaoxiao2021-02-27  230

经常会用到拦截过滤这一功能,比如有时,我们不希望用户没有进行登录访问后台的操作页面,而且这样的非法访问会让系统极为的不安全,所以我们常常需要进行登录才授权访问其它页面,否则只会出现登录页面。 实现思路:(1)在jsp页面进行session的判断,如果不存在该用户的session,就跳转到登录页面,否则执行jsp页面代码,但是你会发现这样做逻辑也简单,但是非常麻烦,如果有很多个jsp,那么就要写多个判断;(2)使用Filter过滤器,访问页面时都进行过滤验证,如果存在该用户session,则访问该页面,否则跳转到登陆页面登录,保存session后访问其它页面。

代码实现:

public class LoginFilter implements Filter { public static final String login_page="/test/admin/index.jsp"; public static final String logout_page = "jsp/index.html"; public void destroy() { // TODO Auto-generated method stub } public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)arg0; HttpServletResponse response = (HttpServletResponse)arg1; String currentURL=request.getRequestURI(); String ctxPath = request.getContextPath(); //除掉项目名称时访问页面当前路径 String targetURL = currentURL.substring(ctxPath.length()); HttpSession session = request.getSession(false); //对当前页面进行判断,如果当前页面不为登录页面 if(!("/jsp/index.html".equals(targetURL))){ System.out.println("1"+targetURL+"ctxPath:"+ctxPath+"currentURL:"+currentURL); //在不为登陆页面时,再进行判断,如果不是登陆页面也没有session则跳转到登录页面, if(session == null || session.getAttribute("admin") == null){ response.sendRedirect(logout_page); return; }else{ //这里表示正确,会去寻找下一个链,如果不存在,则进行正常的页面跳转 arg2.doFilter(request, response); return; } }else{ //这里表示如果当前页面是登陆页面,跳转到登陆页面 System.out.println("正常过滤!!!进入、jsp/index.html页面!!!"); arg2.doFilter(request,response); return; } } public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub }`` [html] view plain copy <filter> <filter-name>LoginFilter</filter-name> <filter-class>com.test.filter.LoginFilter</filter-class> </filter> <filter-mapping> <filter-name>LoginFilter</filter-name> //这里表示对所有的以jsp后缀的文件有效,其它的无效 <url-pattern>*.jsp</url-pattern> </filter-mapping>

这样功能就可以实现了。

但是在实际验证的过程中发现很多问题:

Filter的url-pattern问题

我自己试着写成/jsp/*.jsp,想试着过滤所有该目录下所有的所有jsp文件,但是报了以下的error:

Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/day09]] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154) ... 6 more Caused by: java.lang.IllegalArgumentException: Invalid <url-pattern> /jsp/*.html in filter mapping at org.apache.catalina.core.StandardContext.validateFilterMap(StandardContext.java:3090) at org.apache.catalina.core.StandardContext.addFilterMap(StandardContext.java:3039) at org.apache.catalina.deploy.WebXml.configureContext(WebXml.java:1265) at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1341) at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:873) at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:371) at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117) at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5355) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ... 6 more

研究了一下servlet的基础知识,发现

<filter-mapping> <filter-name> firstfilter </filter-name> <url-pattern> admin/*.jsp </url-pattern> </filter-mapping>

这里不能用admin/.jsp 这个代表这个目录下的所有jsp页面 ,只能写成 admin/*或者 *.jsp 而且 前面不能加目录,可以是servlet 多级url-mapping,只能是这样的形式,这是硬性的规定。

Servlet中转发和重定向的路径问题以及表单提交路径问题

请求转发与响应重定向的种类

有两种方式获得Servlet转发对象(RequestDispatcher):一种是通过HttpServletRequest的getRequestDispatcher()方法获得,一种是通过ServletContext的getRequestDispatcher()方法获得。Servlet 重定向的方法只有一种:HttpServletResponse的sendRedirect()方法。这三个方法的参数都是一个URL形式的字符串,但在使用相对路径或绝对路径上有所区别。

请求转发与响应重定向中路径参数区别

假设通过http://localhost/project_name/cool/bar.do 请求到达该方法所属的Servlet。 (1) 响应重定向HttpServletResponse.sendRedirect(String) 参数可以指定为相对路径、绝对路径或其它Web应用。

相对路径:response.sendRedirect(“foo/stuff.do”),容器相对于原来请求URL的目录加参数来生成完整的URL——http://localhost/project_name/cool/foo/stuff.do。绝对路径:response.sendRedirect(“/foo/stuff.do”),容器相对于Web应用本身加参数建立完整的URL,这是因为 重定向response.sendRedirect(“”)是服务器向客户端发送一个请求头信息,由客户端再请求一次服务器,请求是在服务器外进行的,即完整的url是——http://localhost/foo/stuff.do。其它Web应用:response.sendRedirect(“http://www.xxx.com “)容器直接定向到该URL。

(2) 请求转发 ◆HttpServletRequest.getRequestDispatcher(String) 参数可以指定为相对路径或绝对路径。

相对路径情况下生成的完整URL与重定向方法相同。绝对路径与Servlet重定向不同,容器将相对于Web应用的根目录加参数生成完整的URL(即“/”根路径就是相对于虚拟路径)这是因为转发是在服务器内部进行的,写绝对路径/开头指的是当前的Web应用程序。即: request.getRequestDispatcher(“/foo/stuff.do”)生成的URL是 http://localhost/myApp/foo/stuff.do 。

(3) ServletContext.getRequestDispatcher(String)参数只能指定为绝对路径, 生成的完整URL:HttpServletRequest.getRequestDispatcher(String)相同。

JSP 提交表单给 Servlet 路径问题

JSP页面提交表单给Servlet时,路径的写法要格外注意。 例如在web.xml中注册如下的servlet:

<servlet> <servlet-name>addStudent</servlet-name> <servlet-class>org.mytest.addStudent</servlet-class> </servlet> <servlet-mapping> <servlet-name>addStudent</servlet-name> <url-pattern>/servlet/addStudent</url-pattern> </servlet-mapping>

假如说,你工程名字为HibernateApp3,JSP页面提交表单给servlet时有两种写法: 1.相对路径: <form action=servlet/addStudent method=post>...</form> 2. 绝对路径: <form action="/HibernateApp3/servlet/addStudent" method=post>...</form> 或者

注意:代表根目录,如果路径是使用/开头,Tomcat就是webApp那个目录,如果你不是/开头代表你从当前工程的目录开始,例如:webApp/project_name/ 这一点非常重要,很多提交表单时发生的错误都是因为提交路径出错造成的。<a href>的路径如果是”/”开头,则表示相对于主机,如果不是则表示相对于当前请求。综上所述:这里最最关键的要能清楚发出请求目的资源的请求是在服务器内部还是服务器外部:内部时,“/”就是项目的虚拟目录;外部时,“/”就是代表主机的根目录。

内部转发forward和include不会被过滤

Servlet2.4默认只拦截外部提交的请求,forward和include这些内部转发不会被filter过滤。

转载请注明原文地址: https://www.6miu.com/read-10796.html

最新回复(0)