|
@@ -1,12 +1,17 @@
|
1
|
1
|
package com.lqkj.link.module.authority.service;
|
2
|
2
|
|
3
|
3
|
import cn.hutool.core.util.RandomUtil;
|
|
4
|
+import com.lqkj.link.module.authority.domain.Captcha;
|
4
|
5
|
import com.lqkj.link.module.authority.domain.UserInfo;
|
|
6
|
+import com.lqkj.link.module.authority.repository.CaptchaRepository;
|
5
|
7
|
import com.lqkj.link.module.authority.repository.RoleInfoRepository;
|
6
|
8
|
import com.lqkj.link.module.authority.repository.UserInfoRepository;
|
7
|
9
|
import com.lqkj.link.util.RSAUtils;
|
|
10
|
+import com.lqkj.link.util.SendSmsUtils;
|
8
|
11
|
import jakarta.annotation.PostConstruct;
|
9
|
12
|
import org.bouncycastle.jcajce.PKIXCertRevocationCheckerParameters;
|
|
13
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
14
|
+import org.springframework.security.authentication.LockedException;
|
10
|
15
|
import org.springframework.transaction.annotation.Transactional;
|
11
|
16
|
import org.apache.commons.lang3.StringUtils;
|
12
|
17
|
import org.springframework.data.domain.Page;
|
|
@@ -25,6 +30,9 @@ public class UserInfoService {
|
25
|
30
|
private final PasswordEncoder passwordEncoder;
|
26
|
31
|
private final RoleInfoRepository roleInfoRepository;
|
27
|
32
|
|
|
33
|
+ @Autowired
|
|
34
|
+ private CaptchaRepository captchaRepository;
|
|
35
|
+
|
28
|
36
|
public UserInfoService(UserInfoRepository userInfoRepository, PasswordEncoder passwordEncoder, RoleInfoRepository roleInfoRepository) {
|
29
|
37
|
this.userInfoRepository = userInfoRepository;
|
30
|
38
|
this.passwordEncoder = passwordEncoder;
|
|
@@ -49,7 +57,7 @@ public class UserInfoService {
|
49
|
57
|
guestUser.setLocking(false);
|
50
|
58
|
guestUser.setRefreshResource(false);
|
51
|
59
|
guestUser.setUpdateTime(new Date());
|
52
|
|
- register(guestUser, true, null);
|
|
60
|
+ register(guestUser, true, null, null);
|
53
|
61
|
return guestUser.getUserCode();
|
54
|
62
|
}
|
55
|
63
|
|
|
@@ -207,13 +215,13 @@ public class UserInfoService {
|
207
|
215
|
* @param userInfo
|
208
|
216
|
*/
|
209
|
217
|
@Transactional
|
210
|
|
- public void register(UserInfo userInfo, boolean isGuest, String userCode) {
|
|
218
|
+ public void register(UserInfo userInfo, boolean isGuest, String userCode, String captcha) {
|
211
|
219
|
if (!isGuest){
|
212
|
220
|
userInfo.setUserCode(RSAUtils.decryptBase64(userInfo.getUserCode()));
|
213
|
221
|
if (StringUtils.isNotBlank(userInfo.getPassword()) && userInfo.getPassword().length() == 172) {
|
214
|
222
|
userInfo.setPassword(RSAUtils.decryptBase64(userInfo.getPassword()));
|
215
|
223
|
}
|
216
|
|
- checkerParameters(userInfo.getUserCode(), userInfo.getPassword());
|
|
224
|
+ checkerParameters(userInfo.getUserCode(), userInfo.getPassword(), captcha);
|
217
|
225
|
assemblyParameter(userInfo, userCode);
|
218
|
226
|
}
|
219
|
227
|
userInfoRepository.save(userInfo);
|
|
@@ -247,25 +255,43 @@ public class UserInfoService {
|
247
|
255
|
* @param userCode 用户账号
|
248
|
256
|
* @param password 用户密码
|
249
|
257
|
*/
|
250
|
|
- private void checkerParameters(String userCode, String password) {
|
|
258
|
+ private void checkerParameters(String userCode, String password, String captcha) {
|
251
|
259
|
if (StringUtils.isBlank(userCode)) {
|
252
|
260
|
throw new RuntimeException("账号不能为空!");
|
253
|
261
|
}
|
254
|
262
|
if (StringUtils.isBlank(password)) {
|
255
|
263
|
throw new RuntimeException("密码不能为空!");
|
256
|
264
|
}
|
|
265
|
+ if (StringUtils.isBlank(captcha)) {
|
|
266
|
+ throw new RuntimeException("验证码不能为空!");
|
|
267
|
+ }
|
257
|
268
|
if (userInfoRepository.hasSameUserCode(userCode)) {
|
258
|
269
|
throw new RuntimeException("该手机号已被注册!");
|
259
|
270
|
}
|
260
|
271
|
if (!userCode.matches("^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$")){
|
261
|
272
|
throw new RuntimeException("请输入正确的手机号!");
|
262
|
273
|
}
|
|
274
|
+ checkCaptcha(userCode, captcha);
|
263
|
275
|
if (!password.matches("^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{8,16}$")){
|
264
|
276
|
throw new RuntimeException("密码必须是8-16位数字、大写字母、小写字母!");
|
265
|
277
|
}
|
266
|
278
|
|
267
|
279
|
}
|
268
|
280
|
|
|
281
|
+ private void checkCaptcha(String userCode, String captcha) {
|
|
282
|
+ Captcha byPhoneNumber = captchaRepository.findByPhoneNumber(userCode);
|
|
283
|
+ if (Objects.isNull(byPhoneNumber)){
|
|
284
|
+ throw new RuntimeException("验证码错误!");
|
|
285
|
+ }
|
|
286
|
+ if ((new Date()).getTime() - byPhoneNumber.getCreateTime().getTime() > 1000 * 60 * 5){
|
|
287
|
+ throw new RuntimeException("验证码已过期,请重新获取");
|
|
288
|
+ }
|
|
289
|
+ if (!byPhoneNumber.getCode().equals(captcha)){
|
|
290
|
+ throw new RuntimeException("验证码错误!");
|
|
291
|
+ }
|
|
292
|
+
|
|
293
|
+ }
|
|
294
|
+
|
269
|
295
|
|
270
|
296
|
/**
|
271
|
297
|
* 保存ue设置
|
|
@@ -276,4 +302,22 @@ public class UserInfoService {
|
276
|
302
|
public void saveSettings(Integer autosaveTime, Integer movingSpeed, String userCode) {
|
277
|
303
|
userInfoRepository.saveSettings(autosaveTime, movingSpeed, userCode);
|
278
|
304
|
}
|
|
305
|
+
|
|
306
|
+ @Transactional
|
|
307
|
+ public void sendSms(String phoneNumber) {
|
|
308
|
+ String code = SendSmsUtils.generateSMSCode();
|
|
309
|
+ Captcha captcha = captchaRepository.findByPhoneNumber(phoneNumber);
|
|
310
|
+ if (Objects.nonNull(captcha)){
|
|
311
|
+ captcha.setCode(code);
|
|
312
|
+ captcha.setCreateTime(new Date());
|
|
313
|
+ }else {
|
|
314
|
+ captcha = new Captcha(null, code, phoneNumber, new Date());
|
|
315
|
+ }
|
|
316
|
+ captchaRepository.save(captcha);
|
|
317
|
+ try {
|
|
318
|
+ SendSmsUtils.sendSms(phoneNumber, code);
|
|
319
|
+ } catch (Exception e) {
|
|
320
|
+ throw new RuntimeException(e);
|
|
321
|
+ }
|
|
322
|
+ }
|
279
|
323
|
}
|