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; }
原文地址: https://www.cveoy.top/t/topic/mLPl 著作权归作者所有。请勿转载和采集!