ソースを参照

白名单支持对通配符路径匹配

RuoYi 1 年間 前
コミット
2335157f6e
共有2 個のファイルを変更した23 個の追加6 個の削除を含む
  1. 8 3
      ruoyi-ui/src/permission.js
  2. 15 3
      ruoyi-ui/src/utils/validate.js

+ 8 - 3
ruoyi-ui/src/permission.js

@@ -4,11 +4,16 @@ import { Message } from 'element-ui'
4 4
 import NProgress from 'nprogress'
5 5
 import 'nprogress/nprogress.css'
6 6
 import { getToken } from '@/utils/auth'
7
+import { isPathMatch } from '@/utils/validate'
7 8
 import { isRelogin } from '@/utils/request'
8 9
 
9 10
 NProgress.configure({ showSpinner: false })
10 11
 
11
-const whiteList = ['/login', '/register']
12
+const whiteList = ['/login', '/register', '/register*', '/register/*']
13
+
14
+const isWhiteList = (path) => {
15
+  return whiteList.some(pattern => isPathMatch(pattern, path))
16
+}
12 17
 
13 18
 router.beforeEach((to, from, next) => {
14 19
   NProgress.start()
@@ -18,7 +23,7 @@ router.beforeEach((to, from, next) => {
18 23
     if (to.path === '/login') {
19 24
       next({ path: '/' })
20 25
       NProgress.done()
21
-    } else if (whiteList.indexOf(to.path) !== -1) {
26
+    } else if (isWhiteList(to.path)) {
22 27
       next()
23 28
     } else {
24 29
       if (store.getters.roles.length === 0) {
@@ -43,7 +48,7 @@ router.beforeEach((to, from, next) => {
43 48
     }
44 49
   } else {
45 50
     // 没有token
46
-    if (whiteList.indexOf(to.path) !== -1) {
51
+    if (isWhiteList(to.path)) {
47 52
       // 在免登录白名单,直接进入
48 53
       next()
49 54
     } else {

+ 15 - 3
ruoyi-ui/src/utils/validate.js

@@ -1,13 +1,25 @@
1 1
 /**
2
+ * 路径匹配器
3
+ * @param {string} pattern
4
+ * @param {string} path
5
+ * @returns {Boolean}
6
+ */
7
+export function isPathMatch(pattern, path) {
8
+  const regexPattern = pattern.replace(/\//g, '\\/').replace(/\*\*/g, '.*').replace(/\*/g, '[^\\/]*')
9
+  const regex = new RegExp(`^${regexPattern}$`)
10
+  return regex.test(path)
11
+}
12
+
13
+/**
2 14
  * 判断value字符串是否为空 
3 15
  * @param {string} value
4 16
  * @returns {Boolean}
5 17
  */
6 18
 export function isEmpty(value) {
7 19
   if (value == null || value == "" || value == undefined || value == "undefined") {
8
-    return true;
20
+    return true
9 21
   }
10
-  return false;
22
+  return false
11 23
 }
12 24
 
13 25
 /**
@@ -87,7 +99,7 @@ export function validEmail(email) {
87 99
  * @returns {Boolean}
88 100
  */
89 101
 export function isString(str) {
90
-  return typeof str === 'string' || str instanceof String;
102
+  return typeof str === 'string' || str instanceof String
91 103
 }
92 104
 
93 105
 /**