Skip to content

Commit c2f33a4

Browse files
committed
add tryLoginTimes config
1 parent babf85d commit c2f33a4

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

config/config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ config.development = {
2424
common: {
2525
//登录jwt签名密钥,必须更改,否则有安全隐患,可以随机生成字符串
2626
loginSecret: "CodePushServer",
27+
//当天登录密码错误尝试次数,超过次数帐户将会锁定。0:表示无限制,可能会出现暴力破解。 大于0:必须开启redis服务。
28+
tryLoginTimes: 0,
2729
//CodePush Web部署地址,也可以配置成CodePush Web登录地址
2830
codePushWebUrl: "http://localhost:3001/login",
2931
//差异化更新版本生成数目 默认为3

core/services/account-manager.js

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var security = require('../utils/security');
77
var factory = require('../utils/factory');
88
var moment = require('moment');
99
var EmailManager = require('./email-manager');
10+
var config = require('../config');
1011

1112
var proto = module.exports = function (){
1213
function AccountManager() {
@@ -103,6 +104,7 @@ proto.login = function (account, password) {
103104
}else {
104105
where = {username: account};
105106
}
107+
var tryLoginTimes = _.get(config, 'common.tryLoginTimes', 0);
106108
return models.Users.findOne({where: where})
107109
.then(function(users) {
108110
if (_.isEmpty(users)) {
@@ -111,30 +113,37 @@ proto.login = function (account, password) {
111113
return users;
112114
})
113115
.then(function (users) {
114-
var loginKey = `${LOGIN_LIMIT_PRE}${users.id}`;
115-
return factory.getRedisClient("default").getAsync(loginKey)
116-
.then(function (loginErrorTimes) {
117-
if (loginErrorTimes > 10) {
118-
throw new Error(`您输入密码错误次数超过限制,帐户已经锁定`);
119-
}
116+
if (tryLoginTimes > 0) {
117+
var loginKey = `${LOGIN_LIMIT_PRE}${users.id}`;
118+
var client = factory.getRedisClient("default");
119+
return client.getAsync(loginKey)
120+
.then(function (loginErrorTimes) {
121+
if (loginErrorTimes > tryLoginTimes) {
122+
throw new Error(`您输入密码错误次数超过限制,帐户已经锁定`);
123+
}
124+
return users;
125+
});
126+
} else {
120127
return users;
121-
});
128+
}
122129
})
123130
.then(function (users) {
124131
if (!security.passwordVerifySync(password, users.password)) {
125-
var loginKey = `${LOGIN_LIMIT_PRE}${users.id}`;
126-
var client = factory.getRedisClient("default");
127-
client.existsAsync(loginKey)
128-
.then(function (isExists) {
129-
if (!isExists) {
130-
var expires = moment().endOf('day').format('X') - moment().format('X');
131-
return client.setexAsync(loginKey, expires, 0);
132-
}
133-
return isExists;
134-
})
135-
.then(function () {
136-
return client.incrAsync(loginKey);
137-
})
132+
if (tryLoginTimes > 0) {
133+
var loginKey = `${LOGIN_LIMIT_PRE}${users.id}`;
134+
var client = factory.getRedisClient("default");
135+
client.existsAsync(loginKey)
136+
.then(function (isExists) {
137+
if (!isExists) {
138+
var expires = moment().endOf('day').format('X') - moment().format('X');
139+
return client.setexAsync(loginKey, expires, 0);
140+
}
141+
return isExists;
142+
})
143+
.then(function () {
144+
return client.incrAsync(loginKey);
145+
});
146+
}
138147
throw new Error("account or password error.");
139148
} else {
140149
return users;

0 commit comments

Comments
 (0)