重构客户注册-基于ActiveMQ实现短信验证码生产者

重构目标:将bos_fore项目中的CustomerAction作为短信消息生产者,将消息发给ActiveMQ,创建一个单独的SMS项目,作为短信息的消费者,从ActiveMQ获取短信消息,调用第三方接口完成短信发送。 
CustomerAction完整代码:

  1 @ParentPackage("json-default")
  2 @Namespace("/")
  3 @Controller
  4 @Scope("prototype")
  5 public class CustomerAction extends BaseAction<Customer> {
  6     @Autowired
  7     @Qualifier("jmsQueueTemplate")
  8     private JmsTemplate jmsTemplate;
  9
 10     @Action(value = "customer_sendSms")
 11     public String sendSms() throws IOException {
 12         // 手机号保存在Customer对象
 13         // 生成短信验证码
 14         String randomCode = RandomStringUtils.randomNumeric(4);
 15         // 将短信验证码 保存到session
 16         ServletActionContext.getRequest().getSession()
 17                 .setAttribute(model.getTelephone(), randomCode);
 18
 19         System.out.println("生成手机验证码为:" + randomCode);
 20         // 编辑短信内容
 21         final String msg = "尊敬的用户您好,本次获取的验证码为:" + randomCode
 22                 + ",服务电话:4007654321";
 23
 24         // 调用MQ服务,发送一条消息
 25         jmsTemplate.send("bos_sms", new MessageCreator() {
 26             @Override
 27             public Message createMessage(Session session) throws JMSException {
 28                 MapMessage mapMessage = session.createMapMessage();
 29                 mapMessage.setString("telephone", model.getTelephone());
 30                 mapMessage.setString("msg", msg);
 31                 return mapMessage;
 32             }
 33         });
 34         return NONE;
 35
 36     }
 37
 38     // 属性驱动
 39     private String checkcode;
 40
 41     public void setCheckcode(String checkcode) {
 42         this.checkcode = checkcode;
 43     }
 44
 45     @Autowired
 46     private RedisTemplate<String, String> redisTemplate;
 47
 48     @Action(value = "customer_regist", results = {
 49             @Result(name = "success", type = "redirect", location = "signup-success.html"),
 50             @Result(name = "input", type = "redirect", location = "signup.html") })
 51     public String regist() {
 52         // 先校验短信验证码,如果不通过,调回注册页面
 53         // 从session获取 之前生成验证码
 54         String checkcodeSession = (String) ServletActionContext.getRequest()
 55                 .getSession().getAttribute(model.getTelephone());
 56         if (checkcodeSession == null || !checkcodeSession.equals(checkcode)) {
 57             System.out.println("短信验证码错误...");
 58             // 短信验证码错误
 59             return INPUT;
 60         }
 61         // 调用webService 连接CRM 保存客户信息
 62         WebClient
 63                 .create("http://localhost:9002/crm_management/services"
 64                         + "/customerService/customer")
 65                 .type(MediaType.APPLICATION_JSON).post(model);
 66         System.out.println("客户注册成功...");
 67
 68         // 发送一封激活邮件
 69         // 生成激活码
 70         String activecode = RandomStringUtils.randomNumeric(32);
 71
 72         // 将激活码保存到redis,设置24小时失效
 73         redisTemplate.opsForValue().set(model.getTelephone(), activecode, 24,
 74                 TimeUnit.HOURS);
 75
 76         // 调用MailUtils发送激活邮件
 77         String content = "尊敬的客户您好,请于24小时内,进行邮箱账户的绑定,点击下面地址完成绑定:<br/><a href=‘"
 78                 + MailUtils.activeUrl + "?telephone=" + model.getTelephone()
 79                 + "&activecode=" + activecode + "‘>速运快递邮箱绑定地址</a>";
 80         MailUtils.sendMail("速运快递激活邮件", content, model.getEmail());
 81
 82         return SUCCESS;
 83     }
 84
 85     // 属性驱动
 86     private String activecode;
 87
 88     public void setActivecode(String activecode) {
 89         this.activecode = activecode;
 90     }
 91
 92     @Action("customer_activeMail")
 93     public String activeMail() throws IOException {
 94         ServletActionContext.getResponse().setContentType(
 95                 "text/html;charset=utf-8");
 96         // 判断激活码是否有效
 97         String activecodeRedis = redisTemplate.opsForValue().get(
 98                 model.getTelephone());
 99         if (activecodeRedis == null || !activecodeRedis.equals(activecodeRedis)) {
100             // 激活码无效
101             ServletActionContext.getResponse().getWriter()
102                     .println("激活码无效,请登录系统,重新绑定邮箱!");
103         } else {
104             // 激活码有效
105             // 防止重复绑定
106             // 调用CRM webService 查询客户信息,判断是否已经绑定
107             Customer customer = WebClient
108                     .create("http://localhost:9002/crm_management/services"
109                             + "/customerService/customer/telephone/"
110                             + model.getTelephone())
111                     .accept(MediaType.APPLICATION_JSON).get(Customer.class);
112             if (customer.getType() == null || customer.getType() != 1) {
113                 // 没有绑定,进行绑定
114                 WebClient.create(
115                         "http://localhost:9002/crm_management/services"
116                                 + "/customerService/customer/updatetype/"
117                                 + model.getTelephone()).get();
118                 ServletActionContext.getResponse().getWriter()
119                         .println("邮箱绑定成功!");
120             } else {
121                 // 已经绑定过
122                 ServletActionContext.getResponse().getWriter()
123                         .println("邮箱已经绑定过,无需重复绑定!");
124             }
125
126             // 删除redis的激活码
127             redisTemplate.delete(model.getTelephone());
128         }
129         return NONE;
130     }
131
132 }

