2023年7月31日发(作者:)

MQ+短信发送(前端vue)+redis⽂章⽬录前⾔⼀、发送验证码的使⽤短信发送是电信运营商提供的服务,需要访问对应的接⼝,不同运营商提供的接⼝地址肯定不⼀样,如果直接访问这些接⼝就需要判断收信息的⼿机号属于哪个运营商,关键在于这些接⼝不对个⼈开放,还要考虑调⽤短信服务的费⽤问题因此⽬前调⽤短信业务都是使⽤第三⽅企业的短信服务,他们与运营商合作,封装了短信接⼝,调⽤⽅法,⽽且费⽤相对便宜短信服务(推荐)注册购买第⼀步:云市场搜索短信服务点击购买,有5条免费使⽤,测试也会消耗使⽤次数,⽤完了在付费购买即可找到⾃⼰购买的云服务可以看到已购买的服务这⾥有⼏项重要信息:AppKey AppSecret AppCode 调⽤购买的这个短信服务接⼝时候需要⽤到,⽤于确认使⽤者⾝份接⼝ : 调⽤该服务时访问的接⼝参考API,编写发送短信⼯具类import ;import quest;import p;import ;/** *

测试短信 */public class SmsUtilTest { public static void main(String[] args) { //⼿机号码 String mobile = ""; //验证码 String code = ""; //签名ID。(联系客服申请。测试请⽤:2e65b1bb3d054466b82f0c9d125465e2) String smsSignId=""; //模板ID。(联系客服申请。测试ID请⽤:908e94ccf08b4476ba6c876d13f084ad,短信内容为 {

验证码:**code**,**minute**分钟内有效,请勿泄漏于他⼈!}) String templateId=""; //应⽤code /imageconsole/?#/bizlist?_k=r5f9m0

查找 String appCode=""; //请求连接 String host = "/sms/smsSend"; //拼装请求体 Map querys = new HashMap(); ("mobile", mobile); ("param", "**code**:"+code+",**minute**:5"); ("smsSignId", smsSignId); ("templateId", templateId); try { String result = (host) .header(IZATION, "APPCODE " + appCode)//头信息,多个头信息多次调⽤此⽅法即可 .form(querys)//表单内容 .timeout(20000)//超时,毫秒 .execute().body(); n(result); } catch (Exception e) { tackTrace(); } }}⼆、实例前端Vue后端启动类UserController@RestController@RequestMapping("/user")public class UserController {// @Autowired// private HttpServletRequest request; @Autowired private RabbitTemplate rabbitTemplate;// RabbitTemplate rabbitTemplate; @Autowired private StringRedisTemplate redisTemplate; @PostMapping public BaseResult addUser(@RequestBody User user, @RequestHeader("Authorization") String authorization){// String token = der("authorization"); Claims claims = oken(authorization, "user"); Object userId = ("userId"); n("新增的⽤户信息:"+user); return s(null); } /** * 1

产⽣随机码6位 * 2

保存到rabbitMQ中 * 3

保存到redis中 * 4

返回发送成功 * @param phone * @return */ // @GetMapping("/sendSms") public BaseResult sendSMS(String phone){// 1

产⽣随机码6位 String code = (); //拼接消息内容 String msg = phone + "%%%%%%" + code;// 2

保存到rabbitMQ中 tAndSend("",msg);// 3

保存到redis中 Value().set(phone,code,1, S);// 4

返回发送成功 return s("成功"); } @PostMapping("/reg") public BaseResult reg(@RequestBody User user){ String s2 = ile(); String s = Value().get(s2); n(s); if (s==null){ return (); } if (e().equals(s)){ n("注册成功,"+user); (ile()); return s("成功"+user); } return (); }}配置⽂件ring: datasource: url: jdbc:mysql://localhost/数据库表名?useSSL=false username: 账户名 password: 密码 driver-class-name: application: //服务名 name: userservice profiles: active: dev cloud: nacos: server-addr: localhost:8848 config: file-extension: yaml# rabbitmq:# host:# 主机名# port: 5672 # 端⼝# virtual-host: / # 虚拟主机# username: # ⽤户名# password: # 密码 redis: host: localhost port: 6379 database: 0 jedis: pool: max-idle: 8 min-idle: 0 max-active: 8server: port: 8000SmsListener同步redis中的值@Componentpublic class SmsListener { @RabbitListener(queues = "") public void listenerWorkQueue1(String msg) throws Exception{ String[] vals = ("%%%%%%"); //这⾥调⽤短信接⼝ (vals[0],vals[1]);

