diff --git a/luoo_sms/pom.xml b/luoo_sms/pom.xml new file mode 100644 index 0000000..a9a71df --- /dev/null +++ b/luoo_sms/pom.xml @@ -0,0 +1,36 @@ + + + + luoo_parent + com.luoo + 1.0-SNAPSHOT + + 4.0.0 + + luoo_sms + + + org.springframework.boot + spring-boot-starter-amqp + + + com.aliyun + aliyun-java-sdk-dysmsapi + 1.0.0 + + + com.aliyun + aliyun-java-sdk-core + 3.2.5 + + + + + 8 + 8 + UTF-8 + + + \ No newline at end of file diff --git a/luoo_sms/src/main/java/com/luoo/sms/SmsApplication.java b/luoo_sms/src/main/java/com/luoo/sms/SmsApplication.java new file mode 100644 index 0000000..4399124 --- /dev/null +++ b/luoo_sms/src/main/java/com/luoo/sms/SmsApplication.java @@ -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); + } +} diff --git a/luoo_sms/src/main/java/com/luoo/sms/listener/SmsListener.java b/luoo_sms/src/main/java/com/luoo/sms/listener/SmsListener.java new file mode 100644 index 0000000..249ebe2 --- /dev/null +++ b/luoo_sms/src/main/java/com/luoo/sms/listener/SmsListener.java @@ -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 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); +// } + } +} diff --git a/luoo_sms/src/main/java/com/luoo/sms/util/SmsUtils.java b/luoo_sms/src/main/java/com/luoo/sms/util/SmsUtils.java new file mode 100644 index 0000000..6505fc8 --- /dev/null +++ b/luoo_sms/src/main/java/com/luoo/sms/util/SmsUtils.java @@ -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; + } +} \ No newline at end of file diff --git a/luoo_sms/src/main/resources/application.yml b/luoo_sms/src/main/resources/application.yml new file mode 100644 index 0000000..267731c --- /dev/null +++ b/luoo_sms/src/main/resources/application.yml @@ -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 \ No newline at end of file diff --git a/luoo_user/pom.xml b/luoo_user/pom.xml index 3647629..b327618 100644 --- a/luoo_user/pom.xml +++ b/luoo_user/pom.xml @@ -15,6 +15,14 @@ mysql mysql-connector-java + + org.springframework.boot + spring-boot-starter-data-redis + + + org.springframework.boot + spring-boot-starter-amqp + com.luoo luoo_common diff --git a/luoo_user/src/main/java/com/luoo/user/controller/UserController.java b/luoo_user/src/main/java/com/luoo/user/controller/UserController.java index 1545759..78dd190 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/UserController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/UserController.java @@ -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,"注册成功"); } diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserService.java b/luoo_user/src/main/java/com/luoo/user/service/UserService.java index 45f6363..d23540b 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserService.java @@ -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 map = new HashMap<>(); + map.put("mobile",mobile); + map.put("checkcode",checkcode); + rabbitTemplate.convertAndSend("sms",map); // 在控制台放一份(方便测试) diff --git a/luoo_user/src/main/resources/application.yml b/luoo_user/src/main/resources/application.yml index 078b9d3..a95706d 100644 --- a/luoo_user/src/main/resources/application.yml +++ b/luoo_user/src/main/resources/application.yml @@ -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 diff --git a/pom.xml b/pom.xml index b8d3920..aef9279 100644 --- a/pom.xml +++ b/pom.xml @@ -17,6 +17,7 @@ luoo_friend luoo_manage luoo_web + luoo_sms pom luoo_parent