fix: 优化短信功能
This commit is contained in:
parent
ac05b629ee
commit
6a0216c3bf
@ -57,6 +57,7 @@ public class SecurityConfig {
|
|||||||
.requestMatchers("/env/**").permitAll()
|
.requestMatchers("/env/**").permitAll()
|
||||||
.requestMatchers("/sw/**").permitAll()
|
.requestMatchers("/sw/**").permitAll()
|
||||||
.requestMatchers("/data/**").permitAll()
|
.requestMatchers("/data/**").permitAll()
|
||||||
|
.requestMatchers("/sms/**").permitAll()
|
||||||
.requestMatchers(HttpMethod.GET, "/").permitAll()
|
.requestMatchers(HttpMethod.GET, "/").permitAll()
|
||||||
.requestMatchers(HttpMethod.GET,
|
.requestMatchers(HttpMethod.GET,
|
||||||
"/*.html",
|
"/*.html",
|
||||||
|
|||||||
@ -44,7 +44,7 @@ public class SmsVerifyCodeController {
|
|||||||
if (phone == null || phone.isEmpty()) {
|
if (phone == null || phone.isEmpty()) {
|
||||||
return ResponseResult.error("手机号不能为空");
|
return ResponseResult.error("手机号不能为空");
|
||||||
}
|
}
|
||||||
if (type == null || (type != SmsVerifyCode.TYPE_REGISTER && type != SmsVerifyCode.TYPE_FIND_PASSWORD)) {
|
if (type == null || (!type.equals(SmsVerifyCode.TYPE_REGISTER) && !type.equals(SmsVerifyCode.TYPE_FIND_PASSWORD))) {
|
||||||
return ResponseResult.error("类型错误:1-注册 2-找回密码");
|
return ResponseResult.error("类型错误:1-注册 2-找回密码");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,21 +1,66 @@
|
|||||||
package com.yfd.platform.utils;
|
package com.yfd.platform.utils;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import cn.hutool.http.HttpUtil;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 默认短信发送实现(模拟实现)
|
* 默认短信发送实现(模拟实现)
|
||||||
|
* 当 sms.enabled=false 或未配置时启用
|
||||||
* 实际使用时替换为真实的短信网关
|
* 实际使用时替换为真实的短信网关
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
@ConditionalOnProperty(name = "sms.enabled", havingValue = "false", matchIfMissing = true)
|
@ConditionalOnProperty(name = "sms.enabled", havingValue = "true", matchIfMissing = true)
|
||||||
public class DefaultSmsSender implements SmsSender {
|
public class DefaultSmsSender implements SmsSender {
|
||||||
|
@Resource
|
||||||
|
private SmsProperties smsProperties;
|
||||||
@Override
|
@Override
|
||||||
public boolean send(String phone, String content) {
|
public boolean send(String phone, String content) {
|
||||||
log.info("【模拟短信发送】手机号: {}, 内容: {}", phone, content);
|
if (StrUtil.isBlank(phone) || StrUtil.isBlank(content)) {
|
||||||
|
log.warn("【短信发送】手机号或内容为空");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 构建请求参数
|
||||||
|
Map<String, Object> params = new HashMap<>();
|
||||||
|
params.put("phone", phone);
|
||||||
|
params.put("msg", content);
|
||||||
|
params.put("source", smsProperties.getSource());
|
||||||
|
|
||||||
|
log.info("【短信发送】开始发送,URL: {}, 手机号: {}", smsProperties.getUrl(), phone);
|
||||||
|
|
||||||
|
// 发送 POST 请求
|
||||||
|
String result = HttpUtil.post(smsProperties.getUrl(), JSONObject.toJSONString(params), smsProperties.getTimeout());
|
||||||
|
|
||||||
|
if (StrUtil.isNotBlank(result)) {
|
||||||
|
JSONObject json = JSONObject.parseObject(result);
|
||||||
|
int code = json.getIntValue("code");
|
||||||
|
boolean data = json.getBooleanValue("data");
|
||||||
|
String msg = json.getString("msg");
|
||||||
|
|
||||||
|
if (code == 200 && data) {
|
||||||
|
log.info("【短信发送】发送成功,手机号: {}", phone);
|
||||||
return true;
|
return true;
|
||||||
|
} else {
|
||||||
|
log.error("【短信发送】发送失败,code: {}, msg: {}, 手机号: {}", code, msg, phone);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.error("【短信发送】HTTP 请求失败,接口无响应,手机号: {}", phone);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("【短信发送】发生异常: {}", e.getMessage(), e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -0,0 +1,17 @@
|
|||||||
|
package com.yfd.platform.utils;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Component
|
||||||
|
@ConfigurationProperties(prefix = "sms")
|
||||||
|
public class SmsProperties {
|
||||||
|
|
||||||
|
private String url;
|
||||||
|
|
||||||
|
private Integer source;
|
||||||
|
|
||||||
|
private Integer timeout = 10000;
|
||||||
|
}
|
||||||
@ -10,6 +10,13 @@ jasypt:
|
|||||||
rsa:
|
rsa:
|
||||||
private_key: ${RSA_PRIVATE_KEY:MIIBUwIBADANBgkqhkiG9w0BAQEFAASCAT0wggE5AgEAAkEA0vfvyTdGJkdbHkB8mp0f3FE0GYP3AYPaJF7jUd1M0XxFSE2ceK3k2kw20YvQ09NJKk+OMjWQl9WitG9pB6tSCQIDAQABAkA2SimBrWC2/wvauBuYqjCFwLvYiRYqZKThUS3MZlebXJiLB+Ue/gUifAAKIg1avttUZsHBHrop4qfJCwAI0+YRAiEA+W3NK/RaXtnRqmoUUkb59zsZUBLpvZgQPfj1MhyHDz0CIQDYhsAhPJ3mgS64NbUZmGWuuNKp5coY2GIj/zYDMJp6vQIgUueLFXv/eZ1ekgz2Oi67MNCk5jeTF2BurZqNLR3MSmUCIFT3Q6uHMtsB9Eha4u7hS31tj1UWE+D+ADzp59MGnoftAiBeHT7gDMuqeJHPL4b+kC+gzV4FGTfhR9q3tTbklZkD2A==}
|
private_key: ${RSA_PRIVATE_KEY:MIIBUwIBADANBgkqhkiG9w0BAQEFAASCAT0wggE5AgEAAkEA0vfvyTdGJkdbHkB8mp0f3FE0GYP3AYPaJF7jUd1M0XxFSE2ceK3k2kw20YvQ09NJKk+OMjWQl9WitG9pB6tSCQIDAQABAkA2SimBrWC2/wvauBuYqjCFwLvYiRYqZKThUS3MZlebXJiLB+Ue/gUifAAKIg1avttUZsHBHrop4qfJCwAI0+YRAiEA+W3NK/RaXtnRqmoUUkb59zsZUBLpvZgQPfj1MhyHDz0CIQDYhsAhPJ3mgS64NbUZmGWuuNKp5coY2GIj/zYDMJp6vQIgUueLFXv/eZ1ekgz2Oi67MNCk5jeTF2BurZqNLR3MSmUCIFT3Q6uHMtsB9Eha4u7hS31tj1UWE+D+ADzp59MGnoftAiBeHT7gDMuqeJHPL4b+kC+gzV4FGTfhR9q3tTbklZkD2A==}
|
||||||
|
|
||||||
|
# 短信配置
|
||||||
|
sms:
|
||||||
|
enabled: true
|
||||||
|
url: http://172.16.31.153:8688/sms/sendNotice
|
||||||
|
source: 2
|
||||||
|
timeout: 10000
|
||||||
|
|
||||||
# Actuator & Micrometer 默认配置
|
# Actuator & Micrometer 默认配置
|
||||||
management:
|
management:
|
||||||
endpoints:
|
endpoints:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user