Java全栈学习笔记39

# 过滤器

也是和servlet一样,属于是web服务器的组件(应用程序)

实现Filter接口。

init,destory,doFilter

创建过滤器

/**

* @param request 封装请求

* @param response 封装响应

* @param filteChain 过滤器链

*/

@Override

public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)throws IOException, ServletException {

//过滤器执行的方法

filterChain.doFilter(request, response);

}

装配filter

使用web.xml文件

<filter>

<filter-name>firstFilter</filter-name>

<filter-class>com.baor.filter.FirstFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>firstFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

/* 代表是过滤所有的请求。url-pattern可以配置多个

使用注解装配

@WebFilter

多个过滤器的执行顺序

通过web.xml配置的先后顺序,决定执行顺序

如果使用注解装配,默认是使用类的先后顺序决定过滤器的执行先后顺序

#监听器

通过实现不同的事件监听的接口,实现不同的监听器

ServletRequest HttpSession ServletContext

装配

web.xml

<listener>

<listener-class>com.baor.listener.RequestListener</listener-class>

</listener>

注解

@WebListener

@Override

public void requestDestroyed(ServletRequestEvent sre) {

System.out.println("请求结束");

}

@Override

public void requestInitialized(ServletRequestEvent sre) {

System.out.println("请求初始化");

}

/**

* 为request请求域中存储数据

*/

@Override

public void attributeAdded(ServletRequestAttributeEvent srae) {

System.out.println("请求域中存储数据:"+srae.getName()+":"+srae.getValue());

}

/**

* 移除数据

*/

@Override

public void attributeRemoved(ServletRequestAttributeEvent srae) {

System.out.println("移除数据:"+srae.getName()+":"+srae.getValue());

}

/**

* 替换数据

*/

@Override

public void attributeReplaced(ServletRequestAttributeEvent srae) {

System.out.println("新值:"+srae.getServletRequest().getAttribute(srae.getName()));

System.out.println("替换数据:"+srae.getName()+":"+srae.getValue());

}

@Override

public void sessionCreated(HttpSessionEvent se) {

System.out.println("session创建");

}

@Override

public void sessionDestroyed(HttpSessionEvent se) {

//TODO 登出

System.out.println("session销毁");

}

/*

* session中存储内容

*/

@Override

public void attributeAdded(HttpSessionBindingEvent se) {

System.out.println("session中添加内容:"+se.getName()+":"+se.getValue());

}

/*

* session中移除内容

*/

@Override

public void attributeRemoved(HttpSessionBindingEvent se) {

System.out.println("session中移除内容:"+se.getName()+":"+se.getValue());

}

/*

* session中替换内容

*/

@Override

public void attributeReplaced(HttpSessionBindingEvent se) {

System.out.println("新值:"+se.getSession().getAttribute(se.getName()));

System.out.println("session中替换内容:"+se.getName()+":"+se.getValue());

}

@Override

public void contextDestroyed(ServletContextEvent sce) {

System.out.println("ServletContext对象销毁");

}

@Override

public void contextInitialized(ServletContextEvent sce) {

int i = 1 / 1;

System.out.println("ServletContext初始化");

}

@Override

public void attributeAdded(ServletContextAttributeEvent scae) {

System.out.println("ServletContext中添加内容:"+scae.getName()+":"+scae.getValue());

}

@Override

public void attributeRemoved(ServletContextAttributeEvent scae) {

System.out.println("ServletContext中移除内容:"+scae.getName()+":"+scae.getValue());

}

@Override

public void attributeReplaced(ServletContextAttributeEvent scae) {

System.out.println("新值:"+scae.getServletContext().getAttribute(scae.getName()));

System.out.println("ServletContext中替换内容:"+scae.getName()+":"+scae.getValue());

}

# 案例

技术划分:前端(客户端)和后端(服务器端)

应用划分:前端(用户端)和后端(后台管理系统)

前端不存在用户访问控制。和用户相关的操作,需要获取用户信息。此时是在登录后,后端需要返回用户信息,前端(客户端)需要在某个地方(本地存储/cookie)保存用户信息,在用时取出即可

后台管理系统,需要用户访问限制,除登录功能外,其他功能没有登录状态一律不能访问。前端(客户端)登录后,后端返回一个类似令牌的内容,之后的操作需要在每次请求中携带令牌。后端(服务器端)验证是否存在令牌以及令牌的合理性,如果令牌不存在或者不合理,都不允许访问。

单点登录(sa-token,shiro,Spring-security)/免登录/顶掉下线...都是在用户端

前后端分离的开发模式

前端和后端不在同一服务下

Apache,nginx,serve...

外卖 ---> 后台管理系统-- --商家,配送员,总管理平台

跨域

当服务器请求发生在非同源策略中,就会产生跨域问题

非同源 -- 不同主机名或者不同端口号

在发送真正的请求之前,会先发起一个请求,该请求的方法OPTIONS(验证权限)

前端解决 --- jsonp

后端解决 --

resp.setHeader("Access-Control-Allow-Origin", "*");

允许的请求方法

resp.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");

允许的请求头

resp.setHeader("Access-Control-Allow-Headers", "Content-Type, Token , Authorization");

预检请求的有效期(秒)

resp.setHeader("Access-Control-Max-Age", "3600");

相关推荐
Hammer_Hans几秒前
DFT笔记40
笔记
我登哥MVP1 分钟前
【Spring6笔记】 - 15 - Spring中的八大设计模式
java·spring boot·笔记·spring·设计模式·intellij-idea
Engineer邓祥浩3 分钟前
JVM学习笔记(8) 第三部分 虚拟机执行子系统 第7章 虚拟机类加载机制
jvm·笔记·学习
深蓝海拓6 分钟前
基于QtPy (PySide6) 的PLC-HMI工程项目(七)上位机通信部分的初步建设:socket客户端
网络·笔记·python·学习·plc
送外卖的CV工程师7 分钟前
STM32 CubeMX Makefile 工程编译 入门指南
stm32·单片机·嵌入式硬件·学习·makefile·stm32cubemx
蚰蜒螟7 分钟前
深入剖析 Tomcat 9.0.53 源码:Web 资源管理与类加载机制
java·前端·tomcat
喜欢吃燃面8 分钟前
Linux 进程间通信:命名管道与 System V 共享内存深度解析
linux·运维·服务器·学习
绛橘色的日落(。・∀・)ノ9 分钟前
机器学习笔记
笔记
m0_4750645011 分钟前
Spring AI文档切片
java·人工智能·spring
我登哥MVP11 分钟前
【SpringMVC笔记】 - 1 - SpringMVC入门
java·spring boot·spring·tomcat·maven·intellij-idea·springmvc