Rumah > Artikel > hujung hadapan web > nodejs怎样通过jsonp来实现单点登录Demo
这次给大家带来nodejs怎样通过jsonp来实现单点登录Demo,nodejs通过jsonp来实现单点登录Demo的注意事项有哪些,下面就是实战案例,一起来看一下。
说明:使用redis作为session的存储方式
使用引入sso服务器中的动态js地址来获取cookie
应用服务器获取单点服务器提供的特殊标识比如sessionId或其他后,
通过标识直接去往redis中去查询,或是提交(通过rpc)给单点服务器去查询获取登录信息结果
服务器代码示例
const Koa = require('koa');const Router = require('koa-router');const bodyParser = require('koa-bodyparser');const app = new Koa(); const router = new Router(); app.use(bodyParser());//====session s=====var session = require('koa-generic-session');var redisStore = require('koa-redis'); app.keys = ['keys', 'c29tZSBzZWNyZXQgaHVycg']; app.use(session({//配置session store: redisStore({}), cookie: { path: '/', httpOnly: true, maxAge: 1 * 60 * 60 * 1000, rewrite: true, signed: true } }));//====session e=====router.get('/login', function* (next) {//登录页面 this.session=null;//删除cookie this.body=` <form action="/login" method="post"> <p>用户名: <input type="text" name="name" /></p> <p>密码: <input type="text" name="pwd" /></p> <input type="submit" value="提交" /> </form> `; }).post('/login', function* (next) {//提交登录数据 var sinfo = JSON.stringify(this.request.body);//<==获取post数据 this.session.sinfo =sinfo;//<===存入session,模拟登录成功 this.redirect('/');//<===跳转向到你要的页面}); router.get('/', function* (next) { if(this.session&&this.session.sinfo){//判断是否有cookie this.body=`已登录 `; }else{ this.redirect('/login');//<===跳转向到你要的页面 } }); router.get('/sso.js', function* (next) { //动态js if(this.session&&this.session.sinfo&&this.session.sinfo.length>0){ this.body=`var kosid='${this.sessionId}';`;//示例写入sessionId,也就是存入到redis的key }else{ this.body=`window.location.href="http://sso.com/login";`; } }); app.use(router.routes()).use(router.allowedMethods()); app.listen(8087);
应用代码示例:
const Koa = require('koa');const Router = require('koa-router');const bodyParser = require('koa-bodyparser');const app = new Koa(); const router = new Router(); app.use(bodyParser());//====session s=====可以直接用普通session app.keys = ['c29tZSBzZWNyZXQgaHVycg%3D%3D'];var CONFIG = { key: 'koa:sess', /** (string) cookie key (default is koa:sess) */ maxAge: 2000, /** (number) maxAge in ms (default is 1 days) */ overwrite: true, /** (boolean) can overwrite or not (default true) */ httpOnly: true, /** (boolean) httpOnly or not (default true) */ signed: true, /** (boolean) signed or not (default true) */}; app.use(session(CONFIG, app));//====session e=====/* //====session s===== 或者一样吧 var session = require('koa-generic-session'); var redisStore = require('koa-redis'); app.keys = ['keys', 'c29tZSBzZWNyZXQgaHVycg']; app.use(session({//配置session store: redisStore({}), cookie: { path: '/', httpOnly: true, maxAge: 1 * 60 * 60 * 1000, rewrite: true, signed: true } })); //====session e===== */router.get('/', function* (next) { this.body=` <script type="text/javascript" src="http://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script> <script type="text/javascript" src="http://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js"></script> <script src='http://sso.com:8087/sso.js'></script> <script> $.cookie("sid",kosid); if(kosid){ document.write('key:',kosid); }else{ document.write('未登录'); } console.log("this",document.cookie); </script>`; return; }); app.use(router.routes()).use(router.allowedMethods()); app.listen(8088);
windows系统下修改hosts文件
(一般在C:\Windows\System32\drivers\etc下)
添加:
127.0.0.1 sso.com127.0.0.1 testsso.com
浏览器访问:
http://sso.com:8087 http://testsso.com:8088
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
相关阅读:
Atas ialah kandungan terperinci nodejs怎样通过jsonp来实现单点登录Demo. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!