Ant Design Pro 鉴权/ 权限管理
https://pro.ant.design/docs/authority-management-cn
ant-design-pro 1.0.0 V4
最近需要项目需要用扫码登录,因此就使用antd pro提供的鉴权能力来做

Authorized.ts
提供初始化路由组件和重载路由的函数
import RenderAuthorize from '@/components/Authorized';
import { getAuthority } from './authority';
/* eslint-disable eslint-comments/disable-enable-pair */
/* eslint-disable import/no-mutable-exports */
let Authorized = RenderAuthorize(getAuthority()); // Reload the rights component
const reloadAuthorized = (): void => {
Authorized = RenderAuthorize(getAuthority());
}; export { reloadAuthorized };
export default Authorized;
这里调用 RenderAuthorize(getAuthority()),如果我当前localStorage存的是antd-pro-authority:["admin"] ,则调用实际是 RenderAuthorize(["admin"]) ,直接导至执行以下函数,返回权限组件components/Authorized/Authorized,其中 CURRENT 返回的是当前权限,也即 ["admin"]
currentAuthority => {
if (currentAuthority) {
if (typeof currentAuthority === 'function') {
CURRENT = currentAuthority();
}
if (
Object.prototype.toString.call(currentAuthority) === '[object String]' ||
Array.isArray(currentAuthority)
) {
CURRENT = currentAuthority;
}
} else {
CURRENT = 'NULL';
}
return Authorized;
};
完整如下:renderAuthorize.js
/* eslint-disable eslint-comments/disable-enable-pair */ /* eslint-disable import/no-mutable-exports */
let CURRENT = 'NULL'; /**
* use authority or getAuthority
* @param {string|()=>String} currentAuthority
*/
const renderAuthorize = Authorized => currentAuthority => {
if (currentAuthority) {
if (typeof currentAuthority === 'function') {
CURRENT = currentAuthority();
} if (
Object.prototype.toString.call(currentAuthority) === '[object String]' ||
Array.isArray(currentAuthority)
) {
CURRENT = currentAuthority;
}
} else {
CURRENT = 'NULL';
} return Authorized;
}; export { CURRENT };
export default Authorized => renderAuthorize(Authorized);
值得注意的是,Authorized是在中间被注入的,components/Authorized/index
import Authorized from './Authorized';
import Secured from './Secured';
import check from './CheckPermissions';
import renderAuthorize from './renderAuthorize';
Authorized.Secured = Secured;
Authorized.check = check;
const RenderAuthorize = renderAuthorize(Authorized);
export default RenderAuthorize;
此时问题聚焦到:components/Authorized/Authorized
import React from 'react';
import check from './CheckPermissions'; const Authorized = ({ children, authority, noMatch = null }) => {
const childrenRender = typeof children === 'undefined' ? null : children;
const dom = check(authority, childrenRender, noMatch);
return <>{dom}</>;
}; export default Authorized;
这里使用CheckPermissions对权限作判断,里面实现了对权限的判断逻辑,以及在页面未加载完毕时,控制显示loading的图标
最后回到pages/Authorized上
return (
<Authorized
authority={getRouteAuthority(location.pathname, routes) || ''}
noMatch={isLogin ? <Redirect to="/exception/403" /> : <Redirect to="/user/login" />}
>
{children}
</Authorized>
);
修改后
<Authorized
authority={getRouteAuthority(location.pathname, routes) || ''}
// noMatch={isLogin ? <Redirect to="/exception/403" /> : <Redirect to={`/login?${getPageQuery()}`} />}
currentAuthority={currentAuthority}
noMatch={<Redirect to="/exception/403" />}
>
{children}
</Authorized>
authority传入的是我们在config中路由配置里预定义的权限,noMatch传入的是当鉴权不通过时,应该怎么做,这里加了判断登录,若权限不通过,未登录的立即跳登录,否则即显示权限禁止页,这是适用菜单切换的鉴权,不过,调用登录接口,执行登录逻辑不适合在此做。因为,在layouts/SecurityLayout做登录逻辑更适合,它也是先于其他组件加载的。
我的做法是,让pages/Authorized只判断权限跳403,登录的鉴权交给layouts/SecurityLayout做,这时还不够,还需要修改check的CURRENT的值为在pages/Authorized传入,因为我发现,鉴权组件一开始就会被加载,因此被注入的权限CURRENT不是最新的,所以现在改为实时传入
2020年3月26日:该文章部分内容已过时,请关注 官方发布
Ant Design Pro 鉴权/ 权限管理的更多相关文章
- ant design pro v2 关于用户登录有多个权限的解决方法
ant design pro V2菜单栏显示流程, 用户输入用户名,密码,登录调用登录接口,校验后返回该用户的权限字段currentAuthority,然后通过调用setAuthority(curre ...
- Ant Design Pro (中后台系统)教程
一.概念:https://pro.ant.design/docs/getting-started-cn(官方网站) 1.Ant Design Pro 是什么: https://www.cnblogs ...
- Ant Design Pro入门教程,安装,运行(V5 Typescript版)
[前言] 找了很多Admin模板,最后还是看中了AntDesignPro这个阿里巴巴开源的Admin框架,长这样(还行吧,目前挺主流的): 官网地址:https://pro.ant.design/in ...
- ant design pro (十三)advanced 错误处理
一.概述 原文地址:https://pro.ant.design/docs/error-cn 二.详细 2.1.页面级报错 2.1.1.应用场景 路由直接引导到报错页面,比如你输入的网址没有匹配到任何 ...
- ant design pro (七)和服务端进行交互
一.概述 原文地址:https://pro.ant.design/docs/server-cn Ant Design Pro 是一套基于 React 技术栈的单页面应用,我们提供的是前端代码和本地模拟 ...
- ant design pro(二)布局
一.概述 参看地址:https://pro.ant.design/docs/layout-cn 其实在上述地址ant-design上已经有详细介绍,本文知识简述概要. 页面整体布局是一个产品最外层的框 ...
- ant design pro(一)安装、目录结构、项目加载启动【原始、以及idea开发】
一.概述 1.1.脚手架概念 编程领域中的“脚手架(Scaffolding)”指的是能够快速搭建项目“骨架”的一类工具.例如大多数的React项目都有src,public,webpack配置文件等等, ...
- Ant Design Pro快速入门
在上一篇文章中,我们介绍了如何构建一个Ant Design Pro的环境. 同时讲解了如何启动服务并查看前端页面功能. 在本文中,我们将简单讲解如何在Ant Design Pro框架下实现自己的业务功 ...
- Ant Design Pro 脚手架+umiJS 实践总结
一.简介 1.Ant Design Pro Ant Design Pro是一款搭建中后台管理控制台的脚手架 ,基于React,dva.js,Ant Design (1)其中dva主要是控制数据流向,是 ...
随机推荐
- 【2019-08-29】让自己着眼当下,真TM不容易
07:50 天是蓝色的,路面是灰色的,树是绿色的,那个奶茶店的招牌是白色的.我的表情是木讷的,老婆的表情是嫌弃的,那个路人的表情是无解的,地铁工作人员的表情是无奈的刚才回到公司看到那个通宵了的同事的表 ...
- Jenkins使用过程中遇到的问题
1./usr/local/jdk1.8.0_191/ is not a directory on the Jenkins master (but perhaps it exists on some a ...
- PHP curl cookie不识别
PHP curl cookie不识别 所以curl的时候别用setcookie 用了也没用
- STM32 串口通信基础知识
1.串口通信简介 通信接口的两种方式: 并行通信 -传输原理:数据各个位同时传输. -优点:速度快 -缺点:占用引脚资源多 串行通信 -传输原理:数据按位顺序传输. -优点:占用引脚资源少 -缺点:速 ...
- 《STL源码剖析》——Vector
vector vector的源码分为多个文件:vector.<<stl_vector.h>> vector的底层实现是在<<stl_vector.h>> ...
- SQL 复制表到另一个表
SqlServer 复制表结构和表数据 复制表数据到已存在的表 INSERT INTO targetTableName SELECT COLUMNS FROM sourceTableName; 复制表 ...
- 关于使用PreApplicationStartMethod实现module注册问题
在使用PreApplicationStartMethod实现module注册问题发现一个问题: //用PreApplicationStartMethod实现自注册模块 [assembly: PreAp ...
- python 进程和线程-进程和线程的比较以及分布式进程
进程和线程的比较 参考链接:https://www.liaoxuefeng.com/wiki/1016959663602400/1017631469467456 我们介绍了多进程和多线程,这是实现多任 ...
- 通俗易懂的讲解一下Java的代理模式
一.基本概念 代理模式是对象的结构模式. 代理模式给某一个对象提供一个代理对象,并由代理对象控制对原对象的引用(接口的引用) 二.静态代理 静态代理是指,代理类在程序运行前就已经定义好,其与**目标类 ...
- ES6 Promise对象(七)
一.Promise介绍1.Promise简单说就是一个容器,里面保存着某个未来才会结束的事件(通常是一个异步操作)的结果2.Promise可以将异步操作以同步操作的流程表达出来,避免了层层嵌套的回调函 ...