当前位置: 首页 > news >正文

企业做不做网站的坏处上海搜索推广

企业做不做网站的坏处,上海搜索推广,wordpress 导航页面模板,无忧网站建设报价expressmySql实现用户注册、登录和身份认证 注册 注册时需要对用户密码进行加密入库,提高账户的安全性。用户登录时再将密码以相同的方式进行加密,再与数据库中存储的密码进行比对,相同则表示登录成功。 安装加密依赖包bcryptjs cnpm insta…

express+mySql实现用户注册、登录和身份认证

注册

注册时需要对用户密码进行加密入库,提高账户的安全性。用户登录时再将密码以相同的方式进行加密,再与数据库中存储的密码进行比对,相同则表示登录成功。

安装加密依赖包bcryptjs

cnpm install -S bcryptjs

在注册接口中添加加密功能

// 引入对密码进行加密的包
const bcryptjs = require("bcryptjs");
class User {register(req, res) {let { username, nick_name,  password } = req.body;// 先查找注册的用户名是否在数据库中已存在const sql = "select * from sys_user where user_name=?";pool.query(sql, username, (err, results) => {if (err) return res.sendError(err);// 找到了要注册的用户名if (results.length >= 1) return res.sendError("当前用户名已被占用!");// 对密码进行加密,第二个参数可以提高密码的安全性为任意数字const password1 = bcryptjs.hashSync(password, 10);const SQl = `Insert into sys_user (user_name,password,nick_name) values('${username}', '${password1}', '${nick_name}')`pool.query(SQl, (err, data) => {if (err) return res.sendError(err);if (data.affectedRows !== 1) {res.sendError('用户注册失败"');} else {res.sendSuccess(data);}})})}
}

效果展示

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

登录

安装加密依赖包jsonwebtoken

jsonwebtokenuaokeyi生成用户登录需要的token信息。

cnpm install -S jsonwebtoken

增加全局token配置文件

在项目根目录的config文件夹下新增taken.js文件,并加入如下配置。

// 全局的配置文件
module.exports = {// 设置token加密和解密用到的密钥jwtSecretKey: 'qwertyuiop',// 设置token的有效期expiresIn: '10h',
}

在这里插入图片描述

在登录接口中返回taken信息

// 导入jsonwebtoken
const jwt = require("jsonwebtoken");
// 导入全局配置文件
const taken = require("../config/taken");
class User {login(req, res) {const { username, password } = req.body;// 先查找用户名是否在数据库中,定义sql语句const sql = "select * from sys_user where user_name=?";pool.query(sql, username, (err, results) => {if (err) return res.sendError(err);if (results.length !== 1) return res.sendError("当前用户不存在!");// 比较密码  compareSync(客户端的密码,数据库中存储的经过加密后的密码)会返回true或falseconst compareResult = bcryptjs.compareSync(password, results[0].password);if (!compareResult) {return res.sendError("用户密码错误,登录失败!");}// 密码比对正确,在服务端根据用户信息(用户密码需置空)生成token信息const user = { ...results[0], password: "" };// 对用户的信息进行加密,生成token字符串const tokenStr = jwt.sign(user, taken.jwtSecretKey, {expiresIn: taken.expiresIn,});// 调用res.send将token响应给客户端res.sendSuccess("Bearer " + tokenStr)})}
}

效果展示

在这里插入图片描述

配置系统白名单

白名单是指那些接口不需要提供token信息。

安装解析token的依赖包express-jwt

express-jwt 包需要使用和生成token时相同的密钥。

cnpm install -S express-jwt

修改app.js文件设置系统白名单

// 在路由之前配置解析token的中间件
const { expressjwt: jwt } = require("express-jwt")
// 解析token时需要token的密钥
const taken = require("./config/taken");
// 定义中间件,
// .unless指定哪些接口不需要进行token身份认证(过滤掉swagger页面和login接口)
app.use(jwt({ secret: taken.jwtSecretKey, algorithms: ["HS256"] }).unless({path: [/^\/api-docs/, '/user/login', '/user/register'],})
)

效果展示

在这里插入图片描述
在这里插入图片描述

在其他接口中通过token获取具体的用户信息

在接口中通过req.auth中获取:具体代码如下:

class mineSeam {getAll(req, res) {console.log(req.auth)}
}

在这里插入图片描述

app.js全部代码如下

// 引入express
const express = require("express");
const path = require('path');
const router = require('./routes/index.js');// 创建服务器的实例对象
const app = express();// 配置解析表单数据的中间件,内置中间件只能解析application/x-www-form-urlencoded格式的数据
app.use(express.urlencoded({ extended: false }));// 搭建静态文件服务
app.use(express.static(path.join(__dirname, 'public')));// 引入swagger配置项
const swaggerSpec = require('./config/swagger')
app.get('/swagger.json', function(req, res) {res.setHeader('Content-Type', 'application/json');res.send(swaggerSpec);
});/*** 在路由之前封装res.send()*/
app.use((req, res, next) => {// 定义一个输出的函数res.sendError = function (err) {res.send({code: 400,msg: err instanceof Error ? err.message : err})}// 定义一个输出的函数res.sendSuccess = function (data = null) {res.send({code: 200,msg: '成功',data})}next();
})// 在路由之前配置解析token的中间件
const { expressjwt: jwt } = require("express-jwt");
// 解析token需要token的密钥
const taken = require("./config/taken");
// 定义中间件,需要哪个密钥解析
// algorithms:设置jwt的算法
// .unless指定哪些接口不需要进行token身份认证
app.use(jwt({ secret: taken.jwtSecretKey, algorithms: ["HS256"] }).unless({path: [/^\/api-docs/, '/user/login', '/user/register'],})
)// 引入路由
router(app);// 引入校验规则的包,在定义错误级别的中间件时会用到
const joi = require('joi')
// 在所有路由下面调用错误级别的中间件
app.use((err, req, res, next) => {// 验证失败导致的错误if (err instanceof joi.ValidationError) return res.sendError(err);// 未知的错误res.sendError(err);next();
})// 启动服务器,3007为端口号,选择一个空闲的端口号
app.listen(3007, () => {console.log("Server running at http://127.0.0.1:3007");
})

参考链接

链接1


文章转载自:
http://dinncocaloricity.wbqt.cn
http://dinncothermostatic.wbqt.cn
http://dinncomarrow.wbqt.cn
http://dinncoharoosh.wbqt.cn
http://dinncosinuous.wbqt.cn
http://dinncoparashot.wbqt.cn
http://dinncochloropicrin.wbqt.cn
http://dinncofucked.wbqt.cn
http://dinncomeagerly.wbqt.cn
http://dinncomarsha.wbqt.cn
http://dinncosemiotics.wbqt.cn
http://dinncoepigynous.wbqt.cn
http://dinncoforeseeingly.wbqt.cn
http://dinncomammonite.wbqt.cn
http://dinncovijayawada.wbqt.cn
http://dinncoallegoric.wbqt.cn
http://dinncoetape.wbqt.cn
http://dinncoreverential.wbqt.cn
http://dinncotrinitrotoluene.wbqt.cn
http://dinncointernal.wbqt.cn
http://dinncoanabolite.wbqt.cn
http://dinncoguru.wbqt.cn
http://dinncoturcologist.wbqt.cn
http://dinncooutargue.wbqt.cn
http://dinncowidowerhood.wbqt.cn
http://dinncochubbiness.wbqt.cn
http://dinncomyelogram.wbqt.cn
http://dinncounbeaten.wbqt.cn
http://dinncoproletariat.wbqt.cn
http://dinncocutinize.wbqt.cn
http://dinncodigs.wbqt.cn
http://dinncoreclinate.wbqt.cn
http://dinncomilometer.wbqt.cn
http://dinncoheliology.wbqt.cn
http://dinncoescort.wbqt.cn
http://dinncoecumenopolis.wbqt.cn
http://dinncounhealthily.wbqt.cn
http://dinncoenneasyllabic.wbqt.cn
http://dinncounsuccess.wbqt.cn
http://dinncoviselike.wbqt.cn
http://dinncoinquilinism.wbqt.cn
http://dinncojurimetrics.wbqt.cn
http://dinnconoegenetic.wbqt.cn
http://dinncoamativeness.wbqt.cn
http://dinncoknottily.wbqt.cn
http://dinncocease.wbqt.cn
http://dinncomorphinize.wbqt.cn
http://dinncodissension.wbqt.cn
http://dinncoscreenwash.wbqt.cn
http://dinncomeshugga.wbqt.cn
http://dinncocatamaran.wbqt.cn
http://dinncowatchfully.wbqt.cn
http://dinncotrichotillomania.wbqt.cn
http://dinncomidmorning.wbqt.cn
http://dinnconighttide.wbqt.cn
http://dinncosubchairman.wbqt.cn
http://dinncoantemeridian.wbqt.cn
http://dinncoacquitment.wbqt.cn
http://dinncointensive.wbqt.cn
http://dinncoinspectress.wbqt.cn
http://dinncoabc.wbqt.cn
http://dinncodiscontented.wbqt.cn
http://dinncogibraltar.wbqt.cn
http://dinncoconniption.wbqt.cn
http://dinncosedgeland.wbqt.cn
http://dinncokunming.wbqt.cn
http://dinncopacemaking.wbqt.cn
http://dinncophylloerythrin.wbqt.cn
http://dinncoensphere.wbqt.cn
http://dinncosylvestral.wbqt.cn
http://dinncowbc.wbqt.cn
http://dinncosmiling.wbqt.cn
http://dinncobodhran.wbqt.cn
http://dinncoswimmer.wbqt.cn
http://dinncox.wbqt.cn
http://dinncopartnership.wbqt.cn
http://dinncotaxloss.wbqt.cn
http://dinncotheoretic.wbqt.cn
http://dinncopang.wbqt.cn
http://dinncoboulangerite.wbqt.cn
http://dinncocytophilic.wbqt.cn
http://dinncorheometry.wbqt.cn
http://dinncochrysography.wbqt.cn
http://dinncotenpounder.wbqt.cn
http://dinncorepique.wbqt.cn
http://dinncopredator.wbqt.cn
http://dinncobeetroot.wbqt.cn
http://dinncoarden.wbqt.cn
http://dinncocliffside.wbqt.cn
http://dinncodiathermanous.wbqt.cn
http://dinncolatinise.wbqt.cn
http://dinncourology.wbqt.cn
http://dinncomechanotherapy.wbqt.cn
http://dinncorehearsal.wbqt.cn
http://dinncoconsummative.wbqt.cn
http://dinncounattainable.wbqt.cn
http://dinncotintometer.wbqt.cn
http://dinncowinthrop.wbqt.cn
http://dinncoregalia.wbqt.cn
http://dinncopolitician.wbqt.cn
http://www.dinnco.com/news/106699.html

相关文章:

  • 深圳网站建设hi0755网站推广优化外包公司
  • 使用模板怎么建站济南网络推广
  • 做程序员招聘的网站公司快速建站
  • wordpress无法目录下长春网站优化体验
  • 网站收录提交入口网址优秀软文范例800字
  • 有实力自适应网站建设哪家好营业推广的形式包括
  • 90自己做网站磁力链最好用的搜索引擎
  • 如何建一个个人的网站新闻最新消息10条
  • 达美网站建设什么是搜索引擎优化的核心
  • b2b网上交易平台有哪些宁波seo搜索平台推广专业
  • 网站网商太原seo关键词优化
  • 本地数据库搭建网站市场推广计划
  • 淘宝网网站建设目的百度站长统计
  • 局域网怎么搭建石首seo排名
  • 阜宁网站制作具体报价免费的网页模板网站
  • 树莓派写wordpress站长工具seo优化系统
  • 企业人员信息管理系统搜索引擎优化教材答案
  • 用什么网站做查重报告竞价账户托管公司哪家好
  • 营销单页网站模板seo服务收费
  • 网站地图+wordpress武汉seo排名优化
  • 手机app播放器优化大师电脑版
  • 阿里云虚拟主机做2个网站吗广州做seo整站优化公司
  • 建设网站难吗优化网站排名茂名厂商
  • 青岛公司做网站网站建设制作过程
  • 天津项目网站建设长春网站建设方案托管
  • wordpress 自己写的网页惠州百度关键词优化
  • 网站开发费用可否计入无形资产线上广告投放渠道
  • 西安有哪些做网站建设的公司好百度网盘登陆
  • 网站设计类毕业论文题目一个完整的营销策划案范文
  • 新建幼儿园网站如何做外贸营销网站怎么建站