spring的配置文件applicationContext-mq.xml完整代码:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
 6     xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task"
 7     xmlns:amq="http://activemq.apache.org/schema/core"
 8     xmlns:jms="http://www.springframework.org/schema/jms"
 9     xsi:schemaLocation="
10         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
11         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
12         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
13         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
14         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
15         http://www.springframework.org/schema/data/jpa
16         http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
17         http://www.springframework.org/schema/jms
18         http://www.springframework.org/schema/jms/spring-jms.xsd
19         http://activemq.apache.org/schema/core
20         http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd ">
21
22     <!-- ActiveMQ 连接工厂 -->
23     <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
24     <!-- 如果连接网络:tcp://ip:61616;未连接网络:tcp://localhost:61616 以及用户名,密码-->
25     <amq:connectionFactory id="amqConnectionFactory"
26         brokerURL="tcp://localhost:61616" userName="admin" password="admin"  />
27
28     <!-- Spring Caching连接工厂 -->
29     <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
30     <bean id="mqConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
31         <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
32         <property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
33         <!-- 同上,同理 -->
34         <!-- <constructor-arg ref="amqConnectionFactory" /> -->
35         <!-- Session缓存数量 -->
36         <property name="sessionCacheSize" value="100" />
37     </bean>
38
39      <!-- Spring JmsTemplate 的消息生产者 start-->
40
41     <!-- 定义JmsTemplate的Queue类型 -->
42     <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
43         <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
44         <constructor-arg ref="mqConnectionFactory" />
45         <!-- 非pub/sub模型(发布/订阅),即队列模式 -->
46         <property name="pubSubDomain" value="false" />
47     </bean>
48
49     <!-- 定义JmsTemplate的Topic类型 -->
50     <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
51          <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
52         <constructor-arg ref="mqConnectionFactory" />
53         <!-- pub/sub模型(发布/订阅) -->
54         <property name="pubSubDomain" value="true" />
55     </bean>
56
57     <!--Spring JmsTemplate 的消息生产者 end-->
58 </beans>

maven的pom文件完整代码:

 1 <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">
 2   <modelVersion>4.0.0</modelVersion>
 3
 4   <parent>
 5      <groupId>cn.niwotaxuexiba.maven</groupId>
 6      <artifactId>common_parent</artifactId>
 7      <version>0.0.1-SNAPSHOT</version>
 8   </parent>
 9
10   <artifactId>bos_fore</artifactId>
11   <packaging>war</packaging>
12   <name>bos_fore</name>
13   <description>物流前端系统</description>
14
15   <build>
16     <plugins>
17         <plugin>
18             <groupId>org.codehaus.mojo</groupId>
19             <artifactId>tomcat-maven-plugin</artifactId>
20             <version>1.1</version>
21             <configuration>
22                 <port>9003</port>
23             </configuration>
24         </plugin>
25         <plugin>
26             <groupId>org.apache.maven.plugins</groupId>
27             <artifactId>maven-compiler-plugin</artifactId>
28             <version>2.3.2</version>
29             <configuration>
30                 <source>1.8</source>
31                 <target>1.8</target>
32             </configuration>
33         </plugin>
34     </plugins>
35   </build>
36   <dependencies>
37     <dependency>
38         <groupId>cn.niwotaxuexiba.maven</groupId>
39         <artifactId>crm_domain</artifactId>
40         <version>0.0.1-SNAPSHOT</version>
41     </dependency>
42   </dependencies>
43 </project>
时间: 2024-08-28 15:08:03

重构客户注册-基于ActiveMQ实现短信验证码生产者的相关文章

客户注册功能,发短信功能分离 通过ActiveMQ实现

客户注册功能,发短信功能分离 通过ActiveMQ 配置链接工厂, 配置session缓存工厂(引入链接工厂) 2.配置模板对象JmsTemplate 引入缓存工厂    指定消息模式(队列,发布和订阅) 3. 使用注解方式将模板对象注入给Action,直接发送消息("队列名称与配置监听的队列名相同",内部类MessageCreator(实现createMessage(参数是session))) 4在Spring配置文件 配置链接工厂, 配置session缓存工厂(引入链接工厂) 配置

