久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長(zhǎng)資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      什么是單點(diǎn)登錄?怎么使用Nodejs實(shí)現(xiàn)SSO

      什么是單點(diǎn)登錄?下面本篇文章給大家給大家介紹一下單點(diǎn)登錄原理,聊聊使用Node實(shí)現(xiàn)單點(diǎn)登錄SSO的方法,希望對(duì)大家有所幫助!

      什么是單點(diǎn)登錄?怎么使用Nodejs實(shí)現(xiàn)SSO

      什么是單點(diǎn)登錄

      隨著公司業(yè)務(wù)的增多,必然會(huì)產(chǎn)生各個(gè)不同的系統(tǒng),如果每個(gè)系統(tǒng)都需要單獨(dú)登錄的話(huà)就會(huì)很不方便。

      因此產(chǎn)生了單點(diǎn)登錄這樣的解決方案,單點(diǎn)登錄全稱(chēng) Single Sign On,簡(jiǎn)稱(chēng)SSO,意思是在多個(gè)系統(tǒng)應(yīng)用群中登錄一個(gè)系統(tǒng),便可在其他所有系統(tǒng)中得到授權(quán)而無(wú)需再次登錄。

      比如小明今天登錄了淘寶,如果沒(méi)有登錄,就會(huì)被要求輸入認(rèn)證信息(用戶(hù)名密碼等),登錄過(guò)后再去訪(fǎng)問(wèn)天貓的頁(yè)面時(shí)就不需要登錄可以直接訪(fǎng)問(wèn)。

      單點(diǎn)登錄原理

      什么是單點(diǎn)登錄?怎么使用Nodejs實(shí)現(xiàn)SSO

      SSO 需要有一個(gè)獨(dú)立的認(rèn)證中心,只有獨(dú)立的驗(yàn)證中心能接受用戶(hù)的用戶(hù)名密碼等安全信息,其他系統(tǒng)不提供登錄入口,只接受認(rèn)證中心的間接授權(quán)。 整個(gè)過(guò)程可以簡(jiǎn)單的用上圖描述:

      • 當(dāng)用戶(hù)登錄訪(fǎng)問(wèn)應(yīng)用A時(shí),應(yīng)用A發(fā)現(xiàn)用戶(hù)未登錄,跳轉(zhuǎn)至SSO認(rèn)證中心,并將自己的地址作為參數(shù)方便回調(diào)

      • SSO認(rèn)證中心發(fā)現(xiàn)用戶(hù)沒(méi)有登錄過(guò),將用戶(hù)引導(dǎo)至登錄頁(yè)面;用戶(hù)填寫(xiě)用戶(hù)名密碼提交登錄申請(qǐng);SSO認(rèn)證中心校驗(yàn)用戶(hù)信息,創(chuàng)建用戶(hù)雨SSO認(rèn)證中心的會(huì)話(huà)(這時(shí)會(huì)把信息保存到cookie中),同時(shí)創(chuàng)建授權(quán)令牌token

      • sso認(rèn)證中心帶著令牌跳轉(zhuǎn)到最初的請(qǐng)求地址(應(yīng)用A)

      • 應(yīng)用A拿到令牌去SSO認(rèn)證中心認(rèn)證是否有效,如果返回有效注冊(cè)應(yīng)用A

      • 應(yīng)用A創(chuàng)建與用戶(hù)之間的會(huì)話(huà),展示資源并維持用戶(hù)登錄態(tài)

      • 當(dāng)用戶(hù)訪(fǎng)問(wèn)應(yīng)用B時(shí),發(fā)現(xiàn)用戶(hù)未登錄(SSO認(rèn)證服務(wù)器與應(yīng)用A應(yīng)用B不是同一個(gè)域,不能提供登錄態(tài)),跳轉(zhuǎn)到SSO認(rèn)證中心,并將自己的地址和之前和SSO認(rèn)證中心會(huì)話(huà)的cookie信息帶入

      • SSO認(rèn)證中心發(fā)現(xiàn)用戶(hù)已登錄,跳轉(zhuǎn)回應(yīng)用B地址,并附上令牌token

      • 同樣的應(yīng)用B拿到令牌去SSO認(rèn)證中心認(rèn)證是否有效,如果返回有效注冊(cè)應(yīng)用B

      • 應(yīng)用B創(chuàng)建與用戶(hù)之間的會(huì)話(huà),展示資源并維持用戶(hù)登錄態(tài)

      NodeJS 演示

      三個(gè)不同的服務(wù)

      這里我們需要啟動(dòng)三個(gè)服務(wù)來(lái)分別模擬 應(yīng)用A,SSO認(rèn)證服務(wù)器和應(yīng)用B

      什么是單點(diǎn)登錄?怎么使用Nodejs實(shí)現(xiàn)SSO

      這里端口號(hào) 8383的服務(wù)是SSO認(rèn)證服務(wù)器,其余的 :8686 和 :8787 分別代表應(yīng)用A與應(yīng)用B。

      其實(shí)應(yīng)用A與應(yīng)用B的代碼幾乎一樣,如上圖所示我們可以通過(guò)穿參的方式來(lái)設(shè)置不同的端口及應(yīng)用名。

      先來(lái)看下效果

      什么是單點(diǎn)登錄?怎么使用Nodejs實(shí)現(xiàn)SSO

      首次訪(fǎng)問(wèn)跳轉(zhuǎn)至登錄頁(yè)

      應(yīng)用A判斷登錄態(tài),跳轉(zhuǎn)到SSO認(rèn)證服務(wù)器

      應(yīng)用A

      const Koa=require('koa'); const Router=require('koa-router') const views=require('koa-views') const static=require('koa-static') const path=require('path'); const app=new Koa(); const router=new Router(); const session=require('koa-session') const koa2Req=require('koa2-request');  //模版引擎相關(guān)配置 app.use(views(path.join(__dirname,'./views')),{     extension:'ejs'   }) app.keys=['key']  const keyMap={   '8686':'koa:sess8686',   '8787':'koa:sess8787' } const CONFIG={     key:keyMap[process.env.PORT] || 'koa:sess',     maxAge:1000*60*60*24,     httpOnly:true } app.use(session(CONFIG,app))  const system=process.env.SERVER_NAME router.get("/",async (ctx)=>{     //通過(guò) session來(lái)判斷 應(yīng)用A的登錄狀態(tài)     let user=ctx.session.user     if(user){      //...     }     else //1、當(dāng)用戶(hù)登錄訪(fǎng)問(wèn)應(yīng)用A時(shí),應(yīng)用A發(fā)現(xiàn)用戶(hù)未登錄(應(yīng)為服務(wù)器沒(méi)有保存對(duì)應(yīng)的session)     {       let token=ctx.query.token       //第一次登錄url上也不會(huì)有令牌       if(!token)       {       //1、跳轉(zhuǎn)到SSO認(rèn)證服務(wù)器        ctx.redirect(`http://localhost:8383/login?redirectUrl=${ctx.host+ctx.originalUrl}`)       }       else       {         //...       }     } }) app.use(router.routes())   const port=process.env.PORT||8888  app.listen(port,()=>{     console.log(`app ${system} running at ${port}`)  })

      認(rèn)證服務(wù)器判斷登錄態(tài),渲染登錄頁(yè)

      認(rèn)證服務(wù)器SSO

      認(rèn)證服務(wù)器的目錄結(jié)構(gòu)如下 主要處理兩個(gè)功能,一是登錄邏輯,二是之后驗(yàn)證令牌的有效性,分別有路由 login.js 和 check-token.js 處理

      什么是單點(diǎn)登錄?怎么使用Nodejs實(shí)現(xiàn)SSO

      Auth/index.js

      const Koa=require('koa'); const Router=require('koa-router') const views=require('koa-views') const path=require('path'); const app=new Koa(); const router=new Router(); const login=require("./routes/login") const checkToken=require('./routes/check-token') const bodyparser=require('koa-bodyparser')  app.use(views(path.join(__dirname,'./views')),{     extension:'ejs'   }) app.use(bodyparser()) //處理登錄相關(guān)的邏輯 router.use('/login',login.routes()) //處理令牌驗(yàn)證的邏輯 router.use('/check_token',checkToken.routes()) app.use(router.routes())  app.listen(8383,()=>{     console.log(`app listen at 8383`) })

      剛才我們從應(yīng)用A跳轉(zhuǎn)到 http://localhost:8383/login?redirectUrl=localhost:8686來(lái)看login中的邏輯
      Auth/routes/login.js

      const service = require("../service"); const router=require("koa-router")()   router.get('/',async (ctx)=>{   const cookies=ctx.cookies;   const token=cookies.get('token');   //從cookie中判斷應(yīng)用A的登錄態(tài)   if(token && service.isTokenVailid(token)){     //。。。如果有登錄過(guò)   }else{     //2、SSO認(rèn)證中心發(fā)現(xiàn)用戶(hù)沒(méi)有登錄過(guò),于是渲染登錄頁(yè)面登錄頁(yè)面;     await ctx.render('login.ejs',{         extension:'ejs'      })   } })  //。。。 module.exports=router

      登錄頁(yè)面

      Auth/views/login.ejs

      <html> <head>     <meta charset="UTF-8">     <meta http-equiv="X-UA-Compatible" content="IE=edge">     <meta name="viewport" content="width=device-width, initial-scale=1.0">     <title>統(tǒng)一登錄</title> </head> <body>     <h1>統(tǒng)一登錄</h1>     <form method="post">        <div>用戶(hù)名: <input type="text" name="name"/></div>        <div>密碼  <input type="text" name="password" /></div>        <div><input type="submit" value='登錄'></div>     </form> </body> </html>

      校驗(yàn)用戶(hù)信息,創(chuàng)建令牌

      Auth/routes/login.js

      router.post('/',async (ctx)=>{ //2、用戶(hù)填寫(xiě)用戶(hù)名密碼提交登錄申請(qǐng);    const body=ctx.request.body;    const {name,password}=body;     //2、SSO認(rèn)證中心校驗(yàn)用戶(hù)信息,    if(name==="admin" && password==="123456"){     //2、創(chuàng)建用戶(hù)雨SSO認(rèn)證中心的會(huì)話(huà)(這時(shí)會(huì)把信息保存到cookie中),同時(shí)創(chuàng)建授權(quán)令牌token        const token="passport";        await ctx.cookies.set('token',token,{            maxAge:1000*60*60*24*30,            httpOnly:true        })        if(ctx.query.redirectUrl){        //3、sso認(rèn)證中心帶著令牌跳轉(zhuǎn)到最初的請(qǐng)求地址(應(yīng)用A)            ctx.redirect(`${ctx.protocol}://${ctx.query.redirectUrl}?token=${token}`)            //回跳地址是 http://localhost:8686/?token=passport        }else{            ctx.body="<h1>登錄成功!</h1>"        }    }else{        ctx.response.body={            error:1,            msg:'用戶(hù)名或密碼錯(cuò)誤'        }    } })

      從認(rèn)證服務(wù)器攜帶令牌跳轉(zhuǎn)回應(yīng)用A

      令牌校驗(yàn) 返回資源

      應(yīng)用A

      app.use(views(path.join(__dirname,'./views')),{     extension:'ejs'   })  //...  const system=process.env.SERVER_NAME router.get("/",async (ctx)=>{     let user=ctx.session.user     if(user){       //...     }     else     //這時(shí)應(yīng)用A依舊沒(méi)有登錄態(tài) 但url上有了令牌 http://localhost:8686/?token=passport    {       let token=ctx.query.token       if(!token)       {         //...跳轉(zhuǎn)去SSO登錄頁(yè)面       }       else        //跳回應(yīng)用A時(shí)走這里的邏輯       {         //ajax請(qǐng)求 4. 應(yīng)用A拿到令牌去SSO認(rèn)證中心認(rèn)證是否有效,如果返回有效注冊(cè)應(yīng)用A         const url=`://localhost:8383/check_token?token=${token}&t=${new Date().getTime()}`         let data = await koa2Req(ctx.protocol + url);         if(data && data.body){             try {                 const body=JSON.parse(data.body)                 const {error,userId}=body;                 // console.log(error,userId) 0,admin                 if(error==0){                     if(!userId){                         ctx.redirect(`http://localhost:8383/login?redirectUrl=${ctx.host+ctx.originalUrl}`)                         return                     }                     //驗(yàn)證通過(guò)后注冊(cè)session,渲染頁(yè)面                     //5. 應(yīng)用A創(chuàng)建與用戶(hù)之間的會(huì)話(huà),展示資源并維持用戶(hù)登錄態(tài)                     ctx.session.user=userId;                     await ctx.render('index.ejs',{                         user:userId,                         system                     })                 }else{                     ctx.redirect(`http://localhost:8383/login?redirectUrl=${ctx.host+ctx.originalUrl}`)                 }             } catch (error) {console.log(error)}                       }       }     } }) app.use(router.routes())  const port=process.env.PORT||8888  app.listen(port,()=>{     console.log(`app ${system} running at ${port}`)  })

      與之對(duì)應(yīng)的 SSO中處理驗(yàn)證令牌的邏輯
      Auth/routes/check-token

      const router=require("koa-router")() const service=require("../service")  router.get('/',async (ctx)=>{   const token=ctx.query.token;   const result={       error:1   }   //當(dāng)token 是 password時(shí)   if(service.isTokenVailid(token)){     result.error=0;     result.userId='admin'   }   ctx.body=result    })   module.exports=router

      Auth/service/index.js

      module.exports={     isTokenVailid: function(token){       if(token && token==='passport'){           return true       }       return false     } }

      至此用戶(hù)已經(jīng)能正常訪(fǎng)問(wèn)應(yīng)用A,SSO服務(wù)器和應(yīng)用A服務(wù)器上都有了用戶(hù)登錄過(guò)的信息。

      訪(fǎng)問(wèn)應(yīng)用B

      帶cookie跳轉(zhuǎn)至SSO認(rèn)證服務(wù)器

      應(yīng)用B

      //...  router.get("/",async (ctx)=>{     let user=ctx.session.user     if(user){       //...     }else{       let token=ctx.query.token       //...       if(!token)       {       //同樣既沒(méi)有session也沒(méi)有令牌,跳轉(zhuǎn)到SSO認(rèn)證服務(wù)器       //6、當(dāng)用戶(hù)訪(fǎng)問(wèn)應(yīng)用B時(shí),發(fā)現(xiàn)用戶(hù)未登錄(SSO認(rèn)證服務(wù)器與應(yīng)用A應(yīng)用B不是同一個(gè)域,不能提供登錄態(tài)),跳轉(zhuǎn)到SSO認(rèn)證中心,并將自己的地址和之前和SSO認(rèn)證中心會(huì)話(huà)的cookie信息帶入           ctx.redirect(`http://localhost:8383/login?redirectUrl=${ctx.host+ctx.originalUrl}`)       }       else       {         //。。。驗(yàn)證令牌的部分       }     } }) app.use(router.routes())  const port=process.env.PORT||8888  app.listen(port,()=>{     console.log(`app ${system} running at ${port}`)  })

      從認(rèn)證服務(wù)器攜帶令牌跳轉(zhuǎn)回應(yīng)用B

      SSO認(rèn)證服務(wù)器 ,再次登錄時(shí)攜帶了cookie,因此不會(huì)再請(qǐng)求登錄頁(yè)面 Auth/routes/login

      //... router.get('/',async (ctx)=>{   const cookies=ctx.cookies;   const token=cookies.get('token');   //7. SSO認(rèn)證中心發(fā)現(xiàn)用戶(hù)已登錄,跳轉(zhuǎn)回應(yīng)用B地址,并附上令牌token   if(token && service.isTokenVailid(token)){     const redirectUrl=ctx.query.redirectUrl;     if(redirectUrl){        //帶著令牌跳轉(zhuǎn)回應(yīng)用B         ctx.redirect(`${ctx.protocol}://${redirectUrl}?token=${token}`)     }else{         ctx.body="<h1>登錄成功!</h1>"     }   }else{     //...渲染登錄頁(yè)面   } }) //..

      令牌校驗(yàn) 返回資源

      這里的邏輯和5,6兩步一樣,因?yàn)閠oken容易偽造,所以要檢驗(yàn)真?zhèn)巍?應(yīng)用B

      app.use(views(path.join(__dirname,'./views')),{     extension:'ejs'   })  //...  const system=process.env.SERVER_NAME router.get("/",async (ctx)=>{     let user=ctx.session.user     if(user){       //...     }     else     //這時(shí)應(yīng)用B依舊沒(méi)有登錄態(tài) 但url上有了令牌 http://localhost:8787/?token=passport    {       let token=ctx.query.token       if(!token)       {         //...跳轉(zhuǎn)去SSO登錄頁(yè)面       }       else        //跳回應(yīng)用B時(shí)走這里的邏輯       {         //ajax請(qǐng)求 8. 同樣的應(yīng)用B拿到令牌去SSO認(rèn)證中心認(rèn)證是否有效,如果返回有效注冊(cè)應(yīng)用B         const url=`://localhost:8383/check_token?token=${token}&t=${new Date().getTime()}`         let data = await koa2Req(ctx.protocol + url);         if(data && data.body){             try {                 const body=JSON.parse(data.body)                 const {error,userId}=body;                 // console.log(error,userId) 0,admin                 if(error==0){                     if(!userId){                         ctx.redirect(`http://localhost:8383/login?redirectUrl=${ctx.host+ctx.originalUrl}`)                         return                     }                     //驗(yàn)證通過(guò)后注冊(cè)session,渲染頁(yè)面                     //9. 應(yīng)用B創(chuàng)建與用戶(hù)之間的會(huì)話(huà),展示資源并維持用戶(hù)登錄態(tài)                     ctx.session.user=userId;                     await ctx.render('index.ejs',{                         user:userId,                         system                     })                 }else{                     ctx.redirect(`http://localhost:8383/login?redirectUrl=${ctx.host+ctx.originalUrl}`)                 }             } catch (error) {console.log(error)}                       }       }     } }) app.use(router.routes())  const port=process.env.PORT||8888  app.listen(port,()=>{     console.log(`app ${system} running at ${port}`)  })

      至此單點(diǎn)登錄的大部分邏輯都已經(jīng)完成,之后再session有效期內(nèi)再訪(fǎng)問(wèn)頁(yè)面,就不需要再登錄,直接返回資源

      router.get("/",async (ctx)=>{ //如果session中有用戶(hù)信息,說(shuō)明已經(jīng)登錄過(guò),直接返回請(qǐng)求資源     let user=ctx.session.user     if(user){         await ctx.render('index.ejs',{               user,               system         })     }     //...  })

      原文地址:https://juejin.cn/post/7088343138905325582

      作者:YoYo君

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)