From d0a5c25b5d11e55eb6fea05cc7165458f8340e1b Mon Sep 17 00:00:00 2001 From: RuoYi Date: Wed, 20 Oct 2021 11:23:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=AE=A4=E8=AF=81=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E7=AE=80=E5=8C=96=E6=9D=83=E9=99=90=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../interceptor/HeaderInterceptor.java | 1 + .../com/ruoyi/gateway/filter/AuthFilter.java | 2 +- ruoyi-ui/src/plugins/auth.js | 60 +++++++++++++++++++ ruoyi-ui/src/plugins/index.js | 3 + 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 ruoyi-ui/src/plugins/auth.js diff --git a/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/interceptor/HeaderInterceptor.java b/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/interceptor/HeaderInterceptor.java index caefceb2..3453e1d1 100644 --- a/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/interceptor/HeaderInterceptor.java +++ b/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/interceptor/HeaderInterceptor.java @@ -14,6 +14,7 @@ import com.ruoyi.system.api.model.LoginUser; /** * 自定义请求头拦截器,将Header数据封装到线程变量中方便获取 + * 注意:此拦截器会同时验证当前用户有效期自动刷新有效期 * * @author ruoyi */ diff --git a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/AuthFilter.java b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/AuthFilter.java index e996fffa..101de638 100644 --- a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/AuthFilter.java +++ b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/AuthFilter.java @@ -59,7 +59,7 @@ public class AuthFilter implements GlobalFilter, Ordered Claims claims = JwtUtils.parseToken(token); if (claims == null) { - return unauthorizedResponse(exchange, "token已过期或验证不正确!"); + return unauthorizedResponse(exchange, "令牌已过期或验证不正确!"); } String userkey = JwtUtils.getUserKey(claims); boolean islogin = redisService.hasKey(getTokenKey(userkey)); diff --git a/ruoyi-ui/src/plugins/auth.js b/ruoyi-ui/src/plugins/auth.js new file mode 100644 index 00000000..af740aae --- /dev/null +++ b/ruoyi-ui/src/plugins/auth.js @@ -0,0 +1,60 @@ +import store from '@/store' + +function authPermission(permission) { + const all_permission = "*:*:*"; + const permissions = store.getters && store.getters.permissions + if (permission && permission.length > 0) { + return permissions.some(v => { + return all_permission === v || v === permission + }) + } else { + return false + } +} + +function authRole(role) { + const super_admin = "admin"; + const roles = store.getters && store.getters.roles + if (role && role.length > 0) { + return roles.some(v => { + return super_admin === v || v === role + }) + } else { + return false + } +} + +export default { + // 验证用户是否具备某权限 + hasPermi(permission) { + return authPermission(permission); + }, + // 验证用户是否含有指定权限,只需包含其中一个 + hasPermiOr(permissions) { + return permissions.some(item => { + return authPermission(item) + }) + }, + // 验证用户是否含有指定权限,必须全部拥有 + hasPermiAnd(permissions) { + return permissions.every(item => { + return authPermission(item) + }) + }, + // 验证用户是否具备某角色 + hasRole(role) { + return authRole(role); + }, + // 验证用户是否含有指定角色,只需包含其中一个 + hasRoleOr(roles) { + return roles.some(item => { + return authRole(item) + }) + }, + // 验证用户是否含有指定角色,必须全部拥有 + hasRoleAnd(roles) { + return roles.every(item => { + return authRole(item) + }) + } +} diff --git a/ruoyi-ui/src/plugins/index.js b/ruoyi-ui/src/plugins/index.js index a138e6d6..7cc83a4c 100644 --- a/ruoyi-ui/src/plugins/index.js +++ b/ruoyi-ui/src/plugins/index.js @@ -1,9 +1,12 @@ +import auth from './auth' import cache from './cache' import modal from './modal' import download from './download' export default { install(Vue) { + // 认证对象 + Vue.prototype.$auth = auth // 缓存对象 Vue.prototype.$cache = cache // 模态框对象