Json Web Token (JWT) is a JSON-based development standard (RFC) implemented for the transfer of declarations between network application environments 7519), the token is designed to be compact and secure, especially suitable for single sign-on (SSO) scenarios in distributed sites. The JWT declaration is generally used to pass authenticated user identity information between the identity provider and the service provider to obtain resources from the resource server, and to add some additional declaration information necessary for other business logic. The token can also be used directly for authentication or can be encrypted. Eggmiddleware JWT node- JsonWebToken

Create a new jwt.js file under the Middleware file

‘use strict’ const fs = require(‘fs’) const path = require(‘path’) const JWT = require(‘ jsonWebToken ‘

module.exports = (options, app) => { return async function userInterceptor(ctx, Next) {let authToken = ctx.header.authorization // Obtain the authorization if (authToken) {authToken = Authtoken. substring(7) const res = verifyToken(authToken) // Decrypted Token if (res.corpid && res.userId) {// If you need to restrict single-end login, disable a token during use, or change the permission of the token. That is, once the JWT is signed, Const redis_token = await app.redis. Get (‘loginToken’).get(res.corpid + res.userid) // If (authToken === redis_token) {ctx.locals. Corpid = res.corpid ctx.locals. Userid = res.userid await next()} Else {ctx.body = {code: 50012, MSG: ‘Your account is already registered elsewhere’}}} else {ctx.body = {code: 50012, MSG: ‘Your account is already registered elsewhere’}}} else {ctx.body = {code: 50012, MSG: Else {ctx.body = {code: 50008, MSG: ‘Please login again’}}}}

Function verifyToken(token) {const cert = fs.readfilesync (path.join(__dirname, ‘.. // public/rsa_public_key.pem’) // public/rsa_public_key.pem’) // public/rsa_public_key.pem’) // public/rsa_public_key.pem’) [ ‘RS256’ ] }) || {} const { exp } = result, current = Math.floor(Date.now() / 1000) if (current <= exp) res = result.data || {} } catch (e) { console.log(e) } Return res} Add the following configuration to config.default.js to enable and configure the middleware

// Use middleware config. Middleware = [‘ JWT ‘]

Config. JWT = {enable: true, ignore: [‘/ API /v1/test/’, ‘/public/’], // Which requests do not require authentication}

Exports = app => {const JWT = app.middleware. JWT (); // Exports = app => {const JWT = app.middleware. app.router.get(‘/api/v1/test/’, jwt, app.controller.test.test); }; Token generation suggestions are written in the helper for easy invocation

loginToken(data, expires = 7200) { const exp = Math.floor(Date.now() / 1000) + expires const cert = fs.readFileSync(path.join(__dirname, ‘.. Const token = jwt.sign({data, exp}, cert, {algorithm: ‘RS256’}) return token} calls the token generation method

const token = ctx.helper.loginToken({ corpid: usersData.corpid, userid: Get (‘loginToken’).set(usersdata. corpid + usersdata. userid, token, usersdata. userid, token, usersdata. userid, token, usersdata. userid, token, usersdata. userid, token, ‘ex’, 7200) // Save to redis ctx.body = {data: {token, expires: this.config.login_token_time}, code: 1, MSG: ‘Login successful’} // return front-end using Authorization = ‘Bearer ‘+ Token Bearer followed by Spaces

Example: // Request interceptor in AXIos

service.interceptors.request.use(config => { if (store.getters.token) { config.headers[‘Authorization’] = `Bearer ${getToken()}` } return config }, error => { console.log(error) Promise.reject(error) })























END