程序地带

Cookie和Session的鉴权


作为一个测试工程师,做接口自动化时,难免会遇到接口之间的关联以及登录接口等,所以了解cookie和session、token是必备的,下面这篇文章来给大家简单介绍Cookie和Session:


一、Cookie
1、什么是Cookie?

目前大部分网络都是采用HTTP协议,那么HTTP协议本身是没有状态的。等同于,服务器无法判断用户的身份,cookie实质是以一个Key-value格式保存的。客户端向服务器发送请求时,response向客户端发送一个cookie,客户端将此cookie保存起来(保存于客户端),当浏览器再次发起请求时,再请求信息里将cookie一并发送给服务器,服务器检查该cookie,可以校验用户的状态。


2、Cookie的运行流程
#mermaid-svg-gJvGE0chKw0SXm9B .label{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-gJvGE0chKw0SXm9B .label text{fill:#333}#mermaid-svg-gJvGE0chKw0SXm9B .node rect,#mermaid-svg-gJvGE0chKw0SXm9B .node circle,#mermaid-svg-gJvGE0chKw0SXm9B .node ellipse,#mermaid-svg-gJvGE0chKw0SXm9B .node polygon,#mermaid-svg-gJvGE0chKw0SXm9B .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-gJvGE0chKw0SXm9B .node .label{text-align:center;fill:#333}#mermaid-svg-gJvGE0chKw0SXm9B .node.clickable{cursor:pointer}#mermaid-svg-gJvGE0chKw0SXm9B .arrowheadPath{fill:#333}#mermaid-svg-gJvGE0chKw0SXm9B .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-gJvGE0chKw0SXm9B .flowchart-link{stroke:#333;fill:none}#mermaid-svg-gJvGE0chKw0SXm9B .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-gJvGE0chKw0SXm9B .edgeLabel rect{opacity:0.9}#mermaid-svg-gJvGE0chKw0SXm9B .edgeLabel span{color:#333}#mermaid-svg-gJvGE0chKw0SXm9B .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-gJvGE0chKw0SXm9B .cluster text{fill:#333}#mermaid-svg-gJvGE0chKw0SXm9B div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-gJvGE0chKw0SXm9B .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-gJvGE0chKw0SXm9B text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-gJvGE0chKw0SXm9B .actor-line{stroke:grey}#mermaid-svg-gJvGE0chKw0SXm9B .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-gJvGE0chKw0SXm9B .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-gJvGE0chKw0SXm9B #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-gJvGE0chKw0SXm9B .sequenceNumber{fill:#fff}#mermaid-svg-gJvGE0chKw0SXm9B #sequencenumber{fill:#333}#mermaid-svg-gJvGE0chKw0SXm9B #crosshead path{fill:#333;stroke:#333}#mermaid-svg-gJvGE0chKw0SXm9B .messageText{fill:#333;stroke:#333}#mermaid-svg-gJvGE0chKw0SXm9B .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-gJvGE0chKw0SXm9B .labelText,#mermaid-svg-gJvGE0chKw0SXm9B .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-gJvGE0chKw0SXm9B .loopText,#mermaid-svg-gJvGE0chKw0SXm9B .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-gJvGE0chKw0SXm9B .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-gJvGE0chKw0SXm9B .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-gJvGE0chKw0SXm9B .noteText,#mermaid-svg-gJvGE0chKw0SXm9B .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-gJvGE0chKw0SXm9B .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-gJvGE0chKw0SXm9B .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-gJvGE0chKw0SXm9B .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-gJvGE0chKw0SXm9B .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-gJvGE0chKw0SXm9B .section{stroke:none;opacity:0.2}#mermaid-svg-gJvGE0chKw0SXm9B .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-gJvGE0chKw0SXm9B .section2{fill:#fff400}#mermaid-svg-gJvGE0chKw0SXm9B .section1,#mermaid-svg-gJvGE0chKw0SXm9B .section3{fill:#fff;opacity:0.2}#mermaid-svg-gJvGE0chKw0SXm9B .sectionTitle0{fill:#333}#mermaid-svg-gJvGE0chKw0SXm9B .sectionTitle1{fill:#333}#mermaid-svg-gJvGE0chKw0SXm9B .sectionTitle2{fill:#333}#mermaid-svg-gJvGE0chKw0SXm9B .sectionTitle3{fill:#333}#mermaid-svg-gJvGE0chKw0SXm9B .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-gJvGE0chKw0SXm9B .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-gJvGE0chKw0SXm9B .grid .tick text{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-gJvGE0chKw0SXm9B .grid path{stroke-width:0}#mermaid-svg-gJvGE0chKw0SXm9B .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-gJvGE0chKw0SXm9B .task{stroke-width:2}#mermaid-svg-gJvGE0chKw0SXm9B .taskText{text-anchor:middle;font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-gJvGE0chKw0SXm9B .taskText:not([font-size]){font-size:11px}#mermaid-svg-gJvGE0chKw0SXm9B .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-gJvGE0chKw0SXm9B .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-gJvGE0chKw0SXm9B .task.clickable{cursor:pointer}#mermaid-svg-gJvGE0chKw0SXm9B .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-gJvGE0chKw0SXm9B .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-gJvGE0chKw0SXm9B .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-gJvGE0chKw0SXm9B .taskText0,#mermaid-svg-gJvGE0chKw0SXm9B .taskText1,#mermaid-svg-gJvGE0chKw0SXm9B .taskText2,#mermaid-svg-gJvGE0chKw0SXm9B .taskText3{fill:#fff}#mermaid-svg-gJvGE0chKw0SXm9B .task0,#mermaid-svg-gJvGE0chKw0SXm9B .task1,#mermaid-svg-gJvGE0chKw0SXm9B .task2,#mermaid-svg-gJvGE0chKw0SXm9B .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-gJvGE0chKw0SXm9B .taskTextOutside0,#mermaid-svg-gJvGE0chKw0SXm9B .taskTextOutside2{fill:#000}#mermaid-svg-gJvGE0chKw0SXm9B .taskTextOutside1,#mermaid-svg-gJvGE0chKw0SXm9B .taskTextOutside3{fill:#000}#mermaid-svg-gJvGE0chKw0SXm9B .active0,#mermaid-svg-gJvGE0chKw0SXm9B .active1,#mermaid-svg-gJvGE0chKw0SXm9B .active2,#mermaid-svg-gJvGE0chKw0SXm9B .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-gJvGE0chKw0SXm9B .activeText0,#mermaid-svg-gJvGE0chKw0SXm9B .activeText1,#mermaid-svg-gJvGE0chKw0SXm9B .activeText2,#mermaid-svg-gJvGE0chKw0SXm9B .activeText3{fill:#000 !important}#mermaid-svg-gJvGE0chKw0SXm9B .done0,#mermaid-svg-gJvGE0chKw0SXm9B .done1,#mermaid-svg-gJvGE0chKw0SXm9B .done2,#mermaid-svg-gJvGE0chKw0SXm9B .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-gJvGE0chKw0SXm9B .doneText0,#mermaid-svg-gJvGE0chKw0SXm9B .doneText1,#mermaid-svg-gJvGE0chKw0SXm9B .doneText2,#mermaid-svg-gJvGE0chKw0SXm9B .doneText3{fill:#000 !important}#mermaid-svg-gJvGE0chKw0SXm9B .crit0,#mermaid-svg-gJvGE0chKw0SXm9B .crit1,#mermaid-svg-gJvGE0chKw0SXm9B .crit2,#mermaid-svg-gJvGE0chKw0SXm9B .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-gJvGE0chKw0SXm9B .activeCrit0,#mermaid-svg-gJvGE0chKw0SXm9B .activeCrit1,#mermaid-svg-gJvGE0chKw0SXm9B .activeCrit2,#mermaid-svg-gJvGE0chKw0SXm9B .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-gJvGE0chKw0SXm9B .doneCrit0,#mermaid-svg-gJvGE0chKw0SXm9B .doneCrit1,#mermaid-svg-gJvGE0chKw0SXm9B .doneCrit2,#mermaid-svg-gJvGE0chKw0SXm9B .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-gJvGE0chKw0SXm9B .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-gJvGE0chKw0SXm9B .milestoneText{font-style:italic}#mermaid-svg-gJvGE0chKw0SXm9B .doneCritText0,#mermaid-svg-gJvGE0chKw0SXm9B .doneCritText1,#mermaid-svg-gJvGE0chKw0SXm9B .doneCritText2,#mermaid-svg-gJvGE0chKw0SXm9B .doneCritText3{fill:#000 !important}#mermaid-svg-gJvGE0chKw0SXm9B .activeCritText0,#mermaid-svg-gJvGE0chKw0SXm9B .activeCritText1,#mermaid-svg-gJvGE0chKw0SXm9B .activeCritText2,#mermaid-svg-gJvGE0chKw0SXm9B .activeCritText3{fill:#000 !important}#mermaid-svg-gJvGE0chKw0SXm9B .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-gJvGE0chKw0SXm9B g.classGroup text{fill:#9370db;stroke:none;font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-gJvGE0chKw0SXm9B g.classGroup text .title{font-weight:bolder}#mermaid-svg-gJvGE0chKw0SXm9B g.clickable{cursor:pointer}#mermaid-svg-gJvGE0chKw0SXm9B g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-gJvGE0chKw0SXm9B g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-gJvGE0chKw0SXm9B .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-gJvGE0chKw0SXm9B .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-gJvGE0chKw0SXm9B .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-gJvGE0chKw0SXm9B .dashed-line{stroke-dasharray:3}#mermaid-svg-gJvGE0chKw0SXm9B #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-gJvGE0chKw0SXm9B #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-gJvGE0chKw0SXm9B #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-gJvGE0chKw0SXm9B #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-gJvGE0chKw0SXm9B #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-gJvGE0chKw0SXm9B #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-gJvGE0chKw0SXm9B #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-gJvGE0chKw0SXm9B #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-gJvGE0chKw0SXm9B .commit-id,#mermaid-svg-gJvGE0chKw0SXm9B .commit-msg,#mermaid-svg-gJvGE0chKw0SXm9B .branch-label{fill:lightgrey;color:lightgrey;font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-gJvGE0chKw0SXm9B .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-gJvGE0chKw0SXm9B .slice{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-gJvGE0chKw0SXm9B g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-gJvGE0chKw0SXm9B g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-gJvGE0chKw0SXm9B g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-gJvGE0chKw0SXm9B g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-gJvGE0chKw0SXm9B g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-gJvGE0chKw0SXm9B g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-gJvGE0chKw0SXm9B .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-gJvGE0chKw0SXm9B .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-gJvGE0chKw0SXm9B .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-gJvGE0chKw0SXm9B .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-gJvGE0chKw0SXm9B .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-gJvGE0chKw0SXm9B .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-gJvGE0chKw0SXm9B .edgeLabel text{fill:#333}#mermaid-svg-gJvGE0chKw0SXm9B .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-gJvGE0chKw0SXm9B .node circle.state-start{fill:black;stroke:black}#mermaid-svg-gJvGE0chKw0SXm9B .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-gJvGE0chKw0SXm9B #statediagram-barbEnd{fill:#9370db}#mermaid-svg-gJvGE0chKw0SXm9B .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-gJvGE0chKw0SXm9B .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-gJvGE0chKw0SXm9B .statediagram-state .divider{stroke:#9370db}#mermaid-svg-gJvGE0chKw0SXm9B .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-gJvGE0chKw0SXm9B .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-gJvGE0chKw0SXm9B .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-gJvGE0chKw0SXm9B .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-gJvGE0chKw0SXm9B .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-gJvGE0chKw0SXm9B .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-gJvGE0chKw0SXm9B .note-edge{stroke-dasharray:5}#mermaid-svg-gJvGE0chKw0SXm9B .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: ""trebuchet ms", verdana, arial";--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-gJvGE0chKw0SXm9B .error-icon{fill:#522}#mermaid-svg-gJvGE0chKw0SXm9B .error-text{fill:#522;stroke:#522}#mermaid-svg-gJvGE0chKw0SXm9B .edge-thickness-normal{stroke-width:2px}#mermaid-svg-gJvGE0chKw0SXm9B .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-gJvGE0chKw0SXm9B .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-gJvGE0chKw0SXm9B .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-gJvGE0chKw0SXm9B .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-gJvGE0chKw0SXm9B .marker{fill:#333}#mermaid-svg-gJvGE0chKw0SXm9B .marker.cross{stroke:#333}
:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}
#mermaid-svg-gJvGE0chKw0SXm9B {
color: rgba(0, 0, 0, 0.75);
font: ;
}
client
web server
①HTTP Request
②HTTP Response + Cookie
③HTTP Request + Cookie
④HTTP Response
client
web server
3、实例-登录京东
3.1 登录京东
3.2 退出登录
3.3 回到登录界面,查看cookie信息

