How nodejs implements single sign-on Demo through jsonp

php中世界最好的语言
Release: 2018-03-12 09:54:03
Original
2343 people have browsed it

This time I will bring you how nodejs implements single sign-on demo through jsonp. What are the precautions for nodejs to implement single sign-on demo through jsonp. The following is a practical case, let's take a look.

Instructions: Use redis as the session storage method
Use the dynamic js address introduced into the sso server to obtain the cookie
After the application server obtains the special identifier provided by the single-point server such as sessionId or others,
Go directly to redis to query through the identifier, or submit (through rpc) to a single server to query and obtain the login information results

Server code example

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=` 

用户名:

密码:

`; }).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);
Copy after login

Application code example:

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=`    `; return; }); app.use(router.routes()).use(router.allowedMethods()); app.listen(8088);
Copy after login

Modify hosts file under windows system
(usually under C:\Windows\System32\drivers\etc)
Add:

127.0.0.1 sso.com127.0.0.1 testsso.com
Copy after login

Browser access:

http://sso.com:8087 http://testsso.com:8088
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Related reading:

What is the difference between python3 and JS

How to make an image upload preview component in H5

How to use s-xlsx to import and export Excel files

The above is the detailed content of How nodejs implements single sign-on Demo through jsonp. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!