|
|
@@ -23,6 +23,7 @@ import cn.hutool.core.util.PhoneUtil;
|
|
23
|
23
|
import cn.hutool.core.util.RandomUtil;
|
|
24
|
24
|
import cn.hutool.core.util.StrUtil;
|
|
25
|
25
|
import cn.hutool.json.JSONObject;
|
|
|
26
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
26
|
27
|
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
27
|
28
|
import com.unis.auth.api.SaBaseLoginUserApi;
|
|
28
|
29
|
import com.unis.auth.core.enums.SaClientTypeEnum;
|
|
|
@@ -31,6 +32,12 @@ import com.unis.auth.core.pojo.SaBaseLoginUser;
|
|
31
|
32
|
import com.unis.auth.core.util.StpClientLoginUserUtil;
|
|
32
|
33
|
import com.unis.auth.core.util.StpClientUtil;
|
|
33
|
34
|
import com.unis.auth.core.util.StpLoginUserUtil;
|
|
|
35
|
+import com.unis.auth.modular.login.entity.UserCollectLogin;
|
|
|
36
|
+import com.unis.auth.modular.login.mapper.UserCollectLoginMapper;
|
|
|
37
|
+import com.unis.auth.modular.login.param.UserCollectPasswordLoginParam;
|
|
|
38
|
+import com.unis.auth.modular.login.utils.JwtTokenUtils;
|
|
|
39
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
40
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
34
|
41
|
import org.springframework.stereotype.Service;
|
|
35
|
42
|
import com.unis.auth.api.SaBaseLoginUserApi;
|
|
36
|
43
|
import com.unis.auth.core.enums.SaClientTypeEnum;
|
|
|
@@ -55,6 +62,7 @@ import com.unis.dev.api.DevSmsApi;
|
|
55
|
62
|
|
|
56
|
63
|
import javax.annotation.Resource;
|
|
57
|
64
|
import java.util.List;
|
|
|
65
|
+import java.util.Objects;
|
|
58
|
66
|
import java.util.stream.Collectors;
|
|
59
|
67
|
|
|
60
|
68
|
/**
|
|
|
@@ -63,6 +71,7 @@ import java.util.stream.Collectors;
|
|
63
|
71
|
* @author xuyuxiang
|
|
64
|
72
|
* @date 2021/12/23 21:52
|
|
65
|
73
|
*/
|
|
|
74
|
+@Slf4j
|
|
66
|
75
|
@Service
|
|
67
|
76
|
public class AuthServiceImpl implements AuthService {
|
|
68
|
77
|
|
|
|
@@ -78,6 +87,9 @@ public class AuthServiceImpl implements AuthService {
|
|
78
|
87
|
@Resource(name = "clientLoginUserApi")
|
|
79
|
88
|
private SaBaseLoginUserApi clientLoginUserApi;
|
|
80
|
89
|
|
|
|
90
|
+ @Autowired
|
|
|
91
|
+ private UserCollectLoginMapper userCollectLoginMapper;
|
|
|
92
|
+
|
|
81
|
93
|
@Resource
|
|
82
|
94
|
private DevConfigApi devConfigApi;
|
|
83
|
95
|
|
|
|
@@ -124,7 +136,6 @@ public class AuthServiceImpl implements AuthService {
|
|
124
|
136
|
String phoneValidCodeReqNo = IdWorker.getIdStr();
|
|
125
|
137
|
|
|
126
|
138
|
|
|
127
|
|
-
|
|
128
|
139
|
// TODO 使用阿里云执行发送验证码,将验证码作为短信内容的参数变量放入,
|
|
129
|
140
|
// TODO 签名不传则使用系统默认配置的签名,支持传入多个参数,示例:{"name":"张三","number":"15038****76"}
|
|
130
|
141
|
//devSmsApi.sendSmsAliyun(phone, null, "验证码模板号", JSONUtil.toJsonStr(JSONUtil.createObj().set("validCode", phoneValidCode)));
|
|
|
@@ -148,17 +159,17 @@ public class AuthServiceImpl implements AuthService {
|
|
148
|
159
|
private void validValidCode(String phoneOrEmail, String validCode, String validCodeReqNo) {
|
|
149
|
160
|
// 依据请求号,取出缓存中的验证码进行校验
|
|
150
|
161
|
Object existValidCode;
|
|
151
|
|
- if(ObjectUtil.isEmpty(phoneOrEmail)) {
|
|
|
162
|
+ if (ObjectUtil.isEmpty(phoneOrEmail)) {
|
|
152
|
163
|
existValidCode = commonCacheOperator.get(AUTH_VALID_CODE_CACHE_KEY + validCodeReqNo);
|
|
153
|
164
|
} else {
|
|
154
|
165
|
existValidCode = commonCacheOperator.get(AUTH_VALID_CODE_CACHE_KEY + phoneOrEmail + StrUtil.UNDERLINE + validCodeReqNo);
|
|
155
|
166
|
}
|
|
156
|
167
|
// 为空则直接验证码错误
|
|
157
|
|
- if(ObjectUtil.isEmpty(existValidCode)) {
|
|
|
168
|
+ if (ObjectUtil.isEmpty(existValidCode)) {
|
|
158
|
169
|
throw new CommonException(AuthExceptionEnum.VALID_CODE_ERROR.getValue());
|
|
159
|
170
|
}
|
|
160
|
171
|
// 移除该验证码
|
|
161
|
|
- if(ObjectUtil.isEmpty(phoneOrEmail)) {
|
|
|
172
|
+ if (ObjectUtil.isEmpty(phoneOrEmail)) {
|
|
162
|
173
|
commonCacheOperator.remove(AUTH_VALID_CODE_CACHE_KEY + validCodeReqNo);
|
|
163
|
174
|
} else {
|
|
164
|
175
|
commonCacheOperator.remove(AUTH_VALID_CODE_CACHE_KEY + phoneOrEmail + StrUtil.UNDERLINE + validCodeReqNo);
|
|
|
@@ -177,23 +188,23 @@ public class AuthServiceImpl implements AuthService {
|
|
177
|
188
|
**/
|
|
178
|
189
|
private void validPhoneValidCodeParam(String phoneOrEmail, String validCode, String validCodeReqNo, String type) {
|
|
179
|
190
|
// 验证码正确则校验手机号格式
|
|
180
|
|
- if(ObjectUtil.isEmpty(phoneOrEmail)) {
|
|
|
191
|
+ if (ObjectUtil.isEmpty(phoneOrEmail)) {
|
|
181
|
192
|
// 执行校验验证码
|
|
182
|
193
|
validValidCode(null, validCode, validCodeReqNo);
|
|
183
|
194
|
} else {
|
|
184
|
|
- if(!PhoneUtil.isMobile(phoneOrEmail) && !CommonEmailUtil.isEmail(phoneOrEmail)) {
|
|
|
195
|
+ if (!PhoneUtil.isMobile(phoneOrEmail) && !CommonEmailUtil.isEmail(phoneOrEmail)) {
|
|
185
|
196
|
throw new CommonException(AuthExceptionEnum.PHONE_FORMAT_ERROR.getValue());
|
|
186
|
197
|
}
|
|
187
|
198
|
// 执行校验验证码
|
|
188
|
199
|
validValidCode(phoneOrEmail, validCode, validCodeReqNo);
|
|
189
|
200
|
|
|
190
|
201
|
// 根据手机号获取用户信息,判断用户是否存在,根据B端或C端判断
|
|
191
|
|
- if(SaClientTypeEnum.B.getValue().equals(type)) {
|
|
192
|
|
- if(ObjectUtil.isEmpty(loginUserApi.getUserByPhone(phoneOrEmail))) {
|
|
|
202
|
+ if (SaClientTypeEnum.B.getValue().equals(type)) {
|
|
|
203
|
+ if (ObjectUtil.isEmpty(loginUserApi.getUserByPhone(phoneOrEmail))) {
|
|
193
|
204
|
throw new CommonException(AuthExceptionEnum.PHONE_ERROR.getValue());
|
|
194
|
205
|
}
|
|
195
|
206
|
} else {
|
|
196
|
|
- if(ObjectUtil.isEmpty(clientLoginUserApi.getClientUserByPhone(phoneOrEmail))) {
|
|
|
207
|
+ if (ObjectUtil.isEmpty(clientLoginUserApi.getClientUserByPhone(phoneOrEmail))) {
|
|
197
|
208
|
throw new CommonException(AuthExceptionEnum.PHONE_ERROR.getValue());
|
|
198
|
209
|
}
|
|
199
|
210
|
}
|
|
|
@@ -211,25 +222,25 @@ public class AuthServiceImpl implements AuthService {
|
|
211
|
222
|
// 获取设备
|
|
212
|
223
|
String device = authAccountPasswordLoginParam.getDevice();
|
|
213
|
224
|
// 默认指定为PC,如在小程序跟移动端的情况下,自行指定即可
|
|
214
|
|
- if(ObjectUtil.isEmpty(device)) {
|
|
|
225
|
+ if (ObjectUtil.isEmpty(device)) {
|
|
215
|
226
|
device = AuthDeviceTypeEnum.PC.getValue();
|
|
216
|
227
|
} else {
|
|
217
|
228
|
AuthDeviceTypeEnum.validate(device);
|
|
218
|
229
|
}
|
|
219
|
230
|
// 校验验证码
|
|
220
|
231
|
String defaultCaptchaOpen = devConfigApi.getValueByKey(SNOWY_SYS_DEFAULT_CAPTCHA_OPEN_KEY);
|
|
221
|
|
- if(ObjectUtil.isNotEmpty(defaultCaptchaOpen)) {
|
|
222
|
|
- if(Convert.toBool(defaultCaptchaOpen)) {
|
|
|
232
|
+ if (ObjectUtil.isNotEmpty(defaultCaptchaOpen)) {
|
|
|
233
|
+ if (Convert.toBool(defaultCaptchaOpen)) {
|
|
223
|
234
|
// 获取验证码
|
|
224
|
235
|
String validCode = authAccountPasswordLoginParam.getValidCode();
|
|
225
|
236
|
// 获取验证码请求号
|
|
226
|
237
|
String validCodeReqNo = authAccountPasswordLoginParam.getValidCodeReqNo();
|
|
227
|
238
|
// 开启验证码则必须传入验证码
|
|
228
|
|
- if(ObjectUtil.isEmpty(validCode)) {
|
|
|
239
|
+ if (ObjectUtil.isEmpty(validCode)) {
|
|
229
|
240
|
throw new CommonException(AuthExceptionEnum.VALID_CODE_EMPTY.getValue());
|
|
230
|
241
|
}
|
|
231
|
242
|
// 开启验证码则必须传入验证码请求号
|
|
232
|
|
- if(ObjectUtil.isEmpty(validCodeReqNo)) {
|
|
|
243
|
+ if (ObjectUtil.isEmpty(validCodeReqNo)) {
|
|
233
|
244
|
throw new CommonException(AuthExceptionEnum.VALID_CODE_REQ_NO_EMPTY.getValue());
|
|
234
|
245
|
}
|
|
235
|
246
|
// 执行校验验证码
|
|
|
@@ -245,9 +256,9 @@ public class AuthServiceImpl implements AuthService {
|
|
245
|
256
|
throw new CommonException(AuthExceptionEnum.PWD_DECRYPT_ERROR.getValue());
|
|
246
|
257
|
}
|
|
247
|
258
|
// 根据账号获取用户信息,根据B端或C端判断
|
|
248
|
|
- if(SaClientTypeEnum.B.getValue().equals(type)) {
|
|
|
259
|
+ if (SaClientTypeEnum.B.getValue().equals(type)) {
|
|
249
|
260
|
SaBaseLoginUser saBaseLoginUser = loginUserApi.getUserByAccount(account);
|
|
250
|
|
- if(ObjectUtil.isEmpty(saBaseLoginUser)) {
|
|
|
261
|
+ if (ObjectUtil.isEmpty(saBaseLoginUser)) {
|
|
251
|
262
|
throw new CommonException(AuthExceptionEnum.ACCOUNT_ERROR.getValue());
|
|
252
|
263
|
}
|
|
253
|
264
|
if (!saBaseLoginUser.getPassword().equals(passwordHash)) {
|
|
|
@@ -261,7 +272,7 @@ public class AuthServiceImpl implements AuthService {
|
|
261
|
272
|
return execLoginB(saBaseLoginUser, device);
|
|
262
|
273
|
} else {
|
|
263
|
274
|
SaBaseClientLoginUser saBaseClientLoginUser = clientLoginUserApi.getClientUserByAccount(account);
|
|
264
|
|
- if(ObjectUtil.isEmpty(saBaseClientLoginUser)) {
|
|
|
275
|
+ if (ObjectUtil.isEmpty(saBaseClientLoginUser)) {
|
|
265
|
276
|
throw new CommonException(AuthExceptionEnum.ACCOUNT_ERROR.getValue());
|
|
266
|
277
|
}
|
|
267
|
278
|
if (!saBaseClientLoginUser.getPassword().equals(passwordHash)) {
|
|
|
@@ -281,22 +292,22 @@ public class AuthServiceImpl implements AuthService {
|
|
281
|
292
|
// 设备
|
|
282
|
293
|
String device = authPhoneValidCodeLoginParam.getDevice();
|
|
283
|
294
|
// 默认指定为PC,如在小程序跟移动端的情况下,自行指定即可
|
|
284
|
|
- if(ObjectUtil.isEmpty(device)) {
|
|
|
295
|
+ if (ObjectUtil.isEmpty(device)) {
|
|
285
|
296
|
device = AuthDeviceTypeEnum.PC.getValue();
|
|
286
|
297
|
} else {
|
|
287
|
298
|
AuthDeviceTypeEnum.validate(device);
|
|
288
|
299
|
}
|
|
289
|
300
|
// 根据手机号获取用户信息,根据B端或C端判断
|
|
290
|
|
- if(SaClientTypeEnum.B.getValue().equals(type)) {
|
|
|
301
|
+ if (SaClientTypeEnum.B.getValue().equals(type)) {
|
|
291
|
302
|
SaBaseLoginUser saBaseLoginUser = loginUserApi.getUserByPhone(phone);
|
|
292
|
|
- if(ObjectUtil.isEmpty(saBaseLoginUser)) {
|
|
|
303
|
+ if (ObjectUtil.isEmpty(saBaseLoginUser)) {
|
|
293
|
304
|
throw new CommonException(AuthExceptionEnum.ACCOUNT_ERROR.getValue());
|
|
294
|
305
|
}
|
|
295
|
306
|
// 执行B端登录
|
|
296
|
307
|
return execLoginB(saBaseLoginUser, device);
|
|
297
|
308
|
} else {
|
|
298
|
309
|
SaBaseClientLoginUser saBaseClientLoginUser = clientLoginUserApi.getClientUserByPhone(phone);
|
|
299
|
|
- if(ObjectUtil.isEmpty(saBaseClientLoginUser)) {
|
|
|
310
|
+ if (ObjectUtil.isEmpty(saBaseClientLoginUser)) {
|
|
300
|
311
|
throw new CommonException(AuthExceptionEnum.ACCOUNT_ERROR.getValue());
|
|
301
|
312
|
}
|
|
302
|
313
|
// 执行C端登录
|
|
|
@@ -313,25 +324,25 @@ public class AuthServiceImpl implements AuthService {
|
|
313
|
324
|
long disableTime = StpUtil.getDisableTime(userAccount);
|
|
314
|
325
|
if (disableTime > 0) {
|
|
315
|
326
|
if (disableTime > 60) {
|
|
316
|
|
- throw new CommonException(userAccount + "账号已被封禁, 请再"+ disableTime/60+ "分钟后重新尝试登录!!");
|
|
|
327
|
+ throw new CommonException(userAccount + "账号已被封禁, 请再" + disableTime / 60 + "分钟后重新尝试登录!!");
|
|
317
|
328
|
}
|
|
318
|
|
- throw new CommonException(userAccount + "账号已被封禁, 请再"+ disableTime+ "秒后重新尝试登录!!");
|
|
|
329
|
+ throw new CommonException(userAccount + "账号已被封禁, 请再" + disableTime + "秒后重新尝试登录!!");
|
|
319
|
330
|
}
|
|
320
|
331
|
}
|
|
321
|
332
|
|
|
322
|
333
|
// redis中保存登录错误次数
|
|
323
|
|
- private void saveLoginTimes(String userAccount){
|
|
|
334
|
+ private void saveLoginTimes(String userAccount) {
|
|
324
|
335
|
String loginErrorKey = LOGIN_ERROR_TIMES_KEY_PREFIX + userAccount;
|
|
325
|
336
|
Integer number = (Integer) commonCacheOperator.get(loginErrorKey);
|
|
326
|
337
|
if (number == null) {
|
|
327
|
338
|
// 如果redis中没有保存,代表失败第一次
|
|
328
|
339
|
number = 2;
|
|
329
|
|
- commonCacheOperator.put(loginErrorKey, number,5 * 60);
|
|
|
340
|
+ commonCacheOperator.put(loginErrorKey, number, 5 * 60);
|
|
330
|
341
|
return;
|
|
331
|
342
|
}
|
|
332
|
343
|
if (number < 5) {
|
|
333
|
344
|
number++;
|
|
334
|
|
- commonCacheOperator.put(loginErrorKey, number,5 * 60);
|
|
|
345
|
+ commonCacheOperator.put(loginErrorKey, number, 5 * 60);
|
|
335
|
346
|
return;
|
|
336
|
347
|
}
|
|
337
|
348
|
// 第五次封禁账号,第六次进入isDisableTime方法,返回用户还需等待时间
|
|
|
@@ -343,6 +354,7 @@ public class AuthServiceImpl implements AuthService {
|
|
343
|
354
|
|
|
344
|
355
|
/**
|
|
345
|
356
|
* 登录成功、清空登录次数
|
|
|
357
|
+ *
|
|
346
|
358
|
* @param userAccount 账号
|
|
347
|
359
|
*/
|
|
348
|
360
|
private void clearLoginErrorTimes(String userAccount) {
|
|
|
@@ -359,7 +371,7 @@ public class AuthServiceImpl implements AuthService {
|
|
359
|
371
|
**/
|
|
360
|
372
|
private String execLoginB(SaBaseLoginUser saBaseLoginUser, String device) {
|
|
361
|
373
|
// 校验状态
|
|
362
|
|
- if(!saBaseLoginUser.getEnabled()) {
|
|
|
374
|
+ if (!saBaseLoginUser.getEnabled()) {
|
|
363
|
375
|
throw new CommonException(AuthExceptionEnum.ACCOUNT_DISABLED.getValue());
|
|
364
|
376
|
}
|
|
365
|
377
|
|
|
|
@@ -399,7 +411,7 @@ public class AuthServiceImpl implements AuthService {
|
|
399
|
411
|
**/
|
|
400
|
412
|
private String execLoginC(SaBaseClientLoginUser saBaseClientLoginUser, String device) {
|
|
401
|
413
|
// 校验状态
|
|
402
|
|
- if(!saBaseClientLoginUser.getEnabled()) {
|
|
|
414
|
+ if (!saBaseClientLoginUser.getEnabled()) {
|
|
403
|
415
|
throw new CommonException(AuthExceptionEnum.ACCOUNT_DISABLED.getValue());
|
|
404
|
416
|
}
|
|
405
|
417
|
// 执行登录
|
|
|
@@ -463,7 +475,7 @@ public class AuthServiceImpl implements AuthService {
|
|
463
|
475
|
@Override
|
|
464
|
476
|
public String doLoginById(String userId, String device, String type) {
|
|
465
|
477
|
// 根据id获取用户信息,根据B端或C端判断
|
|
466
|
|
- if(SaClientTypeEnum.B.getValue().equals(type)) {
|
|
|
478
|
+ if (SaClientTypeEnum.B.getValue().equals(type)) {
|
|
467
|
479
|
SaBaseLoginUser saBaseLoginUser = loginUserApi.getUserById(userId);
|
|
468
|
480
|
if (ObjectUtil.isEmpty(saBaseLoginUser)) {
|
|
469
|
481
|
throw new CommonException(AuthExceptionEnum.ACCOUNT_ERROR.getValue());
|
|
|
@@ -479,4 +491,57 @@ public class AuthServiceImpl implements AuthService {
|
|
479
|
491
|
return execLoginC(saBaseClientLoginUser, device);
|
|
480
|
492
|
}
|
|
481
|
493
|
}
|
|
|
494
|
+
|
|
|
495
|
+ /**
|
|
|
496
|
+ * @author: hujianchun
|
|
|
497
|
+ * @Description:
|
|
|
498
|
+ * @param:
|
|
|
499
|
+ * @return: 移动端手机号密码登录
|
|
|
500
|
+ */
|
|
|
501
|
+ @Override
|
|
|
502
|
+ public String userLogin(UserCollectPasswordLoginParam userCollectPasswordLoginParam) {
|
|
|
503
|
+
|
|
|
504
|
+ // 手机号
|
|
|
505
|
+ String phone = userCollectPasswordLoginParam.getPhone();
|
|
|
506
|
+ //加密的密码
|
|
|
507
|
+ String password = userCollectPasswordLoginParam.getPassword();
|
|
|
508
|
+ //SM2密码解密
|
|
|
509
|
+ String passwordStr;
|
|
|
510
|
+ try {
|
|
|
511
|
+ passwordStr = CommonCryptogramUtil.doSm2Decrypt(password);
|
|
|
512
|
+
|
|
|
513
|
+ } catch (Exception e) {
|
|
|
514
|
+ log.error("SM2密码解密失败,请检查前端公钥,堆栈信息[{}]", e.getMessage());
|
|
|
515
|
+ throw new CommonException(AuthExceptionEnum.PWD_DECRYPT_ERROR.getValue());
|
|
|
516
|
+ }
|
|
|
517
|
+
|
|
|
518
|
+ List<UserCollectLogin> userCollectLogins = userCollectLoginMapper.selectList(new LambdaQueryWrapper<UserCollectLogin>()
|
|
|
519
|
+ .eq(UserCollectLogin::getPhone, phone));
|
|
|
520
|
+ if (CollectionUtil.isEmpty(userCollectLogins)) {
|
|
|
521
|
+ //返回用户信息
|
|
|
522
|
+ throw new CommonException(AuthExceptionEnum.PHONE_ERROR.getValue());
|
|
|
523
|
+ }
|
|
|
524
|
+
|
|
|
525
|
+ //有手机号,就进行密码校验
|
|
|
526
|
+ UserCollectLogin userCollectLogin = userCollectLogins.get(0);
|
|
|
527
|
+ if (!Objects.equals(userCollectLogin.getPassword(), passwordStr)) {
|
|
|
528
|
+ return AuthExceptionEnum.PWD_ERROR.getValue();
|
|
|
529
|
+
|
|
|
530
|
+ }
|
|
|
531
|
+ /* try {
|
|
|
532
|
+ //执行登录
|
|
|
533
|
+ // 缓存用户信息,此处使用TokenSession为了指定时间内无操作则自动下线
|
|
|
534
|
+ StpClientUtil.login(userCollectLogin.getId(), new SaLoginModel().setExtra("name", userCollectLogin.getName()));
|
|
|
535
|
+ StpClientUtil.getTokenSession().set("loginUserToken", userCollectLogins);
|
|
|
536
|
+ } catch (Exception e) {
|
|
|
537
|
+ log.error("缓存失败");
|
|
|
538
|
+ throw new RuntimeException(e);
|
|
|
539
|
+ }*/
|
|
|
540
|
+
|
|
|
541
|
+ // 返回token
|
|
|
542
|
+ return JwtTokenUtils.createToken(userCollectLogin,50000);
|
|
|
543
|
+
|
|
|
544
|
+
|
|
|
545
|
+
|
|
|
546
|
+ }
|
|
482
|
547
|
}
|