在这里插入图片描述


3.3.1 会发现上次登录的用户名(手机号码)已经回显在页面上
3.3.2 F12->Appication->Cookie

在这里插入图片描述


3.3.3 mp就是我们填写的用户名
3.3.4 Cookie是有过期机制的,Expires是过期时间,相当于一个月之内登录京东,都会看到手机号回显的效果
二、Session
1、什么是Session?

Session 是存放在服务器端的,当浏览器第一次发送请求时,服务器自动生成了一个Session和一个Session ID用来唯一标识这个Session,并将其通过响应发送到浏览器。当浏览器第二次发送请求,会将前一次服务器响应中的Session ID放在请求中一并发送到服务器上,服务器从请求中提取出Session ID,并和保存的所有Session ID进行对比,找到这个用户对应的Session。


2、运行机制?

服务器生成、存储、验证,以cookie的方式传给客户端,客户端以同样的方式发送给服务端。session有状态。


#mermaid-svg-SHfWXVn6N5FtA0mh .label{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-SHfWXVn6N5FtA0mh .label text{fill:#333}#mermaid-svg-SHfWXVn6N5FtA0mh .node rect,#mermaid-svg-SHfWXVn6N5FtA0mh .node circle,#mermaid-svg-SHfWXVn6N5FtA0mh .node ellipse,#mermaid-svg-SHfWXVn6N5FtA0mh .node polygon,#mermaid-svg-SHfWXVn6N5FtA0mh .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-SHfWXVn6N5FtA0mh .node .label{text-align:center;fill:#333}#mermaid-svg-SHfWXVn6N5FtA0mh .node.clickable{cursor:pointer}#mermaid-svg-SHfWXVn6N5FtA0mh .arrowheadPath{fill:#333}#mermaid-svg-SHfWXVn6N5FtA0mh .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-SHfWXVn6N5FtA0mh .flowchart-link{stroke:#333;fill:none}#mermaid-svg-SHfWXVn6N5FtA0mh .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-SHfWXVn6N5FtA0mh .edgeLabel rect{opacity:0.9}#mermaid-svg-SHfWXVn6N5FtA0mh .edgeLabel span{color:#333}#mermaid-svg-SHfWXVn6N5FtA0mh .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-SHfWXVn6N5FtA0mh .cluster text{fill:#333}#mermaid-svg-SHfWXVn6N5FtA0mh div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-SHfWXVn6N5FtA0mh .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-SHfWXVn6N5FtA0mh text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-SHfWXVn6N5FtA0mh .actor-line{stroke:grey}#mermaid-svg-SHfWXVn6N5FtA0mh .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-SHfWXVn6N5FtA0mh .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-SHfWXVn6N5FtA0mh #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-SHfWXVn6N5FtA0mh .sequenceNumber{fill:#fff}#mermaid-svg-SHfWXVn6N5FtA0mh #sequencenumber{fill:#333}#mermaid-svg-SHfWXVn6N5FtA0mh #crosshead path{fill:#333;stroke:#333}#mermaid-svg-SHfWXVn6N5FtA0mh .messageText{fill:#333;stroke:#333}#mermaid-svg-SHfWXVn6N5FtA0mh .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-SHfWXVn6N5FtA0mh .labelText,#mermaid-svg-SHfWXVn6N5FtA0mh .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-SHfWXVn6N5FtA0mh .loopText,#mermaid-svg-SHfWXVn6N5FtA0mh .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-SHfWXVn6N5FtA0mh .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-SHfWXVn6N5FtA0mh .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-SHfWXVn6N5FtA0mh .noteText,#mermaid-svg-SHfWXVn6N5FtA0mh .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-SHfWXVn6N5FtA0mh .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-SHfWXVn6N5FtA0mh .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-SHfWXVn6N5FtA0mh .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-SHfWXVn6N5FtA0mh .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SHfWXVn6N5FtA0mh .section{stroke:none;opacity:0.2}#mermaid-svg-SHfWXVn6N5FtA0mh .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-SHfWXVn6N5FtA0mh .section2{fill:#fff400}#mermaid-svg-SHfWXVn6N5FtA0mh .section1,#mermaid-svg-SHfWXVn6N5FtA0mh .section3{fill:#fff;opacity:0.2}#mermaid-svg-SHfWXVn6N5FtA0mh .sectionTitle0{fill:#333}#mermaid-svg-SHfWXVn6N5FtA0mh .sectionTitle1{fill:#333}#mermaid-svg-SHfWXVn6N5FtA0mh .sectionTitle2{fill:#333}#mermaid-svg-SHfWXVn6N5FtA0mh .sectionTitle3{fill:#333}#mermaid-svg-SHfWXVn6N5FtA0mh .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SHfWXVn6N5FtA0mh .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-SHfWXVn6N5FtA0mh .grid .tick text{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SHfWXVn6N5FtA0mh .grid path{stroke-width:0}#mermaid-svg-SHfWXVn6N5FtA0mh .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-SHfWXVn6N5FtA0mh .task{stroke-width:2}#mermaid-svg-SHfWXVn6N5FtA0mh .taskText{text-anchor:middle;font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SHfWXVn6N5FtA0mh .taskText:not([font-size]){font-size:11px}#mermaid-svg-SHfWXVn6N5FtA0mh .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SHfWXVn6N5FtA0mh .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-SHfWXVn6N5FtA0mh .task.clickable{cursor:pointer}#mermaid-svg-SHfWXVn6N5FtA0mh .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-SHfWXVn6N5FtA0mh .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-SHfWXVn6N5FtA0mh .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-SHfWXVn6N5FtA0mh .taskText0,#mermaid-svg-SHfWXVn6N5FtA0mh .taskText1,#mermaid-svg-SHfWXVn6N5FtA0mh .taskText2,#mermaid-svg-SHfWXVn6N5FtA0mh .taskText3{fill:#fff}#mermaid-svg-SHfWXVn6N5FtA0mh .task0,#mermaid-svg-SHfWXVn6N5FtA0mh .task1,#mermaid-svg-SHfWXVn6N5FtA0mh .task2,#mermaid-svg-SHfWXVn6N5FtA0mh .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-SHfWXVn6N5FtA0mh .taskTextOutside0,#mermaid-svg-SHfWXVn6N5FtA0mh .taskTextOutside2{fill:#000}#mermaid-svg-SHfWXVn6N5FtA0mh .taskTextOutside1,#mermaid-svg-SHfWXVn6N5FtA0mh .taskTextOutside3{fill:#000}#mermaid-svg-SHfWXVn6N5FtA0mh .active0,#mermaid-svg-SHfWXVn6N5FtA0mh .active1,#mermaid-svg-SHfWXVn6N5FtA0mh .active2,#mermaid-svg-SHfWXVn6N5FtA0mh .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-SHfWXVn6N5FtA0mh .activeText0,#mermaid-svg-SHfWXVn6N5FtA0mh .activeText1,#mermaid-svg-SHfWXVn6N5FtA0mh .activeText2,#mermaid-svg-SHfWXVn6N5FtA0mh .activeText3{fill:#000 !important}#mermaid-svg-SHfWXVn6N5FtA0mh .done0,#mermaid-svg-SHfWXVn6N5FtA0mh .done1,#mermaid-svg-SHfWXVn6N5FtA0mh .done2,#mermaid-svg-SHfWXVn6N5FtA0mh .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-SHfWXVn6N5FtA0mh .doneText0,#mermaid-svg-SHfWXVn6N5FtA0mh .doneText1,#mermaid-svg-SHfWXVn6N5FtA0mh .doneText2,#mermaid-svg-SHfWXVn6N5FtA0mh .doneText3{fill:#000 !important}#mermaid-svg-SHfWXVn6N5FtA0mh .crit0,#mermaid-svg-SHfWXVn6N5FtA0mh .crit1,#mermaid-svg-SHfWXVn6N5FtA0mh .crit2,#mermaid-svg-SHfWXVn6N5FtA0mh .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-SHfWXVn6N5FtA0mh .activeCrit0,#mermaid-svg-SHfWXVn6N5FtA0mh .activeCrit1,#mermaid-svg-SHfWXVn6N5FtA0mh .activeCrit2,#mermaid-svg-SHfWXVn6N5FtA0mh .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-SHfWXVn6N5FtA0mh .doneCrit0,#mermaid-svg-SHfWXVn6N5FtA0mh .doneCrit1,#mermaid-svg-SHfWXVn6N5FtA0mh .doneCrit2,#mermaid-svg-SHfWXVn6N5FtA0mh .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-SHfWXVn6N5FtA0mh .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-SHfWXVn6N5FtA0mh .milestoneText{font-style:italic}#mermaid-svg-SHfWXVn6N5FtA0mh .doneCritText0,#mermaid-svg-SHfWXVn6N5FtA0mh .doneCritText1,#mermaid-svg-SHfWXVn6N5FtA0mh .doneCritText2,#mermaid-svg-SHfWXVn6N5FtA0mh .doneCritText3{fill:#000 !important}#mermaid-svg-SHfWXVn6N5FtA0mh .activeCritText0,#mermaid-svg-SHfWXVn6N5FtA0mh .activeCritText1,#mermaid-svg-SHfWXVn6N5FtA0mh .activeCritText2,#mermaid-svg-SHfWXVn6N5FtA0mh .activeCritText3{fill:#000 !important}#mermaid-svg-SHfWXVn6N5FtA0mh .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SHfWXVn6N5FtA0mh g.classGroup text{fill:#9370db;stroke:none;font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-SHfWXVn6N5FtA0mh g.classGroup text .title{font-weight:bolder}#mermaid-svg-SHfWXVn6N5FtA0mh g.clickable{cursor:pointer}#mermaid-svg-SHfWXVn6N5FtA0mh g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-SHfWXVn6N5FtA0mh g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-SHfWXVn6N5FtA0mh .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-SHfWXVn6N5FtA0mh .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-SHfWXVn6N5FtA0mh .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-SHfWXVn6N5FtA0mh .dashed-line{stroke-dasharray:3}#mermaid-svg-SHfWXVn6N5FtA0mh #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-SHfWXVn6N5FtA0mh #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-SHfWXVn6N5FtA0mh #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-SHfWXVn6N5FtA0mh #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-SHfWXVn6N5FtA0mh #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-SHfWXVn6N5FtA0mh #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-SHfWXVn6N5FtA0mh #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-SHfWXVn6N5FtA0mh #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-SHfWXVn6N5FtA0mh .commit-id,#mermaid-svg-SHfWXVn6N5FtA0mh .commit-msg,#mermaid-svg-SHfWXVn6N5FtA0mh .branch-label{fill:lightgrey;color:lightgrey;font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SHfWXVn6N5FtA0mh .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SHfWXVn6N5FtA0mh .slice{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SHfWXVn6N5FtA0mh g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SHfWXVn6N5FtA0mh g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-SHfWXVn6N5FtA0mh g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-SHfWXVn6N5FtA0mh g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-SHfWXVn6N5FtA0mh g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-SHfWXVn6N5FtA0mh g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-SHfWXVn6N5FtA0mh .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-SHfWXVn6N5FtA0mh .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-SHfWXVn6N5FtA0mh .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-SHfWXVn6N5FtA0mh .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-SHfWXVn6N5FtA0mh .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-SHfWXVn6N5FtA0mh .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-SHfWXVn6N5FtA0mh .edgeLabel text{fill:#333}#mermaid-svg-SHfWXVn6N5FtA0mh .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SHfWXVn6N5FtA0mh .node circle.state-start{fill:black;stroke:black}#mermaid-svg-SHfWXVn6N5FtA0mh .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-SHfWXVn6N5FtA0mh #statediagram-barbEnd{fill:#9370db}#mermaid-svg-SHfWXVn6N5FtA0mh .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-SHfWXVn6N5FtA0mh .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-SHfWXVn6N5FtA0mh .statediagram-state .divider{stroke:#9370db}#mermaid-svg-SHfWXVn6N5FtA0mh .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-SHfWXVn6N5FtA0mh .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-SHfWXVn6N5FtA0mh .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-SHfWXVn6N5FtA0mh .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-SHfWXVn6N5FtA0mh .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-SHfWXVn6N5FtA0mh .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-SHfWXVn6N5FtA0mh .note-edge{stroke-dasharray:5}#mermaid-svg-SHfWXVn6N5FtA0mh .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: ""trebuchet ms", verdana, arial";--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-SHfWXVn6N5FtA0mh .error-icon{fill:#522}#mermaid-svg-SHfWXVn6N5FtA0mh .error-text{fill:#522;stroke:#522}#mermaid-svg-SHfWXVn6N5FtA0mh .edge-thickness-normal{stroke-width:2px}#mermaid-svg-SHfWXVn6N5FtA0mh .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-SHfWXVn6N5FtA0mh .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-SHfWXVn6N5FtA0mh .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-SHfWXVn6N5FtA0mh .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-SHfWXVn6N5FtA0mh .marker{fill:#333}#mermaid-svg-SHfWXVn6N5FtA0mh .marker.cross{stroke:#333}
:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}
#mermaid-svg-SHfWXVn6N5FtA0mh {
color: rgba(0, 0, 0, 0.75);
font: ;
}
client
web server
HTTP Request 发送已登录信息
向用户发送Session ID 记录认证状态
发送包含Session ID 的Cookie,Set-Cookie=**
发送包含Session ID 的Cookie,Set-Cookie=**
校验Session ID来判断用户身份
client
web server
3、Session的过期机制?与Cookie的区别

当服务器端的会话过期了,那么当你继续发起请求的时候,因为你从客户端带过去的会话编号还是之前的那个,就会验证不通过,就会提示你会话过期请重新登录。

Cookie有效时间是写入过期是什么时候,到了这个时间就自动过期,而session的过期一般通过监听系统最后一次访问,如果最近一次访问跟当前访问超过了这个时间,服务端的session就会过期,如果在不断的使用这个系统,那么系统的访问时间会一直不停的更新。

session默认失效时间是30min


4、Session的弊端?
如果做了负载均衡的话,搭建了多个服务器,虽然每个服务器都执行的是同样的业务逻辑,但是session数据是保存在内存中的(不是共享的),用户第一次访问的是服务器1,当用户再次请求时可能访问的是另外一台服务器2,服务器2获取不到session信息,就判定用户没有登陆过。
三、python发起请求使用cookie + session方式进行鉴权
import requests
# 创建session对象,后面直接使用创建的对象发送请求即可
# requests库的session会话对象可以跨请求保持某些参数
s = request.session()
url = 'http://api.juheapi.com/japi/toh'
data = {
"key":"7486da7f50cd55e6774fb3311b526d**",
"v":'1.0',
"month":12,
"day":15
}
response = s.post(url=url,data=data)
print(response.json())

