Skip to content

Commit 2fa7392

Browse files
committedFeb 5, 2017
switch to bcrypt
1 parent 1f7cec8 commit 2fa7392

File tree

6 files changed

+24
-21
lines changed

6 files changed

+24
-21
lines changed
 

‎.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ node_modules/
33
dist/
44
npm-debug.log
55
img/
6-
.vscode/
6+
.vscode/
7+
.idea/

‎package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
"private": true,
77
"scripts": {
88
"dev": "node build/dev-server.js",
9-
"build": "node build/build.js"
9+
"build": "node build/build.js",
10+
"server": "node app.js"
1011
},
1112
"dependencies": {
1213
"axios": "^0.15.3",
14+
"bcryptjs": "^2.4.0",
1315
"element-ui": "^1.1.2",
1416
"koa": "^1.2.4",
1517
"koa-bodyparser": "^2.3.0",
@@ -19,7 +21,6 @@
1921
"koa-logger": "^1.3.0",
2022
"koa-router": "5.4",
2123
"koa-static": "^2.0.0",
22-
"md5": "^2.2.1",
2324
"mysql": "^2.12.0",
2425
"sequelize": "^3.29.0",
2526
"stylus": "^0.54.5",

‎server/controllers/user.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const user = require('../models/user.js');
22
const jwt = require('koa-jwt');
3+
const bcrypt = require('bcryptjs');
34

45
const getUserInfo = function* (){
56
const id = this.params.id; // 获取url里传过来的参数里的id
@@ -13,7 +14,7 @@ const postUserAuth = function* (){
1314
const userInfo = yield user.getUserByName(data.name);
1415
console.log(this.request)
1516
if(userInfo != null){ // 如果查无此用户会返回null
16-
if(userInfo.password != data.password){
17+
if(!bcrypt.compareSync(data.password, userInfo.password)){
1718
this.body = {
1819
success: false, // success标志位是方便前端判断返回是正确与否
1920
info: '密码错误!'
@@ -43,4 +44,4 @@ module.exports = {
4344
router.get('/user/:id', getUserInfo); // 定义url的参数是id
4445
router.post('/user', postUserAuth);
4546
}
46-
}
47+
}

‎server/schema/user.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = function(sequelize, DataTypes) {
1313
allowNull: false
1414
},
1515
password: {
16-
type: DataTypes.CHAR(32),
16+
type: DataTypes.CHAR(128),
1717
allowNull: false
1818
}
1919
}, {

‎sql/user.sql

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ USE `todolist`;
2020
CREATE TABLE IF NOT EXISTS `user` (
2121
`id` int(11) NOT NULL AUTO_INCREMENT,
2222
`user_name` char(50) NOT NULL,
23-
`password` char(32) NOT NULL,
23+
`password` char(128) NOT NULL,
2424
PRIMARY KEY (`id`)
2525
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
2626

2727
-- 正在导出表 todolist.user 的数据:~0 rows (大约)
2828
DELETE FROM `user`;
2929
/*!40000 ALTER TABLE `user` DISABLE KEYS */;
30-
INSERT INTO `user` (`id`, `user_name`, `password`) VALUES
31-
(1, 'molunerfinn', '202cb962ac59075b964b07152d234b70');
30+
INSERT INTO `user` (`user_name`, `password`) VALUES
31+
('molunerfinn', '$2a$10$x3f0Y2SNAmyAfqhKVAV.7uE7RHs3FDGuSYw.LlZhOFoyK7cjfZ.Q6');
32+
INSERT INTO `user` (`user_name`, `password`) VALUES
33+
('admin', '$2a$10$x3f0Y2SNAmyAfqhKVAV.7uE7RHs3FDGuSYw.LlZhOFoyK7cjfZ.Q6');
3234
/*!40000 ALTER TABLE `user` ENABLE KEYS */;
3335

3436
/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;

‎src/components/Login.vue

+10-12
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
<el-row class="content">
33
<el-col :xs="24" :sm="{span: 6,offset: 9}">
44
<span class="title">
5-
欢迎登录
5+
欢迎登录
66
</span>
77
<el-row>
8-
<el-input
9-
v-model="account"
8+
<el-input
9+
v-model="account"
1010
placeholder="账号"
1111
type="text">
1212
</el-input>
13-
<el-input
14-
v-model="password"
13+
<el-input
14+
v-model="password"
1515
placeholder="密码"
1616
type="password"
1717
@keyup.enter.native="loginToDo">
@@ -23,8 +23,6 @@
2323
</template>
2424

2525
<script>
26-
import md5 from 'md5'
27-
2826
export default {
2927
data () {
3028
return {
@@ -36,8 +34,8 @@ export default {
3634
loginToDo() {
3735
let obj = {
3836
name: this.account,
39-
password: md5(this.password)
40-
}
37+
password: this.password
38+
}
4139
this.$http.post('/auth/user', obj) // 将信息发送给后端
4240
.then((res) => {
4341
console.log(res);
@@ -46,7 +44,7 @@ export default {
4644
this.$message({ // 登录成功,显示提示语
4745
type: 'success',
4846
message: '登录成功!'
49-
});
47+
});
5048
this.$router.push('/todolist') // 进入todolist页面,登录成功
5149
}else{
5250
this.$message.error(res.data.info); // 登录失败,显示提示语
@@ -70,5 +68,5 @@ export default {
7068
margin 12px 0
7169
.el-button
7270
width 100%
73-
margin-top 12px
74-
</style>
71+
margin-top 12px
72+
</style>

0 commit comments

Comments
 (0)