当前位置:网站首页>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("登录失败");
}
}
边栏推荐
- 阿里云短信服务的使用(创建,测试笔记)
- 【七夕特效】 -- 满屏爱心
- 3. What is the difference between final, finally, and finalize?
- servlet learning (7) ServletContext
- 【面试准备】游戏开发中的几个岗位分别做什么&考察侧重点
- 3D建模:做什么副业在家就能月入1W?
- All-round interpretation of POE switches (middle)
- 数组与字符串13-两数之和等于目标数
- C#程序默认以管理员权限打开
- 6. What is the difference between Vector, ArrayList and LinkedList?(design, performance, safety)
猜你喜欢
随机推荐
大学毕业后,零基础想转行成为一名3D建模师,现在该做什么?
appium(3)webview测试
二分查找6 - 寻找峰值
申请公网ip后,配置光猫,路由器使用公网ip步骤
二层交换机,三层交换机,路由器内容总结记录
C语言中打印字符数组出现乱码的问题(烫烫烫)
cobalt strike 的基础使用
MySql的Sql语句的练习(试试你能写出来几道呢)
嘿!selenium自动下载driver版本,得知道一下
VS2022 encapsulation under Windows dynamic library and dynamic library calls
次世代建模到底需要哪些美术基础
移动端人脸风格化技术的应用
数组与字符串12-数组拆分
数组与字符串11-反转字符串
VS项目配置管理器
php 数组元素移动
3D建模为什么会变得无处不在
域名注册流程:如何选择购买合适的域名?
数组与字符串15-最大连续1的个数
【C语言】关于数组传参问题/首地址