n("sms接收到的消息:【"+msg+"】发送成功"); }}短信接⼝类public class AliyunSendSmsUtil { /** * * @param to

⼿机号码 * @param code

验证码 */ public static void send(String to, String code) { String accessKeyId=""; String accessSecret = ""; DefaultProfile profile = file("cn-hangzhou", accessKeyId, accessSecret); IAcsClient client = new DefaultAcsClient(profile); CommonRequest request = new CommonRequest(); Method(); Domain(""); Version("2017-05-25"); Action("SendSms"); ryParameter("RegionId", "cn-hangzhou"); ryParameter("PhoneNumbers", to); ryParameter("SignName", ""); ryParameter("TemplateCode", "SMS_85550034"); ryParameter("TemplateParam", "{"code":""+code+""}"); try { CommonResponse response = monResponse(request); n(a()); } catch (ServerException e) { tackTrace(); } catch (ClientException e) { tackTrace(); } } }启动类@SpringBootApplicationpublic class SmsConsumerApplication { @Bean public Queue createSmsQueue(){ return new Queue(""); }

public static void main(String[] args) { (, args); }}配置⽂件ver:port: 20000spring:rabbitmq:host: # 主机名port: 5672 # 端⼝virtual-host: / # 虚拟主机username: # ⽤户名password: # 密码2.项⽬结构及pom mysql mysql-connector-java e jwt-common03 0.0.1-SNAPSHOT spring-boot-starter-web spring-cloud-starter-alibaba-nacos-discovery spring-cloud-starter-alibaba-nacos-config spring-boot-starter-amqp s jedis 2.9.0 spring-boot-starter-data-redis

2023年7月31日发(作者:)

MQ+短信发送(前端vue)+redis⽂章⽬录前⾔⼀、发送验证码的使⽤短信发送是电信运营商提供的服务,需要访问对应的接⼝,不同运营商提供的接⼝地址肯定不⼀样,如果直接访问这些接⼝就需要判断收信息的⼿机号属于哪个运营商,关键在于这些接⼝不对个⼈开放,还要考虑调⽤短信服务的费⽤问题因此⽬前调⽤短信业务都是使⽤第三⽅企业的短信服务,他们与运营商合作,封装了短信接⼝,调⽤⽅法,⽽且费⽤相对便宜短信服务(推荐)注册购买第⼀步:云市场搜索短信服务点击购买,有5条免费使⽤,测试也会消耗使⽤次数,⽤完了在付费购买即可找到⾃⼰购买的云服务可以看到已购买的服务这⾥有⼏项重要信息:AppKey AppSecret AppCode 调⽤购买的这个短信服务接⼝时候需要⽤到,⽤于确认使⽤者⾝份接⼝ : 调⽤该服务时访问的接⼝参考API,编写发送短信⼯具类import ;import quest;import p;import ;/** *

测试短信 */public class SmsUtilTest { public static void main(String[] args) { //⼿机号码 String mobile = ""; //验证码 String code = ""; //签名ID。(联系客服申请。测试请⽤:2e65b1bb3d054466b82f0c9d125465e2) String smsSignId=""; //模板ID。(联系客服申请。测试ID请⽤:908e94ccf08b4476ba6c876d13f084ad,短信内容为 {

验证码:**code**,**minute**分钟内有效,请勿泄漏于他⼈!}) String templateId=""; //应⽤code /imageconsole/?#/bizlist?_k=r5f9m0

