前言
react应用中的路由鉴权是确保用户仅能访问其授权页面的方式,用于已登录或具有访问特定页面所需的权限,由于react没有实现类似于vue的路由守卫功能,所以只能由开发者自行实现。前端中的路由鉴权可以区分以下颗粒度:菜单权限控制、组件权限控制、路由权限控制。在后台管理系统中三者是必不可少的。这篇文章就来记录下react实现路由鉴权的流程。
确定权限树
开始之前我们需要对权限树的数据结构进行确定,一般来说我们需要拿到后端传回的权限树进行存储,在react中通常将权限树存储在redux中,并且我们前端也需要自行维护一颗属于前端的权限树,这里需要两个权限树进行对比从而实现路由鉴权。权限树结构如下:
export type localmenutype = {
key: string;
level: number; // 层级
menucode: string; // 权限码 用于查询是否有此权限
label?: react.reactnode; // 菜单名称
path?: string | string[]; // 路由
parentmenuid?: string; // 父级菜单id
children?: localmenutype[]; // 子菜单
};
权限树处理
在确定权限树后,我们需要对权限树结构进行打平。为此我们可以写个utils来处理权限树。我们使用递归来进行打平权限树:
export const getflattenlist = (
authlist: localmenutype[], // 需要打平的权限树
flattenauthlist: localmenutype[], // 存储打平后的权限树
level?: number // 打平层级
) => {
authlist.foreach((item) => {
// 如果查找层级超出则返回
if (level && item.level > level) return;
flattenauthlist.push(object.assign({}, item, { children: [] }));
if (item.children && item.children.length > 0) {
getflattenlist(item.children, flattenauthlist, level);
}
});
};
通过以上代码我们将一个树打平,打平后结构如下:

菜单权限控制
在后台系统中,每个角色有不同的菜单权限,我们需要根据后端返回的角色数据进行菜单的显示与隐藏,为了方便起见这里直接使用mock数据并将数据存储在localstorage中:
export enum roletype {
manager,
only_order,
only_view_list,
}
export const setcurrentrole = (type: roletype) => {
switch (type) {
case roletype.manager: {
window.localstorage.setitem("menu", json.stringify(menus));
break;
}
case roletype.only_order: {
window.localstorage.setitem("menu", json.stringify(onlyorder));
break;
}
case roletype.only_view_list: {
window.localstorage.setitem("menu", json.stringify(onlyviewlist));
break;
}
}
};
export const getcurrentrole = () =>
json.parse(window.localstorage.getitem("menu")) as localmenutype[];
通过localstorage得到的角色信息进行显示菜单,这里我们只显示1,2级菜单,所以需要对权限菜单进行处理,只保留1,2的菜单数据,也是需要用到递归来处理:
// 处理权限菜单
const handleauthmenu = (
menulist: localmenutype[],// 全部菜单权限树
authcodes: string[], // 角色所有权限
authmenulist: localmenutype[], // 角色最终拥有菜单列表
level?: number // 处理层级
) => {
menulist.foreach((menu) => {
// 如果level 存在,则只处理小于level的情况
if (level && menu.level > level) return;
// 如果有权限,则继续递归遍历菜单
if (authcodes.includes(menu.menucode)) {
let newauthmenu: localmenutype = { ...menu, children: undefined };
let newauthmenuchildren: localmenutype[] = [];
if (menu.children && menu.children.length > 0) {
handleauthmenu(menu.children, authcodes, newauthmenuchildren, level);
}
// 添加子菜单
if (newauthmenuchildren.length > 0) {
newauthmenu.children = newauthmenuchildren;
}
authmenulist.push(newauthmenu);
}
});
};
// 获取角色权限菜单
export const getauthmenu = (flattenauth: localmenutype[], level?: number) => {
// 获取权限菜单的menucode
const authcodes: string[] = flattenauth.map((auth) => auth.menucode);
let authmenu: localmenutype[] = [];
handleauthmenu(menus, authcodes, authmenu, level);
return authmenu;
};
在获取完角色1,2级菜单后,我们需要对左侧菜单栏进行初始化,默认为菜单列表中第一个path。获取二级菜单的首位菜单路由之后通过usenavigate进行跳转。获取菜单路由通过getroutepath进行获取,由于一个页面可能包含多个路由,所以需要对path信息进行判断:
// anthroles.ts
// 获取菜单路由
export const getroutepath = (localmenu: localmenutype) => {
return localmenu.path
? typeof localmenu.path === "object"
? localmenu.path[0]
: localmenu.path
: null;
};
// home.tsx
//2级菜单list
const secondauthmenulist = usememo(() => {
return flattenlist.filter((res) => res.level === 2);
}, [flattenlist]);
// 初始化菜单
useeffect(() => {
const initmenuitem = secondauthmenulist[0];
if (initmenuitem) {
const initroute =
initmenuitem.level > 2 ? pathname : getroutepath(initmenuitem)!;
navigate(initroute, { replace: true });
}
}, [secondauthmenulist, flattenlist]);
那如何根据点击的菜单进行跳转呢?也是需要获取对应key值的二级菜单path进行跳转:
const findsecondmenubykey = (key: string) =>
secondauthmenulist.find((item) => item.key === key);
// 点击菜单进行跳转
const handlemenuchange = ({ key }: { key: string }) => {
setmenuselectkeys([key]);
let chooseitem = findsecondmenubykey(key);
if (chooseitem?.path) navigate(getroutepath(chooseitem) || "");
};
最终实现效果如下:

组件权限控制
组件权限控制相对简单,需要通过menucode也就是权限码进行组件的显示与隐藏,这里以按钮组件为例子,我们需要一个高阶组件authbuutonhoc作为按钮组件的父组件进行显示,同时,我们通过hasauth函数判断是否有当前指定权限:
// 是否有当前权限
export const hasauth = (meuncode: string) => {
// 当前打平的角色权限树
let flattenauthlist: localmenutype[] = getcurrentflattenrole();
return !!flattenauthlist.find((auth) => auth.menucode === meuncode);
};
authbuutonhoc.tsx
const authbutton: react.fc<props> = ({ menucode, children }) => {
// 没有当前权限则不显示
if (!hasauth(menucode)) return null;
return <>{children}</>;
};
export default react.memo(authbutton);
使用authbutton包裹按钮,就能实现组件级别的权限控制:
- 有当前权限:

- 无当前权限:

路由权限控制
路由权限控制需要对用户输入的路径名进行校验,我们通过uselocation获取到当前用户输入的pathname;并路径匹配matchpath判断当前路径与权限菜单路径是否对应,若对应上则表示当前角色拥有权限,若对应不上则跳转到404页面。
通过以上逻辑,我们先创建一个hasauthbyroutepath函数来判断是否有当前的路由权限:
// 是否有当前的路由权限
export const hasauthbyroutepath = (path: string) => {
let flattenauthlist: localmenutype[] = getcurrentflattenrole();
return !!flattenauthlist.find((auth) =>
routepathmatch(path, auth.path || "")
);
};
// 判断路由是否一致
export const routepathmatch = (path: string, menupath: string | string[]) => {
if (typeof menupath === "object") {
return menupath.some((item) => matchpath(item, path));
}
return !!matchpath(menupath, path);
};
在home页面监听用户输入的路径名进行判断,有则跳转到当前菜单:
useeffect(() => {
// 获取当前匹配到的菜单
const matchmenuitem = flattenlist.find((item) =>
routepathmatch(pathname, item.path || "")
);
if (matchmenuitem) {
// 如果当前菜单level为3级或者更小则设置其父id,否则设置其id
matchmenuitem.level > 2
? setmenuselectkeys([matchmenuitem.parentmenuid!])
: setmenuselectkeys([matchmenuitem.key]);
// 如果当前菜单level为3级或者更小则通过父id找到2级菜单
const newsecondmenu =
matchmenuitem.level > 2
? findsecondmenubykey(matchmenuitem.parentmenuid!)
: matchmenuitem;
// 有对应的二级菜单则定位到当前侧边菜单栏位置
if (newsecondmenu) {
setmenuopenkeys((preopenkeys) => {
const openkeysset = new set(preopenkeys || []);
openkeysset.add(newsecondmenu.parentmenuid!);
return array.from(openkeysset);
});
} else {
setmenuselectkeys([]);
}
}
}, [secondauthmenulist, flattenlist, pathname]);
最后,如果没有当前路径权限则跳转到404页面,为此我们需要一个authlayout高阶组件来包裹homepage来实现跳转:
// 白名单
const routerwhitelist = ["/home"];
const authlayout = ({ children }: { children: jsx.element }) => {
const { pathname } = uselocation();
// 判断当前路由是否在白名单内或者有当前权限路由
const hasauthroute = usememo(() => {
return (
routepathmatch(pathname, routerwhitelist) || hasauthbyroutepath(pathname)
);
}, [pathname]);
// 没有权限则跳转至404页面
if (!hasauthroute) return <navigate to="/404" replace />;
return children;
};
export default authlayout;
将以上组件包裹在homepage外层即可实现路由权限控制:
{
path: "/home",
element: (
<authlayout>
<homepage />
</authlayout>
),
}
具体实现效果如下:

总结
到此这篇关于react实现路由鉴权的实例详解的文章就介绍到这了,更多相关react路由鉴权内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论