以上内容有误处,麻烦与在下面评论,多多指教,相互学习。谢谢!


版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/m0_47127594/article/details/111246394

随机推荐

c# 溢出抛异常_Java中的异常处理机制的简单原理和应用

异常是指java程序运行时(非编译)所发生的非正常情况或错误,与现实生活中的事件很相似。现实生活中的事件可以包含事件发生的时间、地点、人物、情节等信息,可以用一个对象来表示...

我行我素12334 阅读(385)

PTA L2-011 玩转二叉树(树的遍历+改进code)

L2-011玩转二叉树(25分)给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜面反转,再输出反转后的层序遍历的序列。所谓镜面反转,是指将所有非叶结点的左...

不导翁 阅读(571)

numpy--task2

均匀分布numpy.random.uniform(low=0.0,high=1.0,size=None)Drawsamplesfromauniformdistribution....

忆杉 阅读(255)

面向对象(下)--- final修饰类和方法

final修饰类和方法final:最终的final可以用来修饰的结构:类、方法、变量final用来修饰一个类:此类不能被继承,比如:...

是阿岚呐 阅读(600)

JSP mysql的企业进销存管理系统

介绍:基于JSP的进销存管理系统(MySQL版)1.包含源程序,数据库脚本。代码和数据库脚本都有详细注释。2.课题设计仅供参考学习使用,可以在此基础上进行扩展...

qq-593186283 阅读(358)

try-catch-return

1、情况一(try中有return,finally中没有return):publicclassTryTest{publicstaticvoidm...

我是tata 阅读(893)