项目要求短信验证,先说思路。
1.调用阿里云短信sdk或其他厂商短信sdk
2.将随机验证码保存 (可以保存到redis或mysql之类的,设置一个失效时间。 )
3.用户输入验证码说 和保存的验证码进行比较。 看是否一致,如果一致删除保存的验证码,不一致就返回验证失败。
!-- 同步---->配置文件如下!-- 异步---> com.aliyun dysmsapi20170525 2.0.9 com.aliyun alibabacloud-dysmsapi20170525 1.0.1
aliyun.access-key-id=你的key aliyun.access-key-secret=你的keySecret aliyun.endpoint=dysmsapi.aliyuncs.com aliyun.region= cn-hangzhou #阿里云短信 aliyun.note.sign-name=阿里云短信测试 aliyun.note.template-code=SMS_154950909编写配置文件
@Configuration
@ConfigurationProperties(prefix = "aliyun")
@Setter
@Getter
public class AliyunSendNoteConfig {
private String accessKeyId;
private String accessKeySecret;
private String endpoint="dysmsapi.aliyuncs.com";
private String region="cn-hangzhou";
}
创建config
@Configuration
public class AliyunClientConfig {
@Resource
private AliyunSendNoteConfig aliyunSendNoteConfig;
@Bean
public com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
Config config = new Config()
// 您的 AccessKey ID
.setAccessKeyId(aliyunSendNoteConfig.getAccessKeyId())
// 您的 AccessKey Secret
.setAccessKeySecret(aliyunSendNoteConfig.getAccessKeySecret());
// 访问的域名
config.endpoint = aliyunSendNoteConfig.getEndpoint();
return new com.aliyun.dysmsapi20170525.Client(config);
}
@Bean
public AsyncClient createAsyncClient() throws Exception {
StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
.accessKeyId(aliyunSendNoteConfig.getAccessKeyId())
.accessKeySecret(aliyunSendNoteConfig.getAccessKeySecret())
//.securityToken("") // use STS token
.build());
return AsyncClient.builder()
.region(aliyunSendNoteConfig.getRegion())
.credentialsProvider(provider)
//.serviceConfiguration(Configuration.create()) // Service-level configuration
// Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
.overrideConfiguration(
ClientOverrideConfiguration.create()
.setEndpointOverride(aliyunSendNoteConfig.getEndpoint())
//.setReadTimeout(Duration.ofSeconds(30))
)
.build();
}
}
编写service
基类接口
boolean syncSendMsg(String phoneNumber,String signName,String templateCode,
String code,String outId);
void asyncSendMsg(String phoneNumber,String signName,String templateCode,
String code,String outId);
阿里云短信接口
public interface AliyunSendMsgBaseService extends SendMsgBaseService{
}
实现阿里云短信接口
@Service
public class AliyunSendMsgServiceImpl implements AliyunSendMsgBaseService {
@Resource
private com.aliyun.dysmsapi20170525.Client client;
@Resource
private com.aliyun.sdk.service.dysmsapi20170525.AsyncClient asyncClient;
@Override
public boolean syncSendMsg(String phoneNumber,String signName,String templateCode, String code,String outId) {
HashMap templateMaps = Maps.newHashMap();
templateMaps.put("code",code);
SendSmsRequest sendSmsRequest = new SendSmsRequest()
.setSignName(signName)
.setTemplateCode(templateCode)
.setPhoneNumbers(phoneNumber)
.setTemplateParam(GsonUtil.object2String(templateMaps));
if(StringUtils.hasLength(outId))sendSmsRequest.setOutId(outId);
RuntimeOptions runtime = new RuntimeOptions();
try {
// 复制代码运行请自行打印 API 的返回值
SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
if(sendSmsResponse != null) {
return SEND_MSG_SUCCESS.equals(sendSmsResponse.getBody().code);
}
} catch (TeaException error) {
// 如有需要,请打印 error
error.printStackTrace();
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
// 如有需要,请打印 error
_error.printStackTrace();
TeaException error = new TeaException(_error.getMessage(), _error);
// 如有需要,请打印 error
com.aliyun.teautil.Common.assertAsString(error.message);
}
return false;
}
@Override
public void asyncSendMsg(String phoneNumber, String signName, String templateCode, String code, String outId) {
HashMap templateMaps = Maps.newHashMap();
templateMaps.put("code",code);
com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest.Builder builder = com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest.builder()
.signName(signName)
.templateCode(templateCode)
.phoneNumbers(phoneNumber)
.templateParam(GsonUtil.object2String(templateMaps));
if(StringUtils.hasLength(outId))builder.outId(outId);
// Asynchronously get the return value of the API request
asyncClient.sendSms(builder.build());
}
}
调用发送短信接口
//验证码 可以是4位或6位的随机数
String code= "1234"
sendMsgBaseService.syncSendMsg(phoneNumber, aliYunNoteConfig.getSignName(),
aliYunNoteConfig.getTemplateCode(), code, null)
验证
交流群
java交流群: 868794080
源码资料源码资料
参考资料阿里云短信sdk



