阿里云短信接口
This commit is contained in:
parent
9d6356a286
commit
47976b80de
|
|
@ -0,0 +1,74 @@
|
|||
package com.ruoyi.web.controller.tool;
|
||||
|
||||
import com.aliyun.dysmsapi20170525.Client;
|
||||
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
|
||||
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
|
||||
import com.aliyun.teaopenapi.models.Config;
|
||||
import com.aliyun.teautil.models.RuntimeOptions;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.utils.CacheUtils;
|
||||
import com.ruoyi.common.utils.ExceptionUtil;
|
||||
import com.ruoyi.system.domain.NoticeRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
/**
|
||||
* @author clunt
|
||||
* 通知模块
|
||||
*/
|
||||
@Slf4j
|
||||
@Controller
|
||||
@RequestMapping("/tool/notice")
|
||||
public class NoticeController {
|
||||
|
||||
|
||||
@PostMapping("/sendSms")
|
||||
@ResponseBody
|
||||
public AjaxResult sendMsg(@RequestBody @Valid NoticeRequest request){
|
||||
try {
|
||||
// 短信验证码
|
||||
String code = String.valueOf((int)((Math.random() * 9 + 1) * Math.pow(10,5)));
|
||||
|
||||
// 工程代码泄露可能会导致AccessKey泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html
|
||||
Client client = createClient("LTAI5tLDuQRdfyHASSxehs9m", "accessKeySecret");
|
||||
SendSmsRequest sendSmsRequest = new SendSmsRequest()
|
||||
.setSignName("保无忧")
|
||||
.setTemplateCode("SMS_275375715")
|
||||
.setPhoneNumbers(request.getPhone())
|
||||
.setTemplateParam("{\"code\":"+ code +"}");
|
||||
RuntimeOptions runtime = new RuntimeOptions();
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
|
||||
log.info("发送给{}短信响应为{}", request.getPhone(), sendSmsResponse);
|
||||
CacheUtils.put(request.getPhone(), code);
|
||||
}catch (Exception e){
|
||||
log.error("给 {} 发送短信失败 {}", request.getPhone(), ExceptionUtil.getExceptionMessage(e));
|
||||
}
|
||||
return AjaxResult.success("操作成功!");
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用AK&SK初始化账号Client
|
||||
* @param accessKeyId
|
||||
* @param accessKeySecret
|
||||
* @return Client
|
||||
* @throws Exception
|
||||
*/
|
||||
public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
|
||||
Config config = new Config()
|
||||
// 必填,您的 AccessKey ID
|
||||
.setAccessKeyId(accessKeyId)
|
||||
// 必填,您的 AccessKey Secret
|
||||
.setAccessKeySecret(accessKeySecret);
|
||||
// 访问的域名
|
||||
config.endpoint = "dysmsapi.aliyuncs.com";
|
||||
return new Client(config);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -100,6 +100,12 @@
|
|||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>dysmsapi20170525</artifactId>
|
||||
<version>2.0.23</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
|
@ -288,6 +288,8 @@ public class ShiroConfig
|
|||
filterChainDefinitionMap.put("/js/**", "anon");
|
||||
filterChainDefinitionMap.put("/ruoyi/**", "anon");
|
||||
filterChainDefinitionMap.put("/captcha/captchaImage**", "anon");
|
||||
// 短信验证码接口
|
||||
filterChainDefinitionMap.put("/tool/notice/**", "anon");
|
||||
// 退出 logout地址,shiro去清除session
|
||||
filterChainDefinitionMap.put("/logout", "logout");
|
||||
// 不需要拦截的访问
|
||||
|
|
|
|||
|
|
@ -0,0 +1,20 @@
|
|||
package com.ruoyi.system.domain;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* @author clunt
|
||||
* 通知实体
|
||||
*/
|
||||
@Data
|
||||
public class NoticeRequest {
|
||||
|
||||
/**
|
||||
* 手机号
|
||||
* */
|
||||
@NotNull
|
||||
private String phone;
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue