您的位置:首页 > Web前端 > React

如何用React-Router进行页面权限管理

2017-11-17 00:00 429 查看

前言

在一个复杂的SAP应用中,我们可能需要根据用户的角色控制用户进行页面的权限,甚至在用户进入系统之前就进行权限的控制。本文就此一权限控制进行讨论。本文假设读者了解React和React-Router的相关使用。

从传统的Router开始

一个传统的路由大概长下边这个样式,这是没有添加任何权限限制的。

export default (store) => { const history = syncHistoryWithStore(hashHistory, store); return ( <<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">Router history={history}><<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">Route path="/"component={AppRoot} > <<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">IndexRoute component={IndexPage} /> <<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">Route path="photo" component={PhotoPage} /> <<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">Route path="info" component={InfoPage} /> </<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">Route> {/ <<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">Redirect path="" to="/error" /> */}</<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">Router> ) }

这里一共有3个页面 IndexPage, PhotoPage,InfoPage。

我有几张阿里云幸运券分享给你,用券购买或者升级阿里云相应产品会有特惠惊喜哦!把想要买的产品的幸运券都领走吧!快下手,马上就要抢光了。

添加第一个权限

假设我们需要在用户进入PhotoPage之前需要验证用户是否有权限,根据store的的一个状态去判断。

先添加如下一个函数

const authRequired = (nextState, replace) => { // Now you can access the store object here. conststate = store.getState(); if (state.admin != 1) { replace('/'); } };

函数里我们判断了state的admin是否等于1,否则跳转到首页。

然后在Route添加 onEnter={authRequired} 属性

[code]<<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">Route path="photo"component={PhotoPage} onEnter={authRequired} />
通过以上,就完成了第一个权限的添加

进入系统之前就进行权限控制

如果需要在进入系统之前就进行权限控制,那么就需要改变一下策略。
比如上边的例子,加入state的admin并未加载,那么就需要在上一层的route进行数据加载

首先添加一个加载数据的函数

function loadData(nextState, replace, callback) { let unsubscribe; function onStateChanged() { conststate = store.getState(); if (state.admin) { unsubscribe(); callback(); } } unsubscribe = store.subscribe(onStateChanged); store.dispatch(actions.queryAdmin()); }

接着再修改一下Router

<<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">Router history={history}>[/code] <<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">Routepath="/" component={AppRoot} onEnter={loadData}> <<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">IndexRoute component={IndexPage} /> <<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">Route path="photo" component={PhotoPage} onEnter={authRequired} /> <<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">Route path="info" component={InfoPage} /> </<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">Route> </<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">Router>[/code]
这样在进入下边之前,就会先进行数据加载。
通过以上简单几步,一个完整的权限控制链就完成了.

原文链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: