当前位置:网站首页>Web Security (VI) the use of session and the difference between session and cookie
Web Security (VI) the use of session and the difference between session and cookie
2022-07-06 05:28:00 【jinyangjie0】
Session It's a conversation , It is a data structure used to record user information on the server .
Session Use
1、 obtain Session object
HttpSession s = request.getSession(boolean flag);
notes :HttpSession It's an interface .
When flag by true when , According to the SessionId Find corresponding Session object , If it does not exist, return to new Session object .
When flag by false when , Also according to the request SessionId Find corresponding Session object , Returns if it does not exist null.
2、 Common methods
Data binding :
session.setAttribute(String name,Object obj);
Get the binding value according to the binding name :
Object session.getAttribute(String name);
Unbind :
session.removeAttribute(String name);
3、Session Automatic destruction of
Tomcat Server default Session The timeout is 30 minute
You can use web.xml Set the timeout in minutes , Set to 0 Indicates not to destroy .
<session-config> <session-timeout>20</session-timeout> </session-config>
Session And Cookie The difference between
solve HTTP The stateless ways of the agreement itself are cookie and session. Both can record the status , The former is to save the state data on the client , The latter is saved on the server .
Session How it works
session The basic principle of is that the server is for each session Maintain a copy of session information data , The client and server rely on a globally unique identity to access the session information data . User access web When applied , The server program decides when to create session, establish session It can be summed up in three steps :
1、 Generate globally unique identifier (sessionid);
2、 Open up data storage space . Generally, the corresponding data structure will be created in memory , But in this case , Once the system is powered down , All session data will be lost , If it's an e-commerce website , This kind of accident will have serious consequences . But it can also be written to a file or even stored in a database , Although this will increase I/O expenses , but session Some degree of persistence can be achieved , And it's better session The share of ;
3、 take session Send the globally unique identifier of to the client .
Cookie How it works
No matter what server-side technology is used , Just send back HTTP The response contains a header in the form of , It is considered that the server requires a cookie:
Set-cookie:name=name;expires=date;path=path;domain=domain
Support cookie All browsers will respond to this , creating cookie File and save ( It could also be memory cookie), Every time the user sends a request in the future , The browser should judge all the current cookie There is no failure in ( according to expires Attribute judgment ) And it matches path Attribute cookie Information , If any , It will be added to the request header and sent back to the server in the following form :
Cookie: name=“zj”; Path="/linkage"
The dynamic script on the server side will analyze it , And make corresponding treatment , Of course, you can also choose to ignore .
It should be noted that , For security reasons ,cookie Can be disabled by the browser .
边栏推荐
- LeetCode_字符串反转_简单_557. 反转字符串中的单词 III
- 【华为机试真题详解】检查是否存在满足条件的数字组合
- 【torch】|torch. nn. utils. clip_ grad_ norm_
- Tetris
- JS array list actual use summary
- Set detailed map + interview questions
- Configuration file converted from Excel to Lua
- 无代码六月大事件|2022无代码探索者大会即将召开;AI增强型无代码工具推出...
- 02. Develop data storage of blog project
- 剑指 Offer II 039. 直方图最大矩形面积
猜你喜欢
05. 博客项目之安全
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
TCP three handshakes you need to know
Golang -- TCP implements concurrency (server and client)
ARTS Week 25
Notes, continuation, escape and other symbols
Easy to understand IIC protocol explanation
Modbus protocol communication exception
【torch】|torch.nn.utils.clip_grad_norm_
初识CDN
随机推荐
In 2022, we must enter the big factory as soon as possible
Vulhub vulnerability recurrence 71_ Unomi
HAC cluster modifying administrator user password
Easy to understand I2C protocol
UCF(暑期团队赛二)
flutter 实现一个有加载动画的按钮(loadingButton)
js Array 列表 实战使用总结
HAC集群修改管理员用户密码
Talking about the type and function of lens filter
02. Develop data storage of blog project
27io stream, byte output stream, OutputStream writes data to file
毕业设计游戏商城
Jvxetable implant j-popup with slot
Detailed summary of SQL injection
ByteDance program yuan teaches you how to brush algorithm questions: I'm not afraid of the interviewer tearing the code
Ad20 is set with through-hole direct connection copper sheet, and the bonding pad is cross connected
EditorUtility. The role and application of setdirty in untiy
指針經典筆試題
Hyperledger Fabric2. Some basic concepts of X (1)
改善Jpopup以实现动态控制disable