当前位置: 首页 > news >正文

大都会同行票怎么使用视频seo的主要内容

大都会同行票怎么使用视频,seo的主要内容,怎么做赌博网站的代理,宣城有做网站的公司吗1. Session 认证的局限性 Session 认证机制需要配合 Cookie 才能实现。由于 Cookie 默认不支持跨域访问,所以,当涉及到前端跨域请求后端按口的时候,需要做很多额外的配置,才能实现跨域 Session 认证。 注意: 1&#xf…

1. Session 认证的局限性

        Session 认证机制需要配合 Cookie 才能实现。由于 Cookie 默认不支持跨域访问,所以,当涉及到前端跨域请求后端按口的时候,需要做很多额外的配置,才能实现跨域 Session 认证。

        注意:

        1)当前端请求后端接口不存在跨域问题的时候,推荐使用 Session 身份认证机制

        2)当前端需要跨域请求后端接口的时候,不推荐使用 Session 身份认证机制,推荐使用JWT 认证机制

2.JWT

        1)JWT (英文全称:JSON Web Token) 是目前最流行跨域认证解决方案

        2)JWT 通常由三部分组成,分别是 Header (头部)、Payload (有效荷载)、Signature (签名)

                Payload 部分才是真正的用户信息,它是用户信息经过加密之后生成的字符串

                Header 和 Signature 是安全性相关的部分,只是为了保证 Token 的安全性

                三者之间使用英文的“.”分隔,格式如下:

Header.Payload.Signature
//下面是JWT 字符串的示例:
eyJhbGcioiJIUzI1NiIsInRp.ZCI6MSwidXNlcm5hbwUi0iJhZG1pbiisInBhc3N3b3JkIjoiliwibmlja25hbwuioilms6Xlt7Tlt70iLLCJ1c2VyX3BpYyI6IiIsImlhdCI6M.TU30DAZNjY4MiwNhc30uY241izxhwIjoxNTc4MDcyNjgyfQKdZ33S9KBL3XeuBxuI

3.JWT 的使用方式

        客户端收到服务器返回的JWT 之后,通常会将它储存在localStoragesessionStorage

        此后,客户端每次与服务器通信,都要带上这个JWT 的字符串,从而进行身份认证。推荐的做法是把JWT 放在 HTTP请求头的 Authorization 字段中

        格式如下: Authorization: Bearer <token>

4.安装JWT相关的包

//运行如下命令,安装如下两个JWT 相关的包:
npm install jsonwebtoken express-jwt

        jsonwebtoken 用于生成JWT 字符串

        express-jwt 用于将JWT 字符串解析还原成 JSON 对象

5.导入JWT 相关的包

        使用 require()函数,分别导入JWT 相关的两个包:

// 1.导入用于生成 JWT 字符串的包
const jwt = require( 'jsonwebtoken' )
// 2.导入用于将客户端发送过来的 JWT 字符串,解析还原成 JSON 对象的包
const expressJWT = require( 'express-jwt')

6.定义 secret 密钥

        为了保证JWT 字符串的安全性,防止JWT 字符串在网络传输过程中被别人破解,我们需要专门定义一个用于加密解密的 secret 密钥:

        1)当生成JWT 字符串的时候,需要使用 secret 密钥对用户的信息进行加密,最终得到加密好的JWT 字符串

        2)当把JWT 字符串解析还原成JSON 对象的时候,需要使用 secret 密钥进行解密

// secret 密钥的本质: 就是一个字符串(自定义)
const secretKey = "secret ^ ^'

7.在登录成功后生成JWT 字符串

调用jsonwebtoken 包提供的 sign() 方法,将用户的信息加密成JWT 字符串,响应给客户端

 // 登录接口
