fix: 优化短信功能

This commit is contained in:
tangwei 2026-04-27 13:48:54 +08:00
parent ac05b629ee
commit 6a0216c3bf
5 changed files with 75 additions and 5 deletions

View File

@ -57,6 +57,7 @@ public class SecurityConfig {
.requestMatchers("/env/**").permitAll()
.requestMatchers("/sw/**").permitAll()
.requestMatchers("/data/**").permitAll()
.requestMatchers("/sms/**").permitAll()
.requestMatchers(HttpMethod.GET, "/").permitAll()
.requestMatchers(HttpMethod.GET,
"/*.html",

View File

@ -44,7 +44,7 @@ public class SmsVerifyCodeController {
if (phone == null || phone.isEmpty()) {
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-找回密码");
}

View File

@ -1,21 +1,66 @@
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 org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
* 默认短信发送实现模拟实现
* sms.enabled=false 或未配置时启用
* 实际使用时替换为真实的短信网关
*/
@Slf4j
@Component
@ConditionalOnProperty(name = "sms.enabled", havingValue = "false", matchIfMissing = true)
@ConditionalOnProperty(name = "sms.enabled", havingValue = "true", matchIfMissing = true)
public class DefaultSmsSender implements SmsSender {
@Resource
private SmsProperties smsProperties;
@Override
public boolean send(String phone, String content) {
log.info("【模拟短信发送】手机号: {}, 内容: {}", phone, content);
return true;
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;
} 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;
}
}
}

View File

@ -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;
}

View File

@ -10,6 +10,13 @@ jasypt:
rsa:
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 默认配置
management:
endpoints: