1 / 17

资源访问与权限控制

☆ WEB 应用开发精品课程. 资源访问与权限控制. 长沙民政职业技术学院. 案例导入. 项目 资源访问权限过滤器的编写与配置 任务 1. 对中文编码进行统一处理; 2. 根据登陆用户的权限控制资源访问。 技术要点 ( 关键字 ) 1. Filter 的编写与配置 2. 实现资源访问权限的统一控制. 目标. Filter 基本概念和适用场合 使用 Filter 的基本步骤 定义和使用 Filter Listener 基础. 过滤器. 请求. 请求. 身份认证 资源审核 资源加密访问. 响应. 响应.

Télécharger la présentation

资源访问与权限控制

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ☆WEB应用开发精品课程 资源访问与权限控制 长沙民政职业技术学院

  2. 案例导入 • 项目 资源访问权限过滤器的编写与配置 • 任务 1.对中文编码进行统一处理; 2. 根据登陆用户的权限控制资源访问。 • 技术要点(关键字) 1. Filter的编写与配置 2.实现资源访问权限的统一控制

  3. 目标 • Filter基本概念和适用场合 • 使用Filter的基本步骤 • 定义和使用Filter • Listener基础

  4. 过滤器 请求 请求 身份认证 资源审核 资源加密访问 响应 响应 过滤器简介 • 过滤器是向 Web 应用程序的请求和响应处理添加功能的 Web 服务组件 • 过滤器的工作原理: Web 资源 浏览器

  5. Filter 接口 Filter被装载 的时候 被自动调用 过滤器要完成的 功能都在 此方法中实现 Filter销毁 的时候 被自动调用 • 创建一个类,实现Filter接口 Filter 接口 Init() doFilter() destroy()

  6. 创建过滤器 package myfilter; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class FirstFilter implements Filter { public void init(FilterConfig config) throws ServletException {} public void destroy(){} public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,ServletException { //写文件 FileWriter fw = new FileWriter("filter.txt"); PrintWriter pw = new PrintWriter(fw); pw.print("First Filter"); pw.close(); fw.close(); //必须调用 chain.doFilter(request,response); } }

  7. 过滤器链 • FilterChain 接口用于调用过滤器链中的一系列过滤器 浏 览 器 过滤器1 过滤器3 Web 资源 过滤器2 过滤器链

  8. web.xml中注册Filter • <filter> • <filter-name>firstFilte</filter-name> • <filter-class>myfilter.FirstFilter</filter-class> • </filter> • <filter-mapping> • <filter-name>firstFilter</filter-name> • <url-pattern>/*</url-pattern> • </filter-mapping> • <filter> • <filter-name>secondFilter</filter-name> • <filter-class>myfilter.SecondFilter</filter-class> • </filter> • <filter-mapping> • <filter-name>secondFilter</filter-name> • <url-pattern>/*</url-pattern> • </filter-mapping>

  9. Listener Listener HttpSessionListener ServletContextListener ServletContextAttributeListener HttpSessionAttributeListener 监听 Servlet Context 属性的变化 监听HttpSession 添加、删除、替换 属性 监听 HttpSession对象 销毁或创建 监听 Servlet Context 的变化

  10. ServletContextListener • Servlet Context创建时 Servlet Context关闭时 都会通知ServletContextListener • 方法: • servletContextInitialized(ServletContextEvent sce) 当Servlet Context创建的时候,将会调用这个方法; • servletContextDestroyed(ServletContextEvent sce) 当Servlet Context销毁的时候(例如关闭应用服务器或者重新加载应用),将会调用这个方法。

  11. ServletContextAttributeListener • 当往Servlet Context添加、删除或者替换一个属性的时候,将会通知ServletContextAttributesListener • 方法: • void attributeAdded(ServletContextAttributeEvent scab):当往ServletContext中加入一个属性的时候,将会调用这个方法; • void attributeRemoved(ServletContextAttributeEvent scab):当从ServletContext中删除一个属性的时候,将会调用这个方法; • void attributeReplaced(ServletContextAttributeEvent scab):当改变ServletContext中的属性的时候,将会调用这个方法。

  12. HttpSessionListener • 当一个HttpSession刚被创建(created)或者失效(invalidated)的时候,将会通知HttpSessionListener • 方法: • void sessionCreated(HttpSessionEvent hse):当一个HttpSession对象被创建时,将会调用这个方法; • void sessionDestroyed(HttpSessionEvent hse):当一个HttpSession超时或者调用HttpSession的invalidate()方法让它销毁时,将会调用这个方法

  13. HttpSessionAttributeListener • HttpSession中添加、删除或者替换一个属性的时候,将会通知HttpSessionAttributesListener • 方法: • void attributeAdded(HttpSessionBindingEvent e):当往会话中加入一个属性的时候,将会调用这个方法; • attributeRemoved(HttpSessionBindingEvent e):当从会话中删除一个属性的时候,将会调用这个方法; • attributeReplaced(HttpSessionBindingEvent e):当改变会话中的属性的时候,将会调用这个方法。

  14. 示例 package mylistener; import javax.servlet.*; import javax.servlet.http.*; public class OnlineUser implements HttpSessionListener,ServletContextListener { //在线人数 private int count = 0; ServletContext ctx = null; //初始化ServletContext public void contextInitialized(ServletContextEvent e) { ctx = e.getServletContext(); }

  15. 示例(续) //将ServletContext设置成null public void contextDestroyed(ServletContextEvent e) { ctx = null; } //当新创建一个HttpSession对象时, //将当前的在线人数加上1,并且保存到ServletContext(application)中 public void sessionCreated(HttpSessionEvent e) { count ++; ctx.setAttribute("OnlineUser",new Integer(count)); } //当一个HttpSession被销毁时(过期或者调用了invalidate()方法) //将当前人数减去1,并且保存到ServletContext(application)中 public void sessionDestroyed(HttpSessionEvent e) { count --; ctx.setAttribute("OnlineUser",new Integer(count)); } }

  16. web.xml中配置Listener <listener> <listener-class>myListener.FristListener</listener-class> </listener>

  17. 总结 • Filter接口 • 定义和使用Filter • FilterChain • Filter在web.xml中配置 • Listener的使用

More Related