查找 String appCode=""; //请求连接 String host = "/sms/smsSend"; //拼装请求体 Map querys = new HashMap(); ("mobile", mobile); ("param", "**code**:"+code+",**minute**:5"); ("smsSignId", smsSignId); ("templateId", templateId); try { String result = (host) .header(IZATION, "APPCODE " + appCode)//头信息,多个头信息多次调⽤此⽅法即可 .form(querys)//表单内容 .timeout(20000)//超时,毫秒 .execute().body(); n(result); } catch (Exception e) { tackTrace(); } }}⼆、实例前端Vue后端启动类UserController@RestController@RequestMapping("/user")public class UserController {// @Autowired// private HttpServletRequest request; @Autowired private RabbitTemplate rabbitTemplate;// RabbitTemplate rabbitTemplate; @Autowired private StringRedisTemplate redisTemplate; @PostMapping public BaseResult addUser(@RequestBody User user, @RequestHeader("Authorization") String authorization){// String token = der("authorization"); Claims claims = oken(authorization, "user"); Object userId = ("userId"); n("新增的⽤户信息:"+user); return s(null); } /** * 1

产⽣随机码6位 * 2

保存到rabbitMQ中 * 3

保存到redis中 * 4

返回发送成功 * @param phone * @return */ // @GetMapping("/sendSms") public BaseResult sendSMS(String phone){// 1

产⽣随机码6位 String code = (); //拼接消息内容 String msg = phone + "%%%%%%" + code;// 2

保存到rabbitMQ中 tAndSend("",msg);// 3

保存到redis中 Value().set(phone,code,1, S);// 4

返回发送成功 return s("成功"); } @PostMapping("/reg") public BaseResult reg(@RequestBody User user){ String s2 = ile(); String s = Value().get(s2); n(s); if (s==null){ return (); } if (e().equals(s)){ n("注册成功,"+user); (ile()); return s("成功"+user); } return (); }}配置⽂件ring: datasource: url: jdbc:mysql://localhost/数据库表名?useSSL=false username: 账户名 password: 密码 driver-class-name: application: //服务名 name: userservice profiles: active: dev cloud: nacos: server-addr: localhost:8848 config: file-extension: yaml# rabbitmq:# host:# 主机名# port: 5672 # 端⼝# virtual-host: / # 虚拟主机# username: # ⽤户名# password: # 密码 redis: host: localhost port: 6379 database: 0 jedis: pool: max-idle: 8 min-idle: 0 max-active: 8server: port: 8000SmsListener同步redis中的值@Componentpublic class SmsListener { @RabbitListener(queues = "") public void listenerWorkQueue1(String msg) throws Exception{ String[] vals = ("%%%%%%"); //这⾥调⽤短信接⼝ (vals[0],vals[1]);

n("sms接收到的消息:【"+msg+"】发送成功"); }}短信接⼝类public class AliyunSendSmsUtil { /** * * @param to

⼿机号码 * @param code

验证码 */ public static void send(String to, String code) { String accessKeyId=""; String accessSecret = ""; DefaultProfile profile = file("cn-hangzhou", accessKeyId, accessSecret); IAcsClient client = new DefaultAcsClient(profile); CommonRequest request = new CommonRequest(); Method(); Domain(""); Version("2017-05-25"); Action("SendSms"); ryParameter("RegionId", "cn-hangzhou"); ryParameter("PhoneNumbers", to); ryParameter("SignName", ""); ryParameter("TemplateCode", "SMS_85550034"); ryParameter("TemplateParam", "{"code":""+code+""}"); try { CommonResponse response = monResponse(request); n(a()); } catch (ServerException e) { tackTrace(); } catch (ClientException e) { tackTrace(); } } }启动类@SpringBootApplicationpublic class SmsConsumerApplication { @Bean public Queue createSmsQueue(){ return new Queue(""); }

public static void main(String[] args) { (, args); }}配置⽂件ver:port: 20000spring:rabbitmq:host: # 主机名port: 5672 # 端⼝virtual-host: / # 虚拟主机username: # ⽤户名password: # 密码2.项⽬结构及pom mysql mysql-connector-java e jwt-common03 0.0.1-SNAPSHOT spring-boot-starter-web spring-cloud-starter-alibaba-nacos-discovery spring-cloud-starter-alibaba-nacos-config spring-boot-starter-amqp s jedis 2.9.0 spring-boot-starter-data-redis