ホームページ  >  記事  >  ウェブフロントエンド  >  ノードに基づいてシングル サインオン (SSO) を実装する方法について説明します。

ノードに基づいてシングル サインオン (SSO) を実装する方法について説明します。

青灯夜游
青灯夜游転載
2022-12-06 19:49:151819ブラウズ

シングル サインオンとは何ですか?原理は何ですか?それを達成するにはどうすればよいでしょうか?次の記事では、シングル サインオンについて説明し、Node を使用してシングル サインオン SSO を実装する方法について説明します。

ノードに基づいてシングル サインオン (SSO) を実装する方法について説明します。

[関連チュートリアルの推奨事項: nodejs ビデオ チュートリアルプログラミング教育]

とはシングル サインオン

会社の業務が増えると必然的に異なるシステムが生成され、それぞれのシステムに別々のログインが必要になると非常に不便です。

そこで、シングル サインオンというソリューションが生まれました。シングル サインオンの正式名称は、Single Sign On (略して SSO) です。複数のシステムで 1 つのシステムにログインすると、アプリケーション グループにアクセスすると、他のすべてのシステムで認証を受けることができるため、再度ログインする必要はありません。

たとえば、シャオミンは今日タオバオにログインしました。ログインしていない場合は、認証情報 (ユーザー名、パスワードなど) の入力を求められます。ログイン後、Tmall のページにアクセスすると、 、ログインせずに直接アクセスできます。

シングル サインオンの原則

ノードに基づいてシングル サインオン (SSO) を実装する方法について説明します。

SSO には独立した認証センターが必要です。独立した認証センターのみがユーザーを受け入れることができます。セキュリティのため名前やパスワードなどの情報を必要とする場合、他のシステムではログイン入り口が提供されず、認証センターからの間接的な承認のみを受け入れます。 プロセス全体は上の図で簡単に説明できます。

  • ユーザーがアプリケーション A にアクセスするためにログインすると、アプリケーション A はユーザーがログインしていないことを認識し、SSO 認証にジャンプします。パラメータはコールバックに便利です

  • SSO 認証センターは、ユーザーがログインしていないことを検出し、ユーザーをログイン ページに誘導します。ユーザーは入力します。ログイン申請時のユーザー名とパスワードを入力すると、SSO認証センターがユーザー情報を検証し、SSO認証センターのセッションにユーザーRainを作成します(このとき情報はCookieに保存されます)。認可トークンを作成するとき token

  • sso 認証センターはトークンを使用して元のリクエスト アドレスにジャンプします (アプリケーション A)

  • アプリケーション A は、トークンを発行し、SSO 認証センターにアクセスして有効かどうかを確認します。有効な登録アプリケーションが返された場合、A

  • アプリケーション A はユーザーとの会話を作成し、リソースを表示し、ユーザーのログイン ステータスを維持します

  • #ユーザーがアプリケーション B にアクセスすると、ユーザーがログインしていないことがわかります (SSO 認証サーバーとアプリケーション A およびアプリケーション B は同じドメインにないため、ログインを提供できません)ステータス)、SSO 認証センターにジャンプし、SSO 認証センターとの前回のセッションからアドレスと Cookie 情報を取得します。

  • SSO 認証センターは、ユーザーがログインしていることを検出します。アプリケーション B のアドレスに戻り、トークン token を添付します

  • 同じアプリケーション B がトークンを取得し、SSO 認証センターに行き、それが有効かどうかを確認します。有効な登録済みアプリケーション B

  • アプリケーション B はユーザーとのセッションを作成し、リソースを表示し、ユーザーのログイン ステータスを維持します

NodeJS デモ

#3 つの異なるサービスここでは、アプリケーション A、SSO 認証サーバー、アプリケーション B をシミュレートする 3 つのサービスを開始する必要があります

ノードに基づいてシングル サインオン (SSO) を実装する方法について説明します。ここでのポート番号 8383 のサービスは SSO 認証サーバーであり、残り: 8686 と :8787 はそれぞれアプリケーション A とアプリケーション B を表します。

実はアプリケーションAとアプリケーションBのコードはほぼ同じで、上図のようにパラメータを渡すことで異なるポートやアプリケーション名を設定することができます。

