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 鉴权/ 权限管理的更多相关文章

  1. ant design pro v2 关于用户登录有多个权限的解决方法

    ant design pro V2菜单栏显示流程, 用户输入用户名,密码,登录调用登录接口,校验后返回该用户的权限字段currentAuthority,然后通过调用setAuthority(curre ...

  2. Ant Design Pro (中后台系统)教程

    一.概念:https://pro.ant.design/docs/getting-started-cn(官方网站) 1.Ant Design Pro 是什么:  https://www.cnblogs ...

  3. Ant Design Pro入门教程,安装,运行(V5 Typescript版)

    [前言] 找了很多Admin模板,最后还是看中了AntDesignPro这个阿里巴巴开源的Admin框架,长这样(还行吧,目前挺主流的): 官网地址:https://pro.ant.design/in ...

  4. ant design pro (十三)advanced 错误处理

    一.概述 原文地址:https://pro.ant.design/docs/error-cn 二.详细 2.1.页面级报错 2.1.1.应用场景 路由直接引导到报错页面,比如你输入的网址没有匹配到任何 ...

  5. ant design pro (七)和服务端进行交互

    一.概述 原文地址:https://pro.ant.design/docs/server-cn Ant Design Pro 是一套基于 React 技术栈的单页面应用,我们提供的是前端代码和本地模拟 ...

  6. ant design pro(二)布局

    一.概述 参看地址:https://pro.ant.design/docs/layout-cn 其实在上述地址ant-design上已经有详细介绍,本文知识简述概要. 页面整体布局是一个产品最外层的框 ...

  7. ant design pro(一)安装、目录结构、项目加载启动【原始、以及idea开发】

    一.概述 1.1.脚手架概念 编程领域中的“脚手架(Scaffolding)”指的是能够快速搭建项目“骨架”的一类工具.例如大多数的React项目都有src,public,webpack配置文件等等, ...

  8. Ant Design Pro快速入门

    在上一篇文章中,我们介绍了如何构建一个Ant Design Pro的环境. 同时讲解了如何启动服务并查看前端页面功能. 在本文中,我们将简单讲解如何在Ant Design Pro框架下实现自己的业务功 ...

  9. Ant Design Pro 脚手架+umiJS 实践总结

    一.简介 1.Ant Design Pro Ant Design Pro是一款搭建中后台管理控制台的脚手架 ,基于React,dva.js,Ant Design (1)其中dva主要是控制数据流向,是 ...

随机推荐

  1. logback&log4j异步日志配置

    logback 原始配置 配置 appender, 控制文件的滚动方式,日志的输出格式. <appender name="method-time-appender" clas ...

  2. postman使用的时候注意的坑

    https://cloud.tencent.com/developer/article/1341037

  3. laravel5.5框架中视图间如何共享数据?视图间共享数据的两种方法

    laravel框架中视图间共享数据有两种,一种是用视图门面share()方法实现,另一种是用视图门面composer() 方法实现,那么,两种方法的实现究竟是怎样的呢?让我们来看一看接下来的文章内容. ...

  4. 关于 AutoResetEvent 的介绍的简单示例

    关于 AutoResetEvent 的介绍的简单示例 直接贴代码了: class Program { static void Main(string[] args) { string result = ...

  5. 不依赖Spring使用AspectJ达到AOP面向切面编程

    网上大多数介绍AspectJ的文章都是和Spring容器混用的,但有时我们想自己写框架就需要抛开Spring造轮子,类似使用原生AspectJ达到面向切面编程.步骤很简单,只需要两步. 1.导入依赖 ...

  6. 浅谈mongodb内存

    本文仅限于mongodb3.0.0(wiredtiger引擎) 一.mongodb内存使用 1.热数据 这一点是SQL和nosql之间的巨大差距,将热数据存在内存相当于自带cache,若wiredti ...

  7. vscode+flutter+win10搭建问题记录

    1.下载安装vscode.flutter sdk.安装vscode相关插件.android sdk,这些网上有教程,比如https://blog.csdn.net/SVNzK/article/deta ...

  8. Mybatis映射文件标签(关于sql)

    Mybatis映射文件 1.接口的全限定名和映射文件的namespace一致 <mapper namespace="com.offcn.dao.UserDao"> 2. ...

  9. linux命令之——grep详解

    grep命令用于查找文件里符合条件的字符串:也可以用于查找内容包含指定的范本样式的文件.它能使用正则表达式搜索,用于在文件中搜索指定的字符串模式,列出含有匹配模式子符串的文件名,并输出含有该字符串的文 ...

  10. 获取apache ignite缓存中的数据行数少于实际行数

    我将ignite项目打包放到linux下,在linux下获取window中存放在oracle数据库中的数据,linux服务器作为ignite的服务端节点,我在本地启动tomact,作为ignite客户 ...