122 lines
2.9 KiB
JavaScript
122 lines
2.9 KiB
JavaScript
/**
|
|
* 鉴权白名单配置
|
|
* 定义哪些 type 类型的请求不需要进行 JWT 鉴权
|
|
*/
|
|
|
|
// type 参数白名单
|
|
const TYPE_WHITELIST = [
|
|
"simpleLogin", // 简单登录
|
|
"login", // 用户登录
|
|
"miniProgramLogin", // 小程序登录
|
|
"refreshToken", // 刷新 Token
|
|
"register", // 用户注册
|
|
"forgetPassword", // 忘记密码
|
|
"resetPassword", // 重置密码
|
|
"getPublicConfig", // 获取公开配置
|
|
"getVerifyCode", // 获取验证码
|
|
"verifyCode", // 验证验证码
|
|
"getUserInfo",
|
|
"getRecommendDoctor", // 获取推荐医生
|
|
"getArticleCateList", // 获取文章分类列表(公开)
|
|
"getArticleList", // 获取文章列表(公开)
|
|
"getArticle", // 获取文章详情(公开)
|
|
"addArticleReadRecord", // 文章阅读打点(公开,用于统计查看量)
|
|
"getDoctorByNo", // 获取医生药师信息
|
|
"getHisEmployeeInfo", // 获取his员工信息
|
|
];
|
|
|
|
// 路径白名单(这些路径完全不需要鉴权)
|
|
const PATH_WHITELIST = [
|
|
"/auth/login",
|
|
"/auth/miniapp/login",
|
|
"/auth/refresh",
|
|
"/auth/register",
|
|
"/IMCallBack",
|
|
"/callback/data",
|
|
"/callback/command",
|
|
"/monitor/stats",
|
|
"/monitor/reset",
|
|
"/monitor/db-health",
|
|
"/alipay-pay-notify",
|
|
"/fengniao-notify",
|
|
"/fengniao-auth",
|
|
"/stats",
|
|
"/zyt-his-notify",
|
|
'/union-pay-notify'
|
|
];
|
|
|
|
/**
|
|
* 检查 type 是否在白名单中
|
|
* @param {string} type - 请求类型
|
|
* @returns {boolean} 是否在白名单中
|
|
*/
|
|
function isTypeInWhitelist(type) {
|
|
return TYPE_WHITELIST.includes(type);
|
|
}
|
|
|
|
/**
|
|
* 检查路径是否在白名单中
|
|
* @param {string} path - 请求路径
|
|
* @returns {boolean} 是否在白名单中
|
|
*/
|
|
function isPathInWhitelist(path) {
|
|
// 精确匹配
|
|
if (PATH_WHITELIST.includes(path)) {
|
|
return true;
|
|
}
|
|
|
|
// 支持通配符匹配
|
|
return PATH_WHITELIST.some((whitelistPath) => {
|
|
if (whitelistPath.endsWith("*")) {
|
|
const prefix = whitelistPath.slice(0, -1);
|
|
return path.startsWith(prefix);
|
|
}
|
|
return false;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 添加 type 到白名单
|
|
* @param {string|string[]} types - 要添加的 type
|
|
*/
|
|
function addTypeToWhitelist(types) {
|
|
const typeArray = Array.isArray(types) ? types : [types];
|
|
typeArray.forEach((type) => {
|
|
if (!TYPE_WHITELIST.includes(type)) {
|
|
TYPE_WHITELIST.push(type);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 从白名单移除 type
|
|
* @param {string|string[]} types - 要移除的 type
|
|
*/
|
|
function removeTypeFromWhitelist(types) {
|
|
const typeArray = Array.isArray(types) ? types : [types];
|
|
typeArray.forEach((type) => {
|
|
const index = TYPE_WHITELIST.indexOf(type);
|
|
if (index > -1) {
|
|
TYPE_WHITELIST.splice(index, 1);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 获取所有白名单 types
|
|
* @returns {string[]} 白名单 types 数组
|
|
*/
|
|
function getWhitelist() {
|
|
return [...TYPE_WHITELIST];
|
|
}
|
|
|
|
module.exports = {
|
|
TYPE_WHITELIST,
|
|
PATH_WHITELIST,
|
|
isTypeInWhitelist,
|
|
isPathInWhitelist,
|
|
addTypeToWhitelist,
|
|
removeTypeFromWhitelist,
|
|
getWhitelist,
|
|
};
|