layui跳转页面如何对token进行校验
-
在跳转页面之前,先将token存储到cookie中或者localStorage中。
-
在跳转页面时,将token作为参数传递到目标页面。
-
在目标页面中,获取传递过来的token,并根据业务需要进行校验。可以使用jwt等方式进行校验。
-
如果校验失败,可以进行相应的处理,例如跳转到登录页面或者提示用户无权访问。
示例代码:
// 存储token localStorage.setItem('token', 'xxxxx');
// 跳转页面 window.location.href = 'target.html?token=' + localStorage.getItem('token');
// 目标页面 var token = getUrlParam('token'); if (!verifyToken(token)) { window.location.href = 'login.html'; }
function getUrlParam(name) { var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i'); var r = window.location.search.substr(1).match(reg); if (r != null) { return unescape(r[2]); } return null; }
function verifyToken(token) { // 进行校验 return true; }
原文地址: http://www.cveoy.top/t/topic/bjHp 著作权归作者所有。请勿转载和采集!