log safe sms delivery diagnostics
This commit is contained in:
1 parent
ed0cc75615
commit
32e977df5c
1 file changed
+9
-1
+9
-1
@@ -7,10 +7,13 @@ import com.moyi.controlplane.common.ApiException;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class CloudVerificationSender implements VerificationSender {
|
||||
private static final Logger log = LoggerFactory.getLogger(CloudVerificationSender.class);
|
||||
private final Environment env;
|
||||
public CloudVerificationSender(Environment env) { this.env=env; }
|
||||
private String value(String key) { return env.getProperty(key,"").trim(); }
|
||||
@@ -53,10 +56,15 @@ public class CloudVerificationSender implements VerificationSender {
|
||||
var request=new SendSmsRequest().setPhoneNumbers(account.startsWith("+86")?account.substring(3):account)
|
||||
.setSignName(value("MOYI_SMS_SIGN_NAME")).setTemplateCode(value("MOYI_SMS_TEMPLATE_CODE")).setTemplateParam("{\"code\":\""+code+"\"}");
|
||||
var response=client.sendSmsWithOptions(request,new RuntimeOptions().setAutoretry(false).setConnectTimeout(5000).setReadTimeout(10000));
|
||||
if(response.getBody()==null || !"OK".equals(response.getBody().getCode())) throw new IllegalStateException("SMS rejected");
|
||||
if(response.getBody()==null || !"OK".equals(response.getBody().getCode())) {
|
||||
String resultCode = response.getBody() == null ? "EMPTY_RESPONSE" : String.valueOf(response.getBody().getCode());
|
||||
log.warn("Aliyun SMS verification rejected: resultCode={}", resultCode);
|
||||
throw new IllegalStateException("SMS rejected");
|
||||
}
|
||||
}
|
||||
} catch(Exception exception) {
|
||||
// SDK exceptions may contain credentials or recipients. Never expose them to logs or clients.
|
||||
log.warn("Verification delivery failed: channel={}, exceptionType={}", channel, exception.getClass().getSimpleName());
|
||||
throw new ApiException("VERIFICATION_SEND_FAILED","验证码发送失败,请稍后重试或联系管理员检查发送服务",502);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user