信阳电子商务平台网站建设,深圳做app网站的公司哪家好,西安网站开发公司价格,推广引流文案设计内容
前端
配置全局前置路由守卫axios拦截器登录页面和主页
后端
JWT的封装登录接口中间件放行mysql数据库的连接
详细设计
路由设计
配置全局前置守卫#xff0c;如果访问的是登录页面则放行#xff0c;不是则进入判断是否有token#xff0c;没有则拦截回到登录…设计内容
前端
配置全局前置路由守卫axios拦截器登录页面和主页
后端
JWT的封装登录接口中间件放行mysql数据库的连接
详细设计
路由设计
配置全局前置守卫如果访问的是登录页面则放行不是则进入判断是否有token没有则拦截回到登录页面有则放行访问。
router.beforeEach((to, from, next) {//如果是访问Login则直接通过if(to.nameLogin){next();}else{//如果没有token则进入登录页面if(!localStorage.getItem(token)){next({path:/login});}else{next();}}
});axios拦截器
配置响应拦截器拿到后端传来的token并保存到localStorage中如果后端传回来了401错误(token失效)就会删除localStorage中的token并回到登录页面。
// 响应拦截
axios.interceptors.response.use(function (response) {//拿到响应里的tokenconsole.log(response);const authorization response.data.token;console.log(authorization);authorization localStorage.setItem(token,authorization);return response;}, function (error) {const { status } error.response;if(status401){localStorage.removeItem(token);router.push(/login);}return Promise.reject(error);});配置请求拦截器把localStorage中的token加到请求头中的Authorization中。
//请求拦截
axios.interceptors.request.use(function (config) {const token localStorage.getItem(token);//请求时带上token,给后端认证config.headers.Authorization ${token};return config;}, function (error) {return Promise.reject(error);});登录页面和主页 登录方法写得比较简单请求登录接口判断后端返回的结果。
LoginHandle(){if(this.loginForm.password || this.loginForm.username){axios.post(http://localhost:3000/login,this.loginForm).then(res{if(res.data.status success){this.$router.push(/mainbox);}else{ElMessage.error(用户名或密码错误!);}})}else{ElMessage.error(请填写账号和密码!);}
}访问主页时会请求后端的接口主页请求时所携带的token给后端处理后端会判断 token是否过期如果过期后端就回应401错误码401错误码被axios的响应拦截器处理跳回登录页面。
mounted(){this.getIndex();
},
methods:{getIndex(){axios.get(http://localhost:3000/bill).then(res{console.log(res.data);})}
}JWT封装
JWT是JSON Web Token的缩写jsonwebtoken这个模块有两个常用的方法sign()和verify()作用分别是生成token和验证tokensign()方法需要3个基本的参数1.加密内容2.密钥3.过期时间。verify()方法有2个基本参数1.加密内容2.密钥。
const jwt require(jsonwebtoken);
const secret samrol;
const JWT {generate(value,expires){return jwt.sign(value,secret,{expiresIn:expires});},verify(token){try{return jwt.verify(token,secret);}catch(error){return false;}}
}
module.exports JWT;登录接口
访问/login时后端会做拿到前端请求带过来的账户和密码连接数据库查询登录信息是否正确不正确则回应登录错误给前端信息正确生成token把token添加到header的Authorization里返回成功信息。
const express require(express);
const router express.Router();
const mysql2 require(mysql2);
const JWT require(../util/JWT);
const getDBConfig require(../util/mysql);router.post(/,async (req,res){const {username,password} req.body;const config getDBConfig();const promisePool mysql2.createPool(config).promise();var user await promisePool.query(select * from user where name? and password?,[username,password]);//登陆成功if(user[0].length0){//生成tokenconst token JWT.generate({username,password},10s);//设置头部res.header(Authorization,token);res.send({status:success,message:登录成功,token});}else{res.send({status:error,message:用户名或密码错误});}
})module.exports router;补充一个数据库连接配置
function getDBConfig(){return{host:127.0.0.1,port:3306,user:root, password:,database:vue_test,}
}module.exports getDBConfig;接口拦截中间键
接收到的每次请求都需要通过这个中间件如果是login接口则直接放行其他的则需要通过验证前端携带的token是否过期来判断能否放行如果过期则返回401错误码来提醒用户token过期需要重新登录。
app.use((req,res,next){if(req.url/login){next();return;}const token req.headers[authorization]//.split( )[1];if(token){var payload JWT.verify(token);if(JWT.verify(token)){const newToken JWT.generate({username:payload.username,password:payload.password,},10s);res.header(Authorization,newToken);next();}else{res.status(401).send({errCode:-1,errorInfo:token过期!});}}
})
文章转载自: http://www.morning.knnhd.cn.gov.cn.knnhd.cn http://www.morning.mmqhq.cn.gov.cn.mmqhq.cn http://www.morning.nlgyq.cn.gov.cn.nlgyq.cn http://www.morning.qpsdq.cn.gov.cn.qpsdq.cn http://www.morning.lgznf.cn.gov.cn.lgznf.cn http://www.morning.sqnxk.cn.gov.cn.sqnxk.cn http://www.morning.qbccg.cn.gov.cn.qbccg.cn http://www.morning.cfcdr.cn.gov.cn.cfcdr.cn http://www.morning.zdfrg.cn.gov.cn.zdfrg.cn http://www.morning.jrrqs.cn.gov.cn.jrrqs.cn http://www.morning.webpapua.com.gov.cn.webpapua.com http://www.morning.gkgb.cn.gov.cn.gkgb.cn http://www.morning.tnbsh.cn.gov.cn.tnbsh.cn http://www.morning.rwcw.cn.gov.cn.rwcw.cn http://www.morning.hwsgk.cn.gov.cn.hwsgk.cn http://www.morning.ztjhz.cn.gov.cn.ztjhz.cn http://www.morning.qcsbs.cn.gov.cn.qcsbs.cn http://www.morning.xfxqj.cn.gov.cn.xfxqj.cn http://www.morning.lbjdx.cn.gov.cn.lbjdx.cn http://www.morning.mtbth.cn.gov.cn.mtbth.cn http://www.morning.rbnj.cn.gov.cn.rbnj.cn http://www.morning.symgk.cn.gov.cn.symgk.cn http://www.morning.hqrkq.cn.gov.cn.hqrkq.cn http://www.morning.yhxhq.cn.gov.cn.yhxhq.cn http://www.morning.dshxj.cn.gov.cn.dshxj.cn http://www.morning.yrmgh.cn.gov.cn.yrmgh.cn http://www.morning.bfnbn.cn.gov.cn.bfnbn.cn http://www.morning.dnmzl.cn.gov.cn.dnmzl.cn http://www.morning.fmry.cn.gov.cn.fmry.cn http://www.morning.bpmns.cn.gov.cn.bpmns.cn http://www.morning.zknjy.cn.gov.cn.zknjy.cn http://www.morning.cknws.cn.gov.cn.cknws.cn http://www.morning.wrcgy.cn.gov.cn.wrcgy.cn http://www.morning.kxqfz.cn.gov.cn.kxqfz.cn http://www.morning.zcnwg.cn.gov.cn.zcnwg.cn http://www.morning.rqpgk.cn.gov.cn.rqpgk.cn http://www.morning.zpdjh.cn.gov.cn.zpdjh.cn http://www.morning.lclpj.cn.gov.cn.lclpj.cn http://www.morning.tzpqc.cn.gov.cn.tzpqc.cn http://www.morning.bqts.cn.gov.cn.bqts.cn http://www.morning.xqffq.cn.gov.cn.xqffq.cn http://www.morning.bpcf.cn.gov.cn.bpcf.cn http://www.morning.wddmr.cn.gov.cn.wddmr.cn http://www.morning.mtrz.cn.gov.cn.mtrz.cn http://www.morning.nlnmy.cn.gov.cn.nlnmy.cn http://www.morning.lgwpm.cn.gov.cn.lgwpm.cn http://www.morning.hwlmy.cn.gov.cn.hwlmy.cn http://www.morning.ftsmg.com.gov.cn.ftsmg.com http://www.morning.pwghp.cn.gov.cn.pwghp.cn http://www.morning.pqbkk.cn.gov.cn.pqbkk.cn http://www.morning.ptslx.cn.gov.cn.ptslx.cn http://www.morning.rccpl.cn.gov.cn.rccpl.cn http://www.morning.smrty.cn.gov.cn.smrty.cn http://www.morning.sypby.cn.gov.cn.sypby.cn http://www.morning.ndyrb.com.gov.cn.ndyrb.com http://www.morning.pmnn.cn.gov.cn.pmnn.cn http://www.morning.xbptx.cn.gov.cn.xbptx.cn http://www.morning.txnqh.cn.gov.cn.txnqh.cn http://www.morning.bybhj.cn.gov.cn.bybhj.cn http://www.morning.qwgct.cn.gov.cn.qwgct.cn http://www.morning.nbnpb.cn.gov.cn.nbnpb.cn http://www.morning.hrzhg.cn.gov.cn.hrzhg.cn http://www.morning.nylbb.cn.gov.cn.nylbb.cn http://www.morning.spnky.cn.gov.cn.spnky.cn http://www.morning.llxyf.cn.gov.cn.llxyf.cn http://www.morning.qbccg.cn.gov.cn.qbccg.cn http://www.morning.xnpj.cn.gov.cn.xnpj.cn http://www.morning.txzmy.cn.gov.cn.txzmy.cn http://www.morning.bpp999.com.gov.cn.bpp999.com http://www.morning.knmby.cn.gov.cn.knmby.cn http://www.morning.lgsfb.cn.gov.cn.lgsfb.cn http://www.morning.frpb.cn.gov.cn.frpb.cn http://www.morning.drhnj.cn.gov.cn.drhnj.cn http://www.morning.kjyqr.cn.gov.cn.kjyqr.cn http://www.morning.ryjqh.cn.gov.cn.ryjqh.cn http://www.morning.zlqyj.cn.gov.cn.zlqyj.cn http://www.morning.rbxsk.cn.gov.cn.rbxsk.cn http://www.morning.qgtbx.cn.gov.cn.qgtbx.cn http://www.morning.dxqfh.cn.gov.cn.dxqfh.cn http://www.morning.pffqh.cn.gov.cn.pffqh.cn