Routing and permission verification, how to deal with routing and permission mapping in participating projects?
The routing processing logic analysis is as follows:
Access /xxx, router.beforeEachwill make the global navigation guard, from cookieacquiring the tokenjudge tokenis present.
If tokennot present, it is determined whether the route of the whitelist. If it is in the whitelist, access the route /xxx. If it is not in the whitelist, access the route /login?redirect=/xxx.
If tokenthere is, it determines whether the route /login. If the route is yes /login, then redirect to /. If the route is not /login, get the user's role, dynamically generate the route, and replaceaccess the route in a pattern /xxx. If an abnormal situation occurs, reset it tokenand visit the router /login?redirect=/xxx.
Analysis of routing scenarios, common in middle and back-end routing, are as follows:
Obtained token:
Visit /login, redirect to/
Visit /login?redirect=/xxx, redirect to/xxx
Access /loginroutes other than direct access/xxx
Not obtained token:
Access /login, direct access/login
Access /loginroutes other than such as access /dashboard, access path for the real /login?redirect=%2Fdashboard, will sign redirected to/dashboard
Second, the realization of routing and permission verification
The global navigation guard router.beforeEach in permission.js , the code is as follows: