提交 | 用户 | 时间
|
722af2
|
1 |
package com.dl.web.controller.common; |
X |
2 |
|
|
3 |
import cn.dev33.satoken.annotation.SaIgnore; |
|
4 |
import cn.hutool.captcha.AbstractCaptcha; |
|
5 |
import cn.hutool.captcha.generator.CodeGenerator; |
|
6 |
import cn.hutool.core.util.IdUtil; |
|
7 |
import cn.hutool.core.util.RandomUtil; |
|
8 |
import com.dl.common.constant.CacheConstants; |
|
9 |
import com.dl.common.constant.Constants; |
|
10 |
import com.dl.common.core.domain.R; |
|
11 |
import com.dl.common.enums.CaptchaType; |
|
12 |
import com.dl.common.utils.StringUtils; |
|
13 |
import com.dl.common.utils.email.MailUtils; |
|
14 |
import com.dl.common.utils.redis.RedisUtils; |
|
15 |
import com.dl.common.utils.reflect.ReflectUtils; |
|
16 |
import com.dl.common.utils.spring.SpringUtils; |
|
17 |
import com.dl.framework.config.properties.CaptchaProperties; |
|
18 |
import com.dl.framework.config.properties.MailProperties; |
|
19 |
import com.dl.sms.config.properties.SmsProperties; |
|
20 |
import com.dl.sms.core.SmsTemplate; |
|
21 |
import com.dl.sms.entity.SmsResult; |
|
22 |
import com.dl.system.service.ISysConfigService; |
|
23 |
import lombok.RequiredArgsConstructor; |
|
24 |
import lombok.extern.slf4j.Slf4j; |
|
25 |
import org.springframework.expression.Expression; |
|
26 |
import org.springframework.expression.ExpressionParser; |
|
27 |
import org.springframework.expression.spel.standard.SpelExpressionParser; |
|
28 |
import org.springframework.validation.annotation.Validated; |
|
29 |
import org.springframework.web.bind.annotation.GetMapping; |
|
30 |
import org.springframework.web.bind.annotation.RestController; |
|
31 |
|
|
32 |
import javax.validation.constraints.NotBlank; |
|
33 |
import java.time.Duration; |
|
34 |
import java.util.HashMap; |
|
35 |
import java.util.Map; |
|
36 |
|
|
37 |
/** |
|
38 |
* 验证码操作处理 |
|
39 |
* |
|
40 |
* @author Lion Li |
|
41 |
*/ |
|
42 |
@SaIgnore |
|
43 |
@Slf4j |
|
44 |
@Validated |
|
45 |
@RequiredArgsConstructor |
|
46 |
@RestController |
|
47 |
public class CaptchaController { |
|
48 |
|
|
49 |
private final CaptchaProperties captchaProperties; |
|
50 |
private final SmsProperties smsProperties; |
|
51 |
private final ISysConfigService configService; |
|
52 |
private final MailProperties mailProperties; |
|
53 |
|
|
54 |
/** |
|
55 |
* 短信验证码 |
|
56 |
* |
|
57 |
* @param phonenumber 用户手机号 |
|
58 |
*/ |
|
59 |
@GetMapping("/captchaSms") |
|
60 |
public R<Void> smsCaptcha(@NotBlank(message = "{user.phonenumber.not.blank}") String phonenumber) { |
|
61 |
if (!smsProperties.getEnabled()) { |
|
62 |
return R.fail("当前系统没有开启短信功能!"); |
|
63 |
} |
|
64 |
String key = CacheConstants.CAPTCHA_CODE_KEY + phonenumber; |
|
65 |
String code = RandomUtil.randomNumbers(4); |
|
66 |
RedisUtils.setCacheObject(key, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION)); |
|
67 |
// 验证码模板id 自行处理 (查数据库或写死均可) |
|
68 |
String templateId = ""; |
|
69 |
Map<String, String> map = new HashMap<>(1); |
|
70 |
map.put("code", code); |
|
71 |
SmsTemplate smsTemplate = SpringUtils.getBean(SmsTemplate.class); |
|
72 |
SmsResult result = smsTemplate.send(phonenumber, templateId, map); |
|
73 |
if (!result.isSuccess()) { |
|
74 |
log.error("验证码短信发送异常 => {}", result); |
|
75 |
return R.fail(result.getMessage()); |
|
76 |
} |
|
77 |
return R.ok(); |
|
78 |
} |
|
79 |
|
|
80 |
/** |
|
81 |
* 邮箱验证码 |
|
82 |
* |
|
83 |
* @param email 邮箱 |
|
84 |
*/ |
|
85 |
@GetMapping("/captchaEmail") |
|
86 |
public R<Void> emailCode(@NotBlank(message = "{user.email.not.blank}") String email) { |
|
87 |
if (!mailProperties.getEnabled()) { |
|
88 |
return R.fail("当前系统没有开启邮箱功能!"); |
|
89 |
} |
|
90 |
String key = CacheConstants.CAPTCHA_CODE_KEY + email; |
|
91 |
String code = RandomUtil.randomNumbers(4); |
|
92 |
RedisUtils.setCacheObject(key, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION)); |
|
93 |
try { |
|
94 |
MailUtils.sendText(email, "登录验证码", "您本次验证码为:" + code + ",有效性为" + Constants.CAPTCHA_EXPIRATION + "分钟,请尽快填写。"); |
|
95 |
} catch (Exception e) { |
|
96 |
log.error("验证码短信发送异常 => {}", e.getMessage()); |
|
97 |
return R.fail(e.getMessage()); |
|
98 |
} |
|
99 |
return R.ok(); |
|
100 |
} |
|
101 |
|
|
102 |
/** |
|
103 |
* 生成验证码 |
|
104 |
*/ |
|
105 |
@GetMapping("/captchaImage") |
|
106 |
public R<Map<String, Object>> getCode() { |
|
107 |
Map<String, Object> ajax = new HashMap<>(); |
|
108 |
boolean captchaEnabled = configService.selectCaptchaEnabled(); |
|
109 |
ajax.put("captchaEnabled", captchaEnabled); |
|
110 |
if (!captchaEnabled) { |
|
111 |
return R.ok(ajax); |
|
112 |
} |
|
113 |
// 保存验证码信息 |
|
114 |
String uuid = IdUtil.simpleUUID(); |
|
115 |
String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid; |
|
116 |
// 生成验证码 |
|
117 |
CaptchaType captchaType = captchaProperties.getType(); |
|
118 |
boolean isMath = CaptchaType.MATH == captchaType; |
|
119 |
Integer length = isMath ? captchaProperties.getNumberLength() : captchaProperties.getCharLength(); |
|
120 |
CodeGenerator codeGenerator = ReflectUtils.newInstance(captchaType.getClazz(), length); |
|
121 |
AbstractCaptcha captcha = SpringUtils.getBean(captchaProperties.getCategory().getClazz()); |
|
122 |
captcha.setGenerator(codeGenerator); |
|
123 |
captcha.createCode(); |
|
124 |
String code = captcha.getCode(); |
|
125 |
if (isMath) { |
|
126 |
ExpressionParser parser = new SpelExpressionParser(); |
|
127 |
Expression exp = parser.parseExpression(StringUtils.remove(code, "=")); |
|
128 |
code = exp.getValue(String.class); |
|
129 |
} |
|
130 |
RedisUtils.setCacheObject(verifyKey, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION)); |
|
131 |
ajax.put("uuid", uuid); |
|
132 |
ajax.put("img", captcha.getImageBase64()); |
|
133 |
return R.ok(ajax); |
|
134 |
} |
|
135 |
|
|
136 |
} |