diff --git a/src/modules/base/middleware/authority.ts b/src/modules/base/middleware/authority.ts index 5fef0c9..47e4381 100644 --- a/src/modules/base/middleware/authority.ts +++ b/src/modules/base/middleware/authority.ts @@ -60,8 +60,11 @@ export class BaseAuthorityMiddleware return; } } catch (error) {} - // 不需要登录 无需权限校验 - if (this.ignoreUrls.includes(url)) { + // 使用matchUrl方法来检查URL是否应该被忽略 + const isIgnored = this.ignoreUrls.some(pattern => + this.matchUrl(pattern, url) + ); + if (isIgnored) { await next(); return; } @@ -153,4 +156,30 @@ export class BaseAuthorityMiddleware await next(); }; } + + // 匹配URL的方法 + matchUrl(pattern, url) { + const patternSegments = pattern.split('/').filter(Boolean); + const urlSegments = url.split('/').filter(Boolean); + + // 如果段的数量不同,则无法匹配 + if (patternSegments.length !== urlSegments.length) { + return false; + } + + // 逐段进行匹配 + for (let i = 0; i < patternSegments.length; i++) { + if (patternSegments[i].startsWith(':')) { + // 如果模式段以':'开始,我们认为它是一个参数,可以匹配任何内容 + continue; + } + // 如果两个段不相同,则不匹配 + if (patternSegments[i] !== urlSegments[i]) { + return false; + } + } + + // 所有段都匹配 + return true; + } } diff --git a/src/modules/user/middleware/app.ts b/src/modules/user/middleware/app.ts index a3882a6..7fbba3a 100644 --- a/src/modules/user/middleware/app.ts +++ b/src/modules/user/middleware/app.ts @@ -46,7 +46,11 @@ export class UserMiddleware implements IMiddleware { return; } } catch (error) {} - if (this.ignoreUrls.includes(url)) { + // 使用matchUrl方法来检查URL是否应该被忽略 + const isIgnored = this.ignoreUrls.some(pattern => + this.matchUrl(pattern, url) + ); + if (isIgnored) { await next(); return; } else { @@ -63,4 +67,30 @@ export class UserMiddleware implements IMiddleware { await next(); }; } + + // 匹配URL的方法 + matchUrl(pattern, url) { + const patternSegments = pattern.split('/').filter(Boolean); + const urlSegments = url.split('/').filter(Boolean); + + // 如果段的数量不同,则无法匹配 + if (patternSegments.length !== urlSegments.length) { + return false; + } + + // 逐段进行匹配 + for (let i = 0; i < patternSegments.length; i++) { + if (patternSegments[i].startsWith(':')) { + // 如果模式段以':'开始,我们认为它是一个参数,可以匹配任何内容 + continue; + } + // 如果两个段不相同,则不匹配 + if (patternSegments[i] !== urlSegments[i]) { + return false; + } + } + + // 所有段都匹配 + return true; + } }