まずは効果を見てみましょう

ノードに基づいてシングル サインオン (SSO) を実装する方法について説明します。

初回訪問用のログインページに移動します

アプリケーション A はログイン ステータスを決定し、SSO 認証サーバーにジャンプします。

アプリケーション 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');

//模版引擎相关配置
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)=>{
    //通过 session来判断 应用A的登录状态
    let user=ctx.session.user
    if(user){
     //...
    }
    else //1、当用户登录访问应用A时,应用A发现用户未登录(应为服务器没有保存对应的session)
    {
      let token=ctx.query.token
      //第一次登录url上也不会有令牌
      if(!token)
      {
      //1、跳转到SSO认证服务器
       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}`)

})

認証サーバーはログイン ステータスを決定し、SSO 認証サーバーにジャンプします。ログイン ページをレンダリングします

認証サーバー SSO

#認証サーバーのディレクトリ構造は次のとおりです 主に 2 つの機能を処理します。1 つはログイン ロジックで、もう 1 つはその後のトークンの有効性の検証です。

を処理するために、それぞれルーティングの login.js と check-token.js があります。

Auth /index.jsノードに基づいてシングル サインオン (SSO) を実装する方法について説明します。

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())
//处理登录相关的逻辑
router.use('/login',login.routes())
//处理令牌验证的逻辑
router.use('/check_token',checkToken.routes())
app.use(router.routes())

app.listen(8383,()=>{
    console.log(`app listen at 8383`)
})

今、アプリケーション A から

http://localhost:8383/login?redirectUrl=localhost:8686

にジャンプして、ログインのロジック

Auth /routes/login.js<pre class="brush:js;toolbar:false;">const service = require(&quot;../service&quot;); const router=require(&quot;koa-router&quot;)() router.get(&amp;#39;/&amp;#39;,async (ctx)=&gt;{ const cookies=ctx.cookies; const token=cookies.get(&amp;#39;token&amp;#39;); //从cookie中判断应用A的登录态 if(token &amp;&amp; service.isTokenVailid(token)){ //。。。如果有登录过 }else{ //2、SSO认证中心发现用户没有登录过,于是渲染登录页面登录页面; await ctx.render(&amp;#39;login.ejs&amp;#39;,{ extension:&amp;#39;ejs&amp;#39; }) } }) //。。。 module.exports=router</pre>ログインページ

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>统一登录</title>
</head>
<body>
    <h1>统一登录</h1>
    <form method="post">
       <div>用户名: <input type="text" name="name"/></div>
       <div>密码  <input type="text" name="password" /></div>
       <div><input type="submit" value=&#39;登录&#39;></div>
    </form>
</body>
</html>

ユーザー情報を確認して作成token

Auth/routes/login.js

router.post(&#39;/&#39;,async (ctx)=>{
//2、用户填写用户名密码提交登录申请;
   const body=ctx.request.body;
   const {name,password}=body;
    //2、SSO认证中心校验用户信息,
   if(name==="admin" && password==="123456"){
    //2、创建用户雨SSO认证中心的会话(这时会把信息保存到cookie中),同时创建授权令牌token
       const token="passport";
       await ctx.cookies.set(&#39;token&#39;,token,{
           maxAge:1000*60*60*24*30,
           httpOnly:true
       })
       if(ctx.query.redirectUrl){
       //3、sso认证中心带着令牌跳转到最初的请求地址(应用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:&#39;用户名或密码错误&#39;
       }
   }
})

#認証サーバーからトークンを運び、アプリケーション A

にジャンプします。 # トークン検証はリソースを返します

アプリケーション A

app.use(views(path.join(__dirname,&#39;./views&#39;)),{
    extension:&#39;ejs&#39;
  })

//...

const system=process.env.SERVER_NAME
router.get("/",async (ctx)=>{
    let user=ctx.session.user
    if(user){
      //...
    }
    else
    //这时应用A依旧没有登录态 但url上有了令牌 http://localhost:8686/?token=passport
   {
      let token=ctx.query.token
      if(!token)
      {
        //...跳转去SSO登录页面
      }
      else 
      //跳回应用A时走这里的逻辑
      {
        //ajax请求 4. 应用A拿到令牌去SSO认证中心认证是否有效,如果返回有效注册应用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
                    }
                    //验证通过后注册session,渲染页面
                    //5. 应用A创建与用户之间的会话,展示资源并维持用户登录态
                    ctx.session.user=userId;
                    await ctx.render(&#39;index.ejs&#39;,{
                        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}`)

})
SSO の検証トークンを処理するための対応するロジックAuth/routes/check-token

