In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
1.Cookie introduction
Cookie is always saved in the client, which can be divided into memory Cookie and hard disk Cookie according to the storage location in the client. The memory Cookie is maintained by the browser and saved in memory. After the browser is closed, it disappears, and its existence time is short. The hard disk Cookie is saved on the hard disk and has an expiration time. Unless the user cleans it manually or reaches the expiration time, the hard disk Cookie will not be deleted and its existence time is long-term. Therefore, according to the time of existence, it can be divided into non-persistent Cookie and persistent Cookie.
Mainly because the HTTP protocol is stateless, the server needs to know what the user has done on the previous page, and the introduction of Cookie is a way to solve the HTTP protocol statelessness.
The server can set or read the information contained in the Cookies to maintain the state in the user and server session.
2.Cookie application scenario
E-commerce shopping sites need to know what products users have purchased before they click to pay. Description: at present, there is also a non-departmental implementation: every time you click to buy, a request is sent directly to the backend, and the backend stores the data to be purchased, and then before clicking on payment, you can obtain the Shangpin data that users want to buy from the backend, which can be done without using Cookie.
The user logs in and remembers the password. After the user logs in, the developer can store the user name and password to the client in some encrypted way. When logging in the second time, the developer can directly obtain the Cookie, and then send Cookie to the backend server for verification, so that the user checks "remember password" and does not need to enter the user name and password later. Within the validity period of Cookie, of course.
JQuery-based jQuery.cookie.js is a library that many developers use to manipulate Cookie in their projects.
Angular provides ngCookies module for users to operate Cookie conveniently and succinctly. Two services are provided under this module, which are:
$cookieStore service
$cookies service
$cookies service in 3.Angular
Cookies is similar to jQuery.cookie.js and provides a way for Angular to manipulate Cookie. Normally, JavaScript writes objects differently to Cookie, but Angular provides a way to write objects to Cookie. $cookies provides the following methods:
Get (key) returns a cookie value for the specified key
GetObject (key) returns a deserialized cookie value of the specified key
GetAll () returns all cookie as a key-value object
Put (key,value, [options]) writes a cookie of key-value
PutObject (key,value, [options]) Serialization sets the Cookie of a key-value
Remove (key, [options]) removes the cookie of the corresponding key
Demo is as follows:
Angular.module ('cookiesExample', [' ngCookies']) .controller ('ExampleController', [' $cookies', function ($cookies) {/ / Retrieving a cookie var favoriteCookie = $cookies.get ('myFavorite'); / / Setting a cookie $cookies.put (' myFavorite', 'oatmeal');}])
For example, set the write directory of cookie, or the expiration time. We can use the default configuration information in $cookiesProvider.
The https://docs.angularjs.org/api/ngCookies/provider/$cookiesProvider#defaults page can view the information that can be configured in $cookieProvider. For example, if a user logs in, the cookie validity period of the password is 7 days. The code is as follows:
Var expireDate = new Date (); expireDate.setDate (expireDate.getDate () + 7); var user= {username:encryptionStr ("admin"), password:encryptionStr ('123456')} $cookies.putObject (' user',user, {'expires':expireDate})
EncryptionStr () is a custom encrypted string method.
$cookieStore service in 4.Angular
The $cookieStore service is based on the back-end Session Cookies, so the options attribute cannot be used when it cannot be written, and its expiration time is session. When you close the browser, cookie becomes invalid. In the process of development, I met a colleague. To complete the user login function of remembering username and password, the following code is used, which is invalid, because $cookieStore cannot set the expiration time by setting the expires in default. The operation Cookie of $cookieStore is based on Session expiration.
Var expireDate = new Date (); expireDate.setDate (expireDate.getDate () + 7); var user= {username:encryptionStr ("admin"), password:encryptionStr ('123456')} $cookieStore.putObject (' user',user, {'expires':expireDate})
At the time of writing this blog, the Angular website has indicated that $cookieStore is out of date, and it is recommended to use the $cookies service.
5. Summary
The $cookies service provided by the ngCookies module provides great convenience for developers to read and write Cookie, and also supports writing and reading JSON objects. At that time, in the process of development, we still could not write too much data to Cookie, on the one hand, the size of Cookie was limited, on the other hand, sensitive data was written into Cookie, which brought security risks. For example, to complete the "remember password" function mentioned earlier, the user name and password we store need to agree on an encryption method with the background, which cannot be stored in plaintext directly.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.