完成对jwt的验证

master
P-Jhao 5 days ago
parent 7b63b99275
commit 4e676ea6bd

@ -2,3 +2,4 @@ DB_NAME = "uml"
DB_USER = "root" DB_USER = "root"
DB_PASSWORD = "123456" DB_PASSWORD = "123456"
DB_HOST = "localhost" DB_HOST = "localhost"
JWT_SECRET = "PJH"

@ -24,7 +24,7 @@ app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public'))); app.use(express.static(path.join(__dirname, 'public')));
//使用路由中间件 //使用路由中间件
app.use('/', userRouter); app.use('/user', userRouter);
// catch 404 and forward to error handler // catch 404 and forward to error handler
app.use(function (req, res, next) { app.use(function (req, res, next) {

@ -11,7 +11,11 @@ module.exports = sequelize.define("user", {
loginPwd: { loginPwd: {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: false allowNull: false
} },
sex: DataTypes.STRING,
brithday: DataTypes.STRING,
region: DataTypes.STRING,
number: DataTypes.STRING
}, { }, {
freezeTableName: true, freezeTableName: true,
createdAt: false, createdAt: false,

@ -9,4 +9,12 @@ module.exports.loginDao = async function ({
loginPwd loginPwd
} }
}) })
}
module.exports.updateUserDao = async function (loginInfo) {
return await userModel.update(loginInfo, {
where: {
loginId: loginInfo.loginId,
}
})
} }

107
package-lock.json generated