各大APP注册时发送短信验证码是怎么实现的?

回答这个问题可以从多个角度来回答,比如商务角度和技术角度,为了快速清晰的让广大的读者了解这个过程,本文我们从商务角度来深入分析. 实现原理 现各大APP发送短信的服务一般是由第三方短信服务商提供的,他们整合了移动,联通.电信三方资源,三网都可以发送,这样就不需要直接对接运营商了. 发送短信验证码主要是为了验证手机方的真实性,实现原理简单说就是系统先生成一个验证码,调用第三方服务商的短信接口,发送到手机方,手机方输入验证码,再由系统去校验是否符合,符合则说明手机真实有效. 服务商选择 提供短信接入

实现短信验证码注册

今晚研究了下短信验证码注册,很简单,用的是  云通讯的短信系统(收费的,不过有测试的api给我们做测试).好了,不多说,进入正题. 1.收到到云通讯短信系统注册账号,然后下载他们的封装好的短信api接口代码,解压,然后找到CCPRestSDK.php文件和SendTemplateSMS.php文件,将其拉到根目录文件夹里: 2.打开SendTemplateSMS.php文件,首先注意include_once('./CCPRestSDK.php'),千万别包含错路径了,将云通讯给的测试主账号,主账

SSH2框架实现注册发短信验证码实例

这两天开始敲代码了,让用SSH2框架,以前没有接触过Java项目更没有接触过SSH2框架,所以用注册开始了我Java之旅.后来发现,后台代码挺容易理解的,跟.net的差不多,就是层与层之间的调用,但是前面前台的交互我差很多,在这里总结一下,顺便跟大家看一下,怎么实现往手机上发送短信验证码的.. 大家先看看我的界面. 原图: 短信验证码错误的界面: 短信验证码正确的界面: 下面开始我的界面代码展示(JSP): <body > <h2 class="titlelog"&g

在ASP.NET MVC下通过短信验证码注册

以前发短信使用过短信猫,现在,更多地是使用第三方API.大致过程是: → 用户在页面输入手机号码→ 用户点击"获取验证码"按钮,把手机号码发送给服务端,服务端产生几位数的随机码,并保存在某个地方(Session, Applicaiton, 数据库, 等等),调用第三方的API→ 第三方发送几位数的随机码至用户手机→ 用户在页面输入接收到的随机码→ 把随机码等发送给服务端,与服务端保存的随机码比较,如果一致,就通过,让注册 就按如下界面来说吧: 我们需要考虑的方面包括: ● 手机号码:判

Web项目中手机注册短信验证码实现的全流程及代码

最近在做只能净化器的后台用户管理系统,需要使用手机号进行注册,找了许久才大致了解了手机验证码实现流程,今天在此和大家分享一下. 我们使用的是榛子云短信平台, 官网地址:http://smsow.zhenzikj.com 我是java开发者,后端使用了springMvc,前端用的是jsp + jquery 短信验证码实现流程 1.构造手机验证码,生成一个6位的随机数字串:2.使用接口向短信平台发送手机号和验证码,然后短信平台再把验证码发送到制定手机号上3.将手机号验证码.操作时间存入Session

JQ-用户注册用到的图形验证码和短信验证码点击事件

// 点击切换图形验证码 页面加载完后执行,类似window.onload $(function () { var imgCaptcha = $(".img-captcha"); imgCaptcha.click(function () { imgCaptcha.attr("src", "/account/register/img/captcha"+"?random="+Math.random()); }); }); // 点

短信验证码如何对接ECShop_V3.0

找到了一家不错的短信插件,有需要对接的可以查看学习,在这边分享一下,有需要的可以详细看看,了解一下.http://www.ihuyi.com/插件说明安装前请详细阅读该说明.本插件基于ecshop官方3.0版本,使用的页面模板是默认模板.如果您的ecshop是二次开发过的,建议手动修改代码的方式安装插件,避免直接安装覆盖了您二开过的文件. 功能介绍1.手机号短信验证注册2.支持手机号登入3.会员(重新)绑定手机号4.没有绑定手机的会员必须绑定手机才能下订单5.管理后台用户列表增加快速发送短信链接

Yii2在Form中处理短信验证码的Validator,耦合度最低的短信验证码验证方式

短信验证码在目前大多数web应用中都会有,本文介绍一个基于Yii2 Validator方式的验证码验证方式. 在其他文章中看到的方式大多比较难做到一次封装,多次重用. 使用此方式的好处自然不用多说,Validator支持在Model和Form中使用,使用的时候只需要在rules中添加一条验证规则即可. 第一步: 准备数据表,用来存储短信验证码 CREATE TABLE `tbl_sms_log` ( `id` int(11) NOT NULL AUTO_INCREMENT, `to` varch