const router=require("koa-router")()
const service=require("../service")

router.get(&#39;/&#39;,async (ctx)=>{
  const token=ctx.query.token;
  const result={
      error:1
  }
  //当token 是 password时
  if(service.isTokenVailid(token)){
    result.error=0;
    result.userId=&#39;admin&#39;
  }
  ctx.body=result
 

})


module.exports=router

Auth/service/index.js

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

これで、ユーザーはアプリケーション A に通常どおりアクセスできるようになり、ユーザーのログイン情報が SSO サーバーとアプリケーション A サーバーで利用できるようになります。

访问应用B

带cookie跳转至SSO认证服务器

应用B

//...

router.get("/",async (ctx)=>{
    let user=ctx.session.user
    if(user){
      //...
    }else{
      let token=ctx.query.token
      //...
      if(!token)
      {
      //同样既没有session也没有令牌,跳转到SSO认证服务器
      //6、当用户访问应用B时,发现用户未登录(SSO认证服务器与应用A应用B不是同一个域,不能提供登录态),跳转到SSO认证中心,并将自己的地址和之前和SSO认证中心会话的cookie信息带入
          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}`)

})

从认证服务器携带令牌跳转回应用B

SSO认证服务器 ,再次登录时携带了cookie,因此不会再请求登录页面 Auth/routes/login

//...
router.get(&#39;/&#39;,async (ctx)=>{
  const cookies=ctx.cookies;
  const token=cookies.get(&#39;token&#39;);
  //7. SSO认证中心发现用户已登录,跳转回应用B地址,并附上令牌token
  if(token && service.isTokenVailid(token)){
    const redirectUrl=ctx.query.redirectUrl;
    if(redirectUrl){
       //带着令牌跳转回应用B
        ctx.redirect(`${ctx.protocol}://${redirectUrl}?token=${token}`)
    }else{
        ctx.body="<h1>登录成功!</h1>"
    }
  }else{
    //...渲染登录页面
  }
})
//..

令牌校验 返回资源

这里的逻辑和5,6两步一样,因为token容易伪造,所以要检验真伪。 应用B

app.use(views(path.join(__dirname,&#39;./views&#39;)),{
    extension:&#39;ejs&#39;
  })

//...

const system=process.env.SERVER_NAME
router.get("/",async (ctx)=>{
    let user=ctx.session.user
    if(user){
      //...
    }
    else
    //这时应用B依旧没有登录态 但url上有了令牌 http://localhost:8787/?token=passport
   {
      let token=ctx.query.token
      if(!token)
      {
        //...跳转去SSO登录页面
      }
      else 
      //跳回应用B时走这里的逻辑
      {
        //ajax请求 8. 同样的应用B拿到令牌去SSO认证中心认证是否有效,如果返回有效注册应用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
                    }
                    //验证通过后注册session,渲染页面
                    //9. 应用B创建与用户之间的会话,展示资源并维持用户登录态
                    ctx.session.user=userId;
                    await ctx.render(&#39;index.ejs&#39;,{
                        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}`)

})

至此单点登录的大部分逻辑都已经完成,之后再session有效期内再访问页面,就不需要再登录,直接返回资源

router.get("/",async (ctx)=>{
//如果session中有用户信息,说明已经登录过,直接返回请求资源
    let user=ctx.session.user
    if(user){
        await ctx.render(&#39;index.ejs&#39;,{
              user,
              system
        })
    }
    //...
 })

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

作者:YoYo君

更多node相关知识,请访问:nodejs 教程

以上がノードに基づいてシングル サインオン (SSO) を実装する方法について説明します。の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はjuejin.cnで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。