
7

egg-jwt的使用 - 无聊猿
source link: https://www.cnblogs.com/wuliaoyuan/p/16010419.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

npm install egg-jwt --save
// config/config.default.js
config.jwt = {
secret: 'zidingyi', // 自定义 token 的加密条件字符串
};
// config/plugin.js
jwt: {
enable: true,
package: 'egg-jwt',
},
- 生成token
// login.js
const token = ctx.app.jwt.sign({
...ctx.request.body,
}, this.app.config.jwt.secret, {
expiresIn: '600m', // 时间根据自己定,具体可参考jsonwebtoken插件官方说明
});
- 验证token
验证token通常会写一个中间件,在需要验证的路由里引用中间件即可
'use strict';
module.exports = options => {
return async function jwt(ctx, next) {
// 拿到传会数据的header 中的token值
const token = ctx.request.header.authorization;
const method = ctx.method.toLowerCase();
// 当前请求时get请求,执行接下来的中间件
if (method === 'get') {
await next();
// 当前token值不存在的时候
} else if (!token) {
if (ctx.path === '/api/v1/register' || ctx.path === '/api/v1/login/account') {
await next();
} else {
ctx.throw(401, '未登录, 请先登录');
ctx.body = {
code: 50008,
};
}
} else { // 当前token值存在
let decode;
// 解码token
decode = await ctx.app.jwt.verify(token, options.secret, (err, decoded) => {
if (err) {
if (err.name === 'TokenExpiredError') { // token过期
return 'TokenExpiredError';
} else if (err.name === 'JsonWebTokenError') { // 无效的token
return 'JsonWebTokenError';
}
} else {
return decoded;
}
});
if (decode === 'TokenExpiredError') {
ctx.body = {
code: 401,
msg: '登录过期,请重新登录',
};
return;
}
if (decode === 'JsonWebTokenError') {
ctx.body = {
code: 50012,
msg: 'token无效,请重新登录',
};
return;
}
await next();
}
};
};
- 使用中间件鉴权
// router.js
const jwt = app.middleware.jwt(app.config.jwt);
router.post('/getHousePropertyFee', jwt, controller.getHousePropertyFee.index);
- 获取token
login登录成功后,后台会把token发送给前端,前端可以将token缓存至cookie中。
- 发送token
封装axios,每次请求去cookie中拿token,并根据后端返回的状态码给予用户提示,比如:token已过期等。。。
// 封装axios请求
service.interceptors.request.use(
config => {
if (store.getters.token) {
config.headers['authorization'] = getToken()
}
return config
},
error => {
// do something with request error
console.log(error) // for debug
return Promise.reject(error)
}
)
// axios 拦截错误码
if (res.code === 50016) {
MessageBox.confirm(res.msg, '提示', {
showClose: false,
showCancelButton: false,
closeOnClickModal: false,
closeOnPressEscape: false,
confirmButtonText: '确定',
type: 'warning'
})
}
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK