bjx-passport
v1.3.4
Published
登录中间件
Readme
bjx_passport
bjx_passport 是一个用于 Koa 项目的登录认证中间件,支持配置化参数和动态公共参数注入。
安装
使用 npm 安装:
npm install bjx_passport快速开始
1. 配置中间件
在项目中创建一个配置文件(例如 bjx_passport.js),用于初始化 bjx_passport 中间件。
const { initialize, BjxPassport } = require('bjx-passport');
// 设置配置文件
BjxPassport.setConfig({
apisConfig: {
login: 'https://api.example.com/login', // 登录服务
common1: 'https://api.example.com/common1' // 其他服务
},
cookieConfig: {
idsrv: 'idsrv.session' // cookieKey 可选参数
},
// 签名参数
signConfig: {
version: '0.9.0',
apiVersion: '1.0.0',
signVersion: '1.0.0',
signKey: 'your-sign-key',
clientId: 'your-client-id',
clientSecret: 'your-client-secret'
}
});
// 设置公共参数
function bjx_passport() {
return async (ctx, next) => {
// 返回的是一个 Promise
await initialize(ctx, next, {
globalParams: { OS: '1', BA: '2', BP: '3', EQP: ctx.uuid }
});
};
}
module.exports = { bjx_passport };2. 在 Koa 项目中使用
在 app.js 中引入并使用 bjx_passport 中间件。
const Koa = require('koa');
const { bjx_passport } = require('./bjx_passport'); // 引入中间件
const app = new Koa();
// 使用 bjx_passport 中间件
app.use(bjx_passport());
// 添加路由
app.use(async (ctx) => {
ctx.body = 'Hello, bjx_passport!';
});
// 启动服务器
const port = 3000;
app.listen(port, () => {
console.log(`服务器已启动,访问 http://localhost:${port}`);
});配置说明
1. BjxPassport.setConfig
用于设置中间件的全局配置。
| 参数 | 类型 | 说明 | 示例值 |
|----------------|--------|-------------------------------|-------------------------------|
| apisConfig.login | string | 登录服务地址 | https://api.example.com/login |
| apisConfig.common1 | string | 其他服务地址 | https://api.example.com/common1 |
| cookieConfig.idsrv | string | Cookie 名称【可选】 | idsrv.session |
| signConfig.version | string | 客户端版本号 | 0.9.0 |
| signConfig.apiVersion | string | API 版本号【可选】 | 1.0.0 |
| signConfig.signVersion | string | 签名版本号 【可选】 | 1.0.0 |
| signConfig.signKey | string | 签名密钥 | your-sign-key |
| signConfig.clientId | string | 客户端 ID | your-client-id |
| signConfig.clientSecret | string | 客户端密钥 | your-client-secret |
2. initialize
用于初始化中间件,并注入全局参数。
| 参数 | 类型 | 说明 | 示例值 |
|----------------|--------|-------------------------------|-------------------------------|
| globalParams.OS | string | 操作系统参数 | 1 |
| globalParams.BA | string | 浏览器参数 | 2 |
| globalParams.BP | string | 浏览器版本参数 | 3 |
| globalParams.EQP | string | 设备参数 | ctx.uuid |
示例
1. 设置全局配置
BjxPassport.setConfig({
apisConfig: {
login: 'https://api.example.com/login',
common1: 'https://api.example.com/common1'
},
cookieConfig: {
idsrv: 'idsrv.session'
},
signConfig: {
version: '0.9.0',
apiVersion: '1.0.0',
signVersion: '1.0.0',
signKey: 'your-sign-key',
clientId: 'your-client-id',
clientSecret: 'your-client-secret'
}
});2. 使用中间件
app.use(bjx_passport());发布到 npm
登录 npm:
npm login发布包:
npm publish
许可证
本项目基于 ISC License 发布。
