当前位置:网站首页>JSD-2204-会话管理-过滤器-Day19
JSD-2204-会话管理-过滤器-Day19
2022-07-23 10:37:00 【程序猿 Monkey】
1.会话管理
- 客户端和服务器之间进行数据交互是遵循的HTTP协议,此协议属于无状态协议(一次请求对弈一次响应,响应完之后则断开连接), 服务器无法跟踪客户端的请求,通过Cookie技术可以给客户端提供一个标识,之后客户端每次请求都会带着这个标识,但是由于这种情况数据是保存在客户端的,存在被篡改的风险, 为了提高安全性同时推出Session解决方案, Session的数据是保存在服务器中的, 不存在被篡改的风险

- Cookie: 类似打孔会员卡, 数据保存在客户端
- cookie的数据默认是保存在浏览器的内存中,当一次会话结束时数据会清除, 可以设置任意的保存时长,设置了保存时间之后数据会保存到磁盘中,当时间到了之后再清除
- 只能保存文本类型的数据
- 数据量最多只能保存几k的数据
- 应用场景: 需要长时间保存的和客户端相关的数据,比如:记住用户名和密码
- Session:类似银行卡,数据保存在服务器内存中
- Session保存数据的时间是半个小时左右,可以修改但是不建议修改.
- 可以保存任意对象类型的数据
- 数据量没有限制(但是不推荐保存大量数据,因为资源有限)
- 应用场景: 对安全性要求较高并且和客户端相关的数据,比如记住登录状态
1.1Cookie的使用方法
后端的使用方法
@RequestMapping("/login")
public int login(@RequestBody User user, HttpSession session, HttpServletResponse response){
User u = mapping.selectByUser(user.getUsername());
if (u!=null){
if (user.getPassword().equals(u.getPassword())){
if (user.getRem()){//需要记住
System.out.println("记住了!");
//创建Cookid 把需要保存的数据装进去
Cookie c1 = new Cookie("username",user.getUsername());
Cookie c2 = new Cookie("password",user.getPassword());
//设置保存时间
c1.setMaxAge(60*60*24*30);
c2.setMaxAge(60*60*24*30);
//发送给客户端
response.addCookie(c1);
response.addCookie(c2);
}
//得到当前客户端对应的会话对象
session.setAttribute("user",u);
return 1;
}
return 2;
}
return 3;
}前端的使用方法
//取出cookie中的用户名和密码显示到页面中
let arr = document.cookie.split(";");
//遍历数组
for (let cookie of arr){
let cookieArr= cookie.split("=");
let name = cookieArr[0].trim();
let value = cookieArr[1];
if (name == "username"){
v.user.username = value;
}else if(name == "password"){
v.user.password = value;
}
}
2.过滤器Filter

- 作用: 过滤器中的代码会在请求到服务器资源(静态资源和动态资源)之前和之后执行, 可以将某些请求资源时需要执行的重复性的代码写在过滤器里面,这样只需要写一次即可, 从而提高了开发效率.
- 如何使用过滤器?
- 创建一个Filter类文件
package cn.tedu.coolshark.filter;
import cn.tedu.coolshark.entitu.User;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter(filterName = "MyFilter",urlPatterns = {"/admin.html","/insertProduct.html","/insertBanner.html"})
public class MyFilter implements Filter {
//初始化执行的方法
public void init(FilterConfig config) throws ServletException {
}
//销毁时执行的方法
public void destroy() {
}
@Override//此方法是请求到服务器资源之前和之后会调用的方法
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
System.out.println("过滤器工作了");
HttpServletRequest ht = (HttpServletRequest) request;
HttpServletResponse he = (HttpServletResponse) response;
//取出请求对象里面的会话对象
HttpSession session = ht.getSession();
User user =(User) session.getAttribute("user");
//判断是否登录
if (user!=null){
chain.doFilter(request, response);//执行此代码表示放行
}else{
//未登录的情况下 让客户端重定向到登录页面
he.sendRedirect("/login.html");
}
}
}
- 在工程名Appliction.java中添加Servlet组件扫描注解

- urlPatterns配置方式:
- 精确匹配: /admin.html /insertProduct.html
- 后缀匹配: *.jpg *.html
- 路径匹配: /product/* /user/*
- 全部匹配: /* (客户端向服务器发出的所有请求都会被拦截)
边栏推荐
- 【无标题】
- Shell script case ---3
- Byte stream & character stream of IO stream
- 基于matlab的BOC调制信号捕获仿真
- Cloud native observability tracking technology in the eyes of Baidu engineers
- Vision and intelligent learning recent journal reading and related knowledge learning
- RTA一种广告精准投放的新玩法?
- day18
- uniapp实现横向点击滑动菜单
- NVIDIA vid2vid paper reproduction
猜你喜欢
随机推荐
RSA加密的使用
Blazor quickly realizes Minesweeper
[software testing] how to sort out your testing business
OPNsense – 多功能高可靠易使用的防火墙(二)
[test platform development] 20. Complete the function of sending interface request on the edit page
Russia hopes to effectively implement the "package" agreement on the export of agricultural products
AVX指令集加速矩阵乘法
The pit trodden by real people tells you to avoid the 10 mistakes often made in automated testing
基于PSO优化的多目标最优值求解matlab仿真
Uniapp realizes horizontal click and slide menu
it 农民工的现状和历史
Kettle implements shared database connection and insert update component instances
postgresql没有nvl的解决办法,postgresql查询所有的表
颜值爆表 Redis官方可视化工具来啦,针不戳
基于matlab的CBOC信号调制解调仿真,输出其相关性,功率谱以及频偏跟踪
raid homes and plunder houses!
Common JS modular specification from a code question
CBOC signal modulation and demodulation simulation based on MATLAB, output its correlation, power spectrum and frequency offset tracking
js判断元素是否到滚动到顶部
day18









