当前位置:网站首页>Use of Alibaba Cloud SMS Service (create, test notes)
Use of Alibaba Cloud SMS Service (create, test notes)
2022-08-03 06:36:00 【I want to learn!】
目录
Set enable and disableAccessKey编辑
注册账号
阿里云官网: https://www.aliyun.com/
Click the registration button on the homepage of the official website.
阿里云短信服务-设置短信签名
选择短信服务-国内消息
注册成功后,点击登录按钮进行登录.登录后进入短信服务管理页面,选择国内消息菜单:
创建短信模板
短信模板包含短信发送内容、场景、变量信息.

创建AccessKey
点击右上角,在弹出的窗口中点击【AccessKey管理】∶

添加AccessKey的使用权限

Set enable and disableAccessKey
代码开发
1、导入maven坐标
<!--阿里云短信服务-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.16</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>2.1.0</version>
</dependency>2、调用API(此处将api封装成utils)
public class SMSUtils {
/**
* 发送短信
* @param signName 签名
* @param templateCode 模板
* @param phoneNumbers 手机号
* @param param 参数
*/
public static void sendMessage(String signName, String templateCode,String phoneNumbers,String param){
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5tJPSzEXKgNYxUor1yjD", "MqeAq4ZW3p1xPN0FPhjgrT5GXCKEuq");
IAcsClient client = new DefaultAcsClient(profile);
SendSmsRequest request = new SendSmsRequest();
request.setSysRegionId("cn-hangzhou");
request.setPhoneNumbers(phoneNumbers); //手机号
request.setSignName(signName); //签名
request.setTemplateCode(templateCode); //模板code
request.setTemplateParam("{\"code\":\""+param+"\"}");
try {
SendSmsResponse response = client.getAcsResponse(request);
System.out.println("短信发送成功");
}catch (ClientException e) {
e.printStackTrace();
}
}
3、Import related configuration classes that accept SMS verification codes
- 实体类User(直接从课程资料中导入即可)
- Mapper接口UserMapper
- 业务层接口UserService
- 业务层实现类UserServicelmpl
- 控制层UserController
- 工具类SMSutils、 ValidateCodeutils(直接从课程资料中导入即可)
-------You need to visit the mobile page,不可被拦截,在LogincheckFilterrelease in the filter
String[] urls = new String[]{
"/employee/login",
"/employee/logout",
"/backend/**",
"/front/**",
"/common/**",
"/user/login",
"/user/sendMsg"
};
//4.1、判断登录状态,如果已登录,则直接放行
if (request.getSession().getAttribute("employee")!=null){
log.info("用户已登录,id为{}",request.getSession().getAttribute("employee"));
//获取当前用户的id,set到线程中,以便controller方法和Handler中使用
Long empId = (Long) request.getSession().getAttribute("employee");
BaseContextUtil.setNowId(empId);
filterChain.doFilter(request,response);
return;
}
// 4-2、判断登录状态,如果已登录,则直接放行
if (request.getSession().getAttribute("user") != null) {
log.info("用户已登录,用户id为:{}", request.getSession().getAttribute("user"));
Long userId= (Long) request.getSession().getAttribute("user");
BaseContextUtil.setNowId(userId);
filterChain.doFilter(request, response);
return;
}
-----UserController处理post请求(Send verification code requests and login requests)
The sending of the verification code is simulated here,Real delivery needsAccessKey,详情见IDEA中的配置
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/sendMsg")
public R<String> sendMsg(@RequestBody User user, HttpSession httpSession ){
//获取手机号
String phone = user.getPhone();
if(StringUtils.isNotEmpty(phone)){
//生成随机的4位验证码
String code = ValidateCodeUtils.generateValidateCode(4).toString();
log.info("code={}",code);
//调用阿里云提供的短信服务API完成发送短信
//SMSUtils.sendMessage("瑞吉外卖","",phone,code);
//需要将生成的验证码保存到Session
httpSession.setAttribute(phone,code);
return R.success("手机验证码短信发送成功");
}
return R.error("短信发送失败");
}
@PostMapping("/login")
public R<User> login(@RequestBody Map map,HttpSession httpSession) {
log.info(map.toString());
//获取客户端传来的phone和code
String phone = map.get("phone").toString();
String code = map.get("code").toString();
//与之前在sessionSaved Contrasts-----k-v形式
Object code1 = httpSession.getAttribute(phone);
if (code1 != null && code1.equals(code)) {
//查询user表数据,判断当前手机号是否为新用户,If it is a new user, the registration is automatically completed
LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
qw.eq(User::getPhone, phone);
User user = userService.getOne(qw);
if (user == null) {
//New users are created automatically
user = new User();
user.setPhone(phone);
user.setStatus(1);
userService.save(user);
}
//用户不为空,登录
httpSession.setAttribute("user", user.getId());
return R.success(user);
}
return R.error("登录失败");
}
}
边栏推荐
猜你喜欢
随机推荐
Eight, the difference between the interface of the abstract class
使用ZBrush制作恶魔模型
【面试准备】游戏开发中的几个岗位分别做什么&考察侧重点
DNS常见资源记录类型详解
VLAN虚拟局域网技术
2. What is the difference between Exception and Error?
嘿!selenium自动下载driver版本,得知道一下
TFS (Azure conversation) prohibit people checked out at the same time
八、抽象类的接口的区别
AI智能剪辑,仅需2秒一键提取精彩片段
SVN客户端安装及操作文档
申请公网ip后,配置光猫,路由器使用公网ip步骤
移动端人脸风格化技术的应用
次世代建模到底需要哪些美术基础
域名怎么管理,域名管理注意事项有哪些?
mib browser无法接收snmp trap消息解决
802.1AS的SystemIdentity理解
VB.net如何使用List类型
Oracle 数据库集群常用巡检命令
数组与字符串11-反转字符串