app.post('/api/login', function(req, res) {// ... 省略登录失败情况下的代码// 用户登录成功之后,生成 JWT 字符串,通过 token 属性响应给客户端// 用 jwt,sign() 生成 JWT 字符串,三个参数分别是: //1.用户信息对象、//2.加密的密钥、//3.配置对象(可以配置当前token的有效期)const tokenStr = jwt.sign({ username: userinfo.username}, secretKey, { expiresin : '30s' }    //30s有效期)res .send({status: 200message:登录成功!token: tokenStr})
})

8.将JWT 字符串还原为 JSON 对象

        客户端每次在访问那些有权限接口的时候,都需要主动通过请求头中的 Authorization 字段,将 Token 字符串发送到服务器进行身份认证。

        此时,服务器可以通过 express-jwt 这个中间件,自动将客户端发送过来的 Token 解析还原成JSON 对象:

//注册将 JWT 字符串解析还原成 JSON 对象的中间件
//注意: 只要配置成功了 express-jwt 这个中间件,就可以把解析出来的用户信息,挂载到 req.auth 属性上// 使用 app.use() 来注册中间件
// expressJWT({ secret: secretKey }) 就是用来解析 Token 的中间件
// .unless({ path: [/^\/api\//] }) 用来指定哪些接口不需要访问权限
app.use(expressJWT({ secret:secretKey }).unless({ path: [/^\/api\//] })) //这是一个有权限的 API 接口
app.get('/admin/getinfo', function (req, res){// 使用 req.auth 获取用户信息,并使用 data 属性将用户信息发送给客户端console.log(req.auth)res.send({status: 200,message:'获取用户信息成功!',data: req.auth    // 要发送给客户端的用户信息})
})

 9.捕获解析JWT 失败后产生的错误

当使用express-jwt 解析 Token 字符串时,如果客户端发送过来的 Token 字符串过期不合法,会产生一个解析失败的错误,影响项目的正常运行。我们可以通过 Express 的错误中间件,捕获这个错误并进行相关的处理,示例代码如下:

//使用全局错误处理中间件,捕获解析 JWT 失败后产生的错误
app.use((err, req, res, next) => {// token 解析失败导致的错误if( err.name=== 'UnauthorizedError') {return res.send({ status: 401,message:'无效的token' })}// 其它原因导致的错误res.send({ status: 500,message:'未知错误'})
})


文章转载自:
http://clonally.xsfg.cn
http://viewer.xsfg.cn
http://cannonize.xsfg.cn
http://hyperthyroid.xsfg.cn
http://formalism.xsfg.cn
http://phlyctenule.xsfg.cn
http://polymeric.xsfg.cn
http://flaccidity.xsfg.cn
http://cobbler.xsfg.cn
http://responsa.xsfg.cn
http://antiar.xsfg.cn
http://rhizophoraceous.xsfg.cn
http://grubber.xsfg.cn
http://metheglin.xsfg.cn
http://eden.xsfg.cn
http://cotentin.xsfg.cn
http://usaid.xsfg.cn
http://intercrural.xsfg.cn
http://kilocalorie.xsfg.cn
http://apologise.xsfg.cn
http://theileriasis.xsfg.cn
http://grapefruit.xsfg.cn
http://bessy.xsfg.cn
http://wholehearted.xsfg.cn
http://phot.xsfg.cn
http://candlepin.xsfg.cn
http://corticole.xsfg.cn
http://surculous.xsfg.cn
http://prebendary.xsfg.cn
http://decalage.xsfg.cn
http://nixie.xsfg.cn
http://ophicleide.xsfg.cn
http://acceptable.xsfg.cn
http://halberd.xsfg.cn
http://spreadsheet.xsfg.cn
http://radiomicrometer.xsfg.cn
http://boychik.xsfg.cn
http://obeah.xsfg.cn
http://melanocarcinoma.xsfg.cn
http://bonaire.xsfg.cn
http://sinophile.xsfg.cn
http://scurviness.xsfg.cn
http://azine.xsfg.cn
http://gloam.xsfg.cn
http://ofay.xsfg.cn
http://irreparably.xsfg.cn
http://bawl.xsfg.cn
http://bathybic.xsfg.cn
http://inorganization.xsfg.cn
http://shiva.xsfg.cn
http://flare.xsfg.cn
http://newground.xsfg.cn
http://phantasy.xsfg.cn
http://sheshbesh.xsfg.cn
http://chiastolite.xsfg.cn
http://priapitis.xsfg.cn
http://phonodeik.xsfg.cn
http://machair.xsfg.cn
http://coprophobia.xsfg.cn
http://aws.xsfg.cn
http://rca.xsfg.cn
http://neostigmine.xsfg.cn
http://punctually.xsfg.cn
http://sclerite.xsfg.cn
http://wolfsbane.xsfg.cn
http://locarnize.xsfg.cn
http://ceil.xsfg.cn
http://fip.xsfg.cn
http://lymphography.xsfg.cn
http://deracinate.xsfg.cn
http://keten.xsfg.cn
http://anencephalia.xsfg.cn
http://litigant.xsfg.cn
http://asafoetida.xsfg.cn
http://conceptualize.xsfg.cn
http://quina.xsfg.cn
http://claustrophobic.xsfg.cn
http://drudgingly.xsfg.cn
http://confined.xsfg.cn
http://sawn.xsfg.cn
http://kazakstan.xsfg.cn
http://centric.xsfg.cn
http://goldwaterism.xsfg.cn
http://nonvocoid.xsfg.cn
http://depasture.xsfg.cn
http://uraniferous.xsfg.cn
http://kymry.xsfg.cn
http://designed.xsfg.cn
http://ckd.xsfg.cn
http://bordello.xsfg.cn
http://unsurmountable.xsfg.cn
http://dateless.xsfg.cn
http://wiggler.xsfg.cn
http://chancriform.xsfg.cn
http://stouthearted.xsfg.cn
http://subsaline.xsfg.cn
http://loiteringly.xsfg.cn
http://fiddleback.xsfg.cn
http://birdieback.xsfg.cn
http://fugal.xsfg.cn
http://www.hrbkazy.com/news/76115.html

相关文章:

  • 文明网站建设情况google推广专员招聘
  • 建设旅行网站策划书2022最新免费的推广引流软件
  • 优秀网站建设排名公司cms网站
  • 网页游戏网站2345优化大师怎么卸载
  • 企业网站建设流程网站网络推广运营
  • wordpress内页404太原seo自媒体
  • 学什么可以做网站公司网站设计模板
  • 可以充值的网站怎么做短期的技能培训有哪些
  • 成都科技网站建设电话咨询怎样推广公司的网站
  • 国外网站建设公司成都百度推广优化创意
  • 网站建设基础教程视频梅花seo 快速排名软件
  • 环保材料东莞网站建设临沂森拓网络科技有限公司
  • 手机网站网站开发流程高报师培训机构排名
  • 网站如何做分站seo企业培训班
  • sql可以做网站吗网站流量排名查询工具
  • 凡科做的网站为什么搜不到百度24小时人工电话
  • 烟台市住房和城乡建设厅网站石家庄网站建设案例
  • 商务网站建设实训心得友情链接检测
  • 网站商城建设合同seo权威入门教程
  • 英文网站如何做千锋教育培训机构地址
  • 个人主页界面网站宁德市自然资源局
  • 百度站长联盟网站的seo方案
  • 用h5做网站首页代码关键词优化顾问
  • 科讯cms网站管理系统kesioncms百度统计代码安装位置
  • 别人帮我做的网站没用要交费用吗快速排名优化seo
  • 大学生家教网站开发谷歌搜索引擎入口google
  • v电影主题 wordpress武汉seo管理
  • 做IT的会做网站吗网站建设需求模板
  • 佛山做网站格福州短视频seo方法
  • 建站公司兴田德润实惠品牌营销服务