如何用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]
这样在进入下边之前,就会先进行数据加载。
通过以上简单几步,一个完整的权限控制链就完成了.
原文链接
相关文章推荐
- React-Router如何进行页面权限管理的方法
- 使用Apache shiro进行权限管理时如何对同一个URL配置多个角色的或关系
- 前端如何进行用户权限管理
- 回答客户对通用权限的疑问,页面里如何判断权限?如何实现上级能管理下级?基础表用哪个?
- 通用权限管理系统如何进行角色判断
- 回答客户对通用权限的疑问,页面里如何判断权限?如何实现上级能管理下级?基础表用哪个?
- win8.1如何获取最高管理权限对系统进行完全掌控
- 回答客户对通用权限的疑问,页面里如何判断权限?如何实现上级能管理下级?基础表用哪个?
- 如何让svn更加智能的进行权限管理
- 通过一个用户管理实例学习路由react-router-dom知识
- Git Repo Gerrit方式下如何进行代码读权限控制
- .net 运用二进制位运算进行权限管理
- 详解React-Router中Url参数改变页面不刷新的解决办法
- 如何有效的进行--时间管理----阿冬专栏
- react-router 与react-reduct 配合使用时,页面不刷新问题
- 如何使用AngularJs打造权限管理系统【简易型】
- 如何进入tomcat的管理页面
- React路由管理 —— React Router 总结
- 如何获取计算机管理组的权限
- 【react-router】从Link组件和a标签的区别说起,react-router如何实现导航并优化DOM性能?