完善短信验证码注册的功能

main
wangqing 1 year ago
parent 5b11711965
commit 6c74c305c4

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>luoo_parent</artifactId>
<groupId>com.luoo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>luoo_sms</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.2.5</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

@ -0,0 +1,11 @@
package com.luoo.sms;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SmsApplication {
public static void main(String[] args) {
SpringApplication.run(SmsApplication.class);
}
}

@ -0,0 +1,38 @@
package com.luoo.sms.listener;
import com.aliyuncs.exceptions.ClientException;
import com.luoo.sms.util.SmsUtils;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
@RabbitListener(queues = "sms")
public class SmsListener {
@Autowired
private SmsUtils smsUtils;
@Value("${aliyun.sms.template_code}")
private String template_code;
@Value("${aliyun.sms.sign_name}")
private String sign_name;
@RabbitHandler
public void executeSms(Map<String,String> map) {
String mobile = map.get("mobile");
String checkcode= map.get("checkcode");
System.out.println("手机号:"+map.get("mobile"));
System.out.println("验证码:"+map.get("checkcode"));
// try {
// smsUtils.sendSms(mobile,template_code,sign_name,"{\"checkcode\":\""+checkcode+"\"}");
// } catch (ClientException e) {
// throw new RuntimeException(e);
// }
}
}

@ -0,0 +1,101 @@
package com.luoo.sms.util;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* @author Administrator
*
*/
@Component
public class SmsUtils {
//产品名称:云通信短信API产品,开发者无需替换
static final String product = "Dysmsapi";
//产品域名,开发者无需替换
static final String domain = "dysmsapi.aliyuncs.com";
@Autowired
private Environment env;
// TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
/**
*
* @param mobile
* @param template_code
* @param sign_name
* @param param
* @return
* @throws ClientException
*/
public SendSmsResponse sendSms(String mobile,String template_code,String sign_name,String param) throws ClientException {
String accessKeyId =env.getProperty("aliyun.sms.accessKeyId");
String accessKeySecret = env.getProperty("aliyun.sms.accessKeySecret");
//可自助调整超时时间
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
//初始化acsClient,暂不支持region化
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
IAcsClient acsClient = new DefaultAcsClient(profile);
//组装请求对象-具体描述见控制台-文档部分内容
SendSmsRequest request = new SendSmsRequest();
//必填:待发送手机号
request.setPhoneNumbers(mobile);
//必填:短信签名-可在短信控制台中找到
request.setSignName(sign_name);
//必填:短信模板-可在短信控制台中找到
request.setTemplateCode(template_code);
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
request.setTemplateParam(param);
//选填-上行短信扩展码(无特殊需求用户请忽略此字段)
//request.setSmsUpExtendCode("90997");
//可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
request.setOutId("yourOutId");
//hint 此处可能会抛出异常注意catch
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
return sendSmsResponse;
}
public QuerySendDetailsResponse querySendDetails(String mobile,String bizId) throws ClientException {
String accessKeyId =env.getProperty("accessKeyId");
String accessKeySecret = env.getProperty("accessKeySecret");
//可自助调整超时时间
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
//初始化acsClient,暂不支持region化
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
IAcsClient acsClient = new DefaultAcsClient(profile);
//组装请求对象
QuerySendDetailsRequest request = new QuerySendDetailsRequest();
//必填-号码
request.setPhoneNumber(mobile);
//可选-流水号
request.setBizId(bizId);
//必填-发送日期 支持30天内记录查询格式yyyyMMdd
SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");
request.setSendDate(ft.format(new Date()));
//必填-页大小
request.setPageSize(10L);
//必填-当前页码从1开始计数
request.setCurrentPage(1L);
//hint 此处可能会抛出异常注意catch
QuerySendDetailsResponse querySendDetailsResponse = acsClient.getAcsResponse(request);
return querySendDetailsResponse;
}
}

@ -0,0 +1,13 @@
server:
port: 9009
spring:
application:
name: luoo-sms
rabbitmq:
host: 116.62.145.60
aliyun:
sms:
accessKeyId: xxx
accessKeySecret: xxx
template_code: xxx
sign_name: xxx

@ -15,6 +15,14 @@
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>com.luoo</groupId>
<artifactId>luoo_common</artifactId>

@ -6,6 +6,7 @@ import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
@ -35,6 +36,9 @@ public class UserController {
@Autowired
private UserService userService;
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private IdWorker idWorker;
@ -83,15 +87,15 @@ public class UserController {
public Result regist(@PathVariable String code,@RequestBody User user){
// 得到缓存中的验证码
String checkcodeRedis = (String) redisTemplate.opsForValue().get("checkcode_"+user.getMobile());
if (checkcodeRedis.isEmpty()){
return new Result(false,StatusCode.ERROR,"请先获取手机验证码");
}
if (!checkcodeRedis.equals(code)) {
return new Result(false,StatusCode.ERROR,"请输入正确的验证码");
}
user.setId(idWorker.nextId()+"");
user.setFollowcount(0); //关注数
user.setFanscount(0); //粉丝数
user.setOnline(0L); // 在线时长
user.setRegdate(new Date()); // 注册日期
user.setUpdatedate(new Date()); // 更新日期
user.setLastdate(new Date()); // 最后登陆日期
userService.add(user);
return new Result(true,StatusCode.OK,"注册成功");
}

@ -1,6 +1,7 @@
package com.luoo.user.service;
import java.util.*;
import java.util.concurrent.TimeUnit;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
@ -14,11 +15,14 @@ import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
@ -45,6 +49,13 @@ public class UserService {
@Autowired
private IdWorker idWorker;
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private BCryptPasswordEncoder encoder;
@ -101,14 +112,14 @@ public class UserService {
*/
public void add(User user) {
user.setId( idWorker.nextId()+"" );
user.setFanscount(0);
user.setFollowcount(0);
user.setOnline(0L);
user.setFanscount(0); //粉丝数
user.setFollowcount(0); //关注数
user.setOnline(0L); //在线时长
user.setRegdate(new Date());
user.setUpdatedate(new Date());
user.setLastdate(new Date());
String newPassword = encoder.encode(user.getPassword());
user.setPassword(newPassword);
// String newPassword = encoder.encode(user.getPassword());
// user.setPassword(newPassword);
userDao.save(user);
}
@ -197,8 +208,13 @@ public class UserService {
// 生成6位数字随机数
String checkcode = RandomStringUtils.randomNumeric(6);
// 向缓存中放一份
redisTemplate.opsForValue().set("checkcode_"+mobile,checkcode,6, TimeUnit.HOURS);
// 向用户发一份
Map<String,String> map = new HashMap<>();
map.put("mobile",mobile);
map.put("checkcode",checkcode);
rabbitTemplate.convertAndSend("sms",map);
// 在控制台放一份(方便测试)

@ -11,6 +11,10 @@ spring:
jpa:
database: MySQL
show-sql: true
redis:
host: 116.62.145.60
rabbitmq:
host: 116.62.145.60
jwt:
config:
key: luoo

@ -17,6 +17,7 @@
<module>luoo_friend</module>
<module>luoo_manage</module>
<module>luoo_web</module>
<module>luoo_sms</module>
</modules>
<packaging>pom</packaging>
<name>luoo_parent</name>

Loading…
Cancel
Save