1.前端vue白名单过滤
router.beforeEach((to, from, next) => {
//设置所有人都能访问的页面白名单
let white = ["/login","/manage"]
//取出用户登录存储的资源权限
let pathlist = JSON.parse(localStorage.getItem("vurl"))
//判断是否有用户登录存储的资源权限
if(pathlist){
//加入白名单
for(var i=0;i<=pathlist.length;i++){
white.push(pathlist[i])
}
//判断访问路径是否在白名单内
if(white.includes(to.path)){
next()
}else{
alert("您没有权限")
}
}else {
if (white.includes(to.path)) {
next()
} else {
alert("您没有权限")
}
}
})
2.后端python白名单过滤(flask项目)
//使用钩子函数before_request(每次请求之前访问)
@app.before_request
def before_request():
//设置共有白名单
white = ["/login"]
//判断后端接口资源是否在内名单内
if request.path not in white:
try:
token = request.headers.get("Authorization")
except:
return jsonify({"code":403,"msg":"no token"})
if token:
//token解析出用户信息(此处使用的自己封装的方法)
user = token1.decode_jwt(token,"123")
if user:
reslist = acl.get_reslist(user["uid"])
for i in reslist:
white.append(i)
if request.path not in white:
return jsonify({"code":403,"msg":"fail"})
else:
return jsonify({"code":40311,"msg":"token error"})
本文介绍了如何使用Vue.js前端和Flask后端实现基于RBAC的权限管理,特别关注白名单过滤的实现细节,包括前端Vue.js的白名单路由过滤和后端Python Flask项目的白名单策略设置。
1550

被折叠的 条评论
为什么被折叠?