@ -14,6 +14,7 @@
"express": "~4.16.1", "express": "~4.16.1",
"http-errors": "~1.6.3", "http-errors": "~1.6.3",
"jade": "~1.11.0", "jade": "~1.11.0",
"jsonwebtoken": "^9.0.2",
"md5": "^2.3.0", "md5": "^2.3.0",
"morgan": "~1.9.1", "morgan": "~1.9.1",
"mysql2": "^3.11.4", "mysql2": "^3.11.4",
@ -160,6 +161,12 @@
"node": ">= 0.8" "node": ">= 0.8"
} }
}, },
"node_modules/buffer-equal-constant-time": {
"version": "1.0.1",
"resolved": "https://registry.npmmirror.com/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz",
"integrity": "sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==",
"license": "BSD-3-Clause"
},
"node_modules/bytes": { "node_modules/bytes": {
"version": "3.0.0", "version": "3.0.0",
"resolved": "https://registry.npmmirror.com/bytes/-/bytes-3.0.0.tgz", "resolved": "https://registry.npmmirror.com/bytes/-/bytes-3.0.0.tgz",
@ -398,6 +405,15 @@
"integrity": "sha512-iGCHkfUc5kFekGiqhe8B/mdaurD+lakO9txNnTvKtA6PISrw86LgqHvRzWYPyoE2Ph5aMIrCw9/uko6XHTKCwA==", "integrity": "sha512-iGCHkfUc5kFekGiqhe8B/mdaurD+lakO9txNnTvKtA6PISrw86LgqHvRzWYPyoE2Ph5aMIrCw9/uko6XHTKCwA==",
"license": "MIT" "license": "MIT"
}, },
"node_modules/ecdsa-sig-formatter": {
"version": "1.0.11",
"resolved": "https://registry.npmmirror.com/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz",
"integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==",
"license": "Apache-2.0",
"dependencies": {
"safe-buffer": "^5.0.1"
}
},
"node_modules/ee-first": { "node_modules/ee-first": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmmirror.com/ee-first/-/ee-first-1.1.1.tgz", "resolved": "https://registry.npmmirror.com/ee-first/-/ee-first-1.1.1.tgz",
@ -620,6 +636,34 @@
"jade": "bin/jade.js" "jade": "bin/jade.js"
} }
}, },
"node_modules/jsonwebtoken": {
"version": "9.0.2",
"resolved": "https://registry.npmmirror.com/jsonwebtoken/-/jsonwebtoken-9.0.2.tgz",
"integrity": "sha512-PRp66vJ865SSqOlgqS8hujT5U4AOgMfhrwYIuIhfKaoSCZcirrmASQr8CX7cUg+RMih+hgznrjp99o+W4pJLHQ==",
"license": "MIT",
"dependencies": {
"jws": "^3.2.2",
"lodash.includes": "^4.3.0",
"lodash.isboolean": "^3.0.3",
"lodash.isinteger": "^4.0.4",
"lodash.isnumber": "^3.0.3",
"lodash.isplainobject": "^4.0.6",
"lodash.isstring": "^4.0.1",
"lodash.once": "^4.0.0",
"ms": "^2.1.1",
"semver": "^7.5.4"
},
"engines": {
"node": ">=12",
"npm": ">=6"
}
},
"node_modules/jsonwebtoken/node_modules/ms": {
"version": "2.1.3",
"resolved": "https://registry.npmmirror.com/ms/-/ms-2.1.3.tgz",
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==",
"license": "MIT"
},
"node_modules/jstransformer": { "node_modules/jstransformer": {
"version": "0.0.2", "version": "0.0.2",
"resolved": "https://registry.npmmirror.com/jstransformer/-/jstransformer-0.0.2.tgz", "resolved": "https://registry.npmmirror.com/jstransformer/-/jstransformer-0.0.2.tgz",
@ -630,6 +674,27 @@
"promise": "^6.0.1" "promise": "^6.0.1"
} }
}, },
"node_modules/jwa": {
"version": "1.4.1",
"resolved": "https://registry.npmmirror.com/jwa/-/jwa-1.4.1.tgz",
"integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==",
"license": "MIT",
"dependencies": {
"buffer-equal-constant-time": "1.0.1",
"ecdsa-sig-formatter": "1.0.11",
"safe-buffer": "^5.0.1"
}
},
"node_modules/jws": {
"version": "3.2.2",
"resolved": "https://registry.npmmirror.com/jws/-/jws-3.2.2.tgz",
"integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==",
"license": "MIT",
"dependencies": {
"jwa": "^1.4.1",
"safe-buffer": "^5.0.1"
}
},
"node_modules/kind-of": { "node_modules/kind-of": {
"version": "3.2.2", "version": "3.2.2",
"resolved": "https://registry.npmmirror.com/kind-of/-/kind-of-3.2.2.tgz", "resolved": "https://registry.npmmirror.com/kind-of/-/kind-of-3.2.2.tgz",
@ -657,6 +722,48 @@
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==",
"license": "MIT" "license": "MIT"
}, },
"node_modules/lodash.includes": {
"version": "4.3.0",
"resolved": "https://registry.npmmirror.com/lodash.includes/-/lodash.includes-4.3.0.tgz",
"integrity": "sha512-W3Bx6mdkRTGtlJISOvVD/lbqjTlPPUDTMnlXZFnVwi9NKJ6tiAk6LVdlhZMm17VZisqhKcgzpO5Wz91PCt5b0w==",
"license": "MIT"
},
"node_modules/lodash.isboolean": {
"version": "3.0.3",
"resolved": "https://registry.npmmirror.com/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz",
"integrity": "sha512-Bz5mupy2SVbPHURB98VAcw+aHh4vRV5IPNhILUCsOzRmsTmSQ17jIuqopAentWoehktxGd9e/hbIXq980/1QJg==",
"license": "MIT"
},
"node_modules/lodash.isinteger": {
"version": "4.0.4",
"resolved": "https://registry.npmmirror.com/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz",
"integrity": "sha512-DBwtEWN2caHQ9/imiNeEA5ys1JoRtRfY3d7V9wkqtbycnAmTvRRmbHKDV4a0EYc678/dia0jrte4tjYwVBaZUA==",
"license": "MIT"
},
"node_modules/lodash.isnumber": {
"version": "3.0.3",
"resolved": "https://registry.npmmirror.com/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz",
"integrity": "sha512-QYqzpfwO3/CWf3XP+Z+tkQsfaLL/EnUlXWVkIk5FUPc4sBdTehEqZONuyRt2P67PXAk+NXmTBcc97zw9t1FQrw==",
"license": "MIT"
},
"node_modules/lodash.isplainobject": {
"version": "4.0.6",
"resolved": "https://registry.npmmirror.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz",
"integrity": "sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA==",
"license": "MIT"
},
"node_modules/lodash.isstring": {
"version": "4.0.1",
"resolved": "https://registry.npmmirror.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz",
"integrity": "sha512-0wJxfxH1wgO3GrbuP+dTTk7op+6L41QCXbGINEmD+ny/G/eCqGzxyCsh7159S+mgDDcoarnBw6PC1PS5+wUGgw==",
"license": "MIT"
},
"node_modules/lodash.once": {
"version": "4.1.1",
"resolved": "https://registry.npmmirror.com/lodash.once/-/lodash.once-4.1.1.tgz",
"integrity": "sha512-Sb487aTOCr9drQVL8pIxOzVhafOjZN9UU54hiN8PU3uAiSV7lx1yYNpbNmex2PK6dSJoNTSJUUswT651yww3Mg==",
"license": "MIT"
},
"node_modules/long": { "node_modules/long": {
"version": "5.2.3", "version": "5.2.3",
"resolved": "https://registry.npmmirror.com/long/-/long-5.2.3.tgz", "resolved": "https://registry.npmmirror.com/long/-/long-5.2.3.tgz",

@ -13,6 +13,7 @@
"express": "~4.16.1", "express": "~4.16.1",
"http-errors": "~1.6.3", "http-errors": "~1.6.3",
"jade": "~1.11.0", "jade": "~1.11.0",
"jsonwebtoken": "^9.0.2",
"md5": "^2.3.0", "md5": "^2.3.0",
"morgan": "~1.9.1", "morgan": "~1.9.1",
"mysql2": "^3.11.4", "mysql2": "^3.11.4",

@ -1,24 +1,50 @@
var express = require('express'); var express = require('express');
var router = express.Router(); var router = express.Router();
const { const {
loginService loginService,
updateUserInfoService
} = require("../services/userService") } = require("../services/userService")
const { const {
formatResponse formatResponse,
analysisToken
} = require("../utils/tools") } = require("../utils/tools")
/* GET home page. */ /* GET home page. */
router.post('/login', async function (req, res, next) { router.post('/login', async function (req, res, next) {
//移交service处理 //移交service处理
console.log(1)
const result = await loginService(req.body) const result = await loginService(req.body)
if (result.token) { if (result.token) {
//有token登陆成功 //有token登陆成功
res.setHeader("authentication", result.token)
res.send(formatResponse(0, "", result.data)) res.send(formatResponse(0, "", result.data))
} else { } else {
throw new Error("账号或密码错误") // throw new Error("账号或密码错误")
res.send(formatResponse(401, "账号或密码错误", null))
} }
}); });
router.get("/whoami", async function (req, res, next) {
//获取token,然后进行解析
const {
loginId,
sex,
brithday,
region,
number
} = analysisToken(req.get("authorization"))
res.send(formatResponse(0, "", {
loginId,
sex,
brithday,
region,
number
}))
})
router.put("/", async function (req, res, next) {
res.send(formatResponse(0, "", await updateUserInfoService(req.body)))
})
module.exports = router; module.exports = router;

@ -1,27 +1,42 @@
const md5 = require('md5'); const md5 = require('md5');
const { const {
loginDao loginDao,
updateUserDao
} = require("../dao/userDao") } = require("../dao/userDao")
const jwt = require("jsonwebtoken");
const {
getJwtToken
} = require('../utils/tools');
module.exports.loginService = async function (loginInfo) { module.exports.loginService = async function (loginInfo) {
//将明文密码转换为加密密码 //将明文密码转换为加密密码
loginInfo.loginPwd = md5(loginInfo.loginPwd); loginInfo.loginPwd = md5(loginInfo.loginPwd);
let userInfo = await loginDao(loginInfo) let userInfo = await loginDao(loginInfo)
if (userInfo) { if (userInfo) {
userInfo = { const {
id: userInfo.id, loginPwd,
loginId: userInfo.loginId id,
} ...data
console.log(userInfo) } = userInfo.dataValues
//添加token后返回 //添加token后返回
let token = "" let token = ""
token = "123" token = getJwtToken(data)
return { return {
token, token,
data: userInfo data
} }
} }
return { return {
data: userInfo data: userInfo
} }
}
module.exports.updateUserInfoService = async function (loginInfo) {
const data = loginInfo
data.loginPwd = md5(data.loginPwd)
await updateUserDao(data)
const {
loginPwd,
...result
} = loginInfo
return result
} }

@ -1,7 +1,21 @@
const jwt = require("jsonwebtoken")
const md5 = require("md5")
module.exports.formatResponse = function (code, msg, data) { module.exports.formatResponse = function (code, msg, data) {
return { return {
code, code,
msg, msg,
data data
} }
}
module.exports.analysisToken = function (token) {
return jwt.verify(token.split(" ")[1], md5(process.env.JWT_SECRET))
}
module.exports.getJwtToken = function (payload) {
let loginPeriod = 1; //默认记住一天
return jwt.sign(payload, md5(process.env.JWT_SECRET), {
expiresIn: 60 * 60 * 24 * loginPeriod
})
} }
Loading…
Cancel
Save