重构客户注册-基于ActiveMQ实现短信验证码生产者
重构目标:将bos_fore项目中的CustomerAction作为短信消息生产者,将消息发给ActiveMQ,创建一个单独的SMS项目,作为短信息的消费者,从ActiveMQ获取短信消息,调用第三方接口完成短信发送。
CustomerAction完整代码:
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class CustomerAction extends BaseAction<Customer> {
@Autowired
@Qualifier("jmsQueueTemplate")
private JmsTemplate jmsTemplate;
@Action(value = "customer_sendSms")
public String sendSms() throws IOException {
// 手机号保存在Customer对象
// 生成短信验证码
String randomCode = RandomStringUtils.randomNumeric(4);
// 将短信验证码 保存到session
ServletActionContext.getRequest().getSession()
.setAttribute(model.getTelephone(), randomCode);
System.out.println("生成手机验证码为:" + randomCode);
// 编辑短信内容
final String msg = "尊敬的用户您好,本次获取的验证码为:" + randomCode
+ ",服务电话:4007654321";
// 调用MQ服务,发送一条消息
jmsTemplate.send("bos_sms", new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
MapMessage mapMessage = session.createMapMessage();
mapMessage.setString("telephone", model.getTelephone());
mapMessage.setString("msg", msg);
return mapMessage;
}
});
return NONE;
}
// 属性驱动
private String checkcode;
public void setCheckcode(String checkcode) {
this.checkcode = checkcode;
}
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Action(value = "customer_regist", results = {
@Result(name = "success", type = "redirect", location = "signup-success.html"),
@Result(name = "input", type = "redirect", location = "signup.html") })
public String regist() {
// 先校验短信验证码,如果不通过,调回注册页面
// 从session获取 之前生成验证码
String checkcodeSession = (String) ServletActionContext.getRequest()
.getSession().getAttribute(model.getTelephone());
if (checkcodeSession == null || !checkcodeSession.equals(checkcode)) {
System.out.println("短信验证码错误...");
// 短信验证码错误
return INPUT;
}
// 调用webService 连接CRM 保存客户信息
WebClient
.create("http://localhost:9002/crm_management/services"
+ "/customerService/customer")
.type(MediaType.APPLICATION_JSON).post(model);
System.out.println("客户注册成功...");
// 发送一封激活邮件
// 生成激活码
String activecode = RandomStringUtils.randomNumeric(32);
// 将激活码保存到redis,设置24小时失效
redisTemplate.opsForValue().set(model.getTelephone(), activecode, 24,
TimeUnit.HOURS);
// 调用MailUtils发送激活邮件
String content = "尊敬的客户您好,请于24小时内,进行邮箱账户的绑定,点击下面地址完成绑定:<br/><a href='"
+ MailUtils.activeUrl + "?telephone=" + model.getTelephone()
+ "&activecode=" + activecode + "'>速运快递邮箱绑定地址</a>";
MailUtils.sendMail("速运快递激活邮件", content, model.getEmail());
return SUCCESS;
}
// 属性驱动
private String activecode;
public void setActivecode(String activecode) {
this.activecode = activecode;
}
@Action("customer_activeMail")
public String activeMail() throws IOException {
ServletActionContext.getResponse().setContentType(
"text/html;charset=utf-8");
// 判断激活码是否有效
String activecodeRedis = redisTemplate.opsForValue().get(
model.getTelephone());
if (activecodeRedis == null || !activecodeRedis.equals(activecodeRedis)) {
// 激活码无效
ServletActionContext.getResponse().getWriter()
.println("激活码无效,请登录系统,重新绑定邮箱!");
} else {
// 激活码有效
// 防止重复绑定
// 调用CRM webService 查询客户信息,判断是否已经绑定
Customer customer = WebClient
.create("http://localhost:9002/crm_management/services"
+ "/customerService/customer/telephone/"
+ model.getTelephone())
.accept(MediaType.APPLICATION_JSON).get(Customer.class);
if (customer.getType() == null || customer.getType() != 1) {
// 没有绑定,进行绑定
WebClient.create(
"http://localhost:9002/crm_management/services"
+ "/customerService/customer/updatetype/"
+ model.getTelephone()).get();
ServletActionContext.getResponse().getWriter()
.println("邮箱绑定成功!");
} else {
// 已经绑定过
ServletActionContext.getResponse().getWriter()
.println("邮箱已经绑定过,无需重复绑定!");
}
// 删除redis的激活码
redisTemplate.delete(model.getTelephone());
}
return NONE;
}
}
spring的配置文件applicationContext-mq.xml完整代码:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task"
xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd "> <!-- ActiveMQ 连接工厂 -->
<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
<!-- 如果连接网络:tcp://ip:61616;未连接网络:tcp://localhost:61616 以及用户名,密码-->
<amq:connectionFactory id="amqConnectionFactory"
brokerURL="tcp://localhost:61616" userName="admin" password="admin" /> <!-- Spring Caching连接工厂 -->
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="mqConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
<!-- 同上,同理 -->
<!-- <constructor-arg ref="amqConnectionFactory" /> -->
<!-- Session缓存数量 -->
<property name="sessionCacheSize" value="100" />
</bean> <!-- Spring JmsTemplate 的消息生产者 start--> <!-- 定义JmsTemplate的Queue类型 -->
<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
<constructor-arg ref="mqConnectionFactory" />
<!-- 非pub/sub模型(发布/订阅),即队列模式 -->
<property name="pubSubDomain" value="false" />
</bean> <!-- 定义JmsTemplate的Topic类型 -->
<bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
<constructor-arg ref="mqConnectionFactory" />
<!-- pub/sub模型(发布/订阅) -->
<property name="pubSubDomain" value="true" />
</bean> <!--Spring JmsTemplate 的消息生产者 end-->
</beans>
maven的pom文件完整代码:
<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">
<modelVersion>4.0.0</modelVersion> <parent>
<groupId>cn.niwotaxuexiba.maven</groupId>
<artifactId>common_parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent> <artifactId>bos_fore</artifactId>
<packaging>war</packaging>
<name>bos_fore</name>
<description>物流前端系统</description> <build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<port>9003</port>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>cn.niwotaxuexiba.maven</groupId>
<artifactId>crm_domain</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
重构客户注册-基于ActiveMQ实现短信验证码生产者的更多相关文章
- 项目一:在线下单(补充) activeMQ使用(重点) 重构客户注册功能,发短信功能分离
1 课程计划 1.在线下单(补充) 2.activeMQ使用(重点) n 简介和安装 n activeMQ入门案例 n spring整合activeMQ应用 3.重构客户注册功能,发短信功能分离 n ...
- 客户注册功能,发短信功能分离 通过ActiveMQ实现
客户注册功能,发短信功能分离 通过ActiveMQ 配置链接工厂, 配置session缓存工厂(引入链接工厂) 2.配置模板对象JmsTemplate 引入缓存工厂 指定消息模式(队列,发布和订 ...
- 基于PHP实现短信验证码接口的方法
步骤: 1.登录荣联运通讯注册获取ACCOUNT SID.AUTH TOKEN.Rest URL(生产).AppID(默认): 2.注册测试用手机号码(先注册测试号码方可使用): 3.下载demo示例 ...
- PHP 手机短信验证码 laravel 实现流程
https://blog.csdn.net/uknow0904/article/details/80336941 本人在自己博客(Laravel)的注册部分 使用手机号注册,需要发送短信验证码. 使用 ...
- java springboot activemq 邮件短信微服务,解决国际化服务的国内外兼容性问题,含各服务商调研情况
java springboot activemq 邮件短信微服务,解决国际化服务的国内外兼容性问题,含各服务商调研情况 邮件短信微服务 spring boot 微服务 接收json格式参数 验证参数合 ...
- day101:MoFang:模型构造器ModelSchema&注册功能之手机号唯一验证/保存用户注册信息/发送短信验证码
目录 1.模型构造器:ModelSchema 1.SQLAlchemySchema 2.SQLAlchemyAutoSchema 2.注册功能基本实现 1.关于手机号码的唯一性验证 2.保存用户注册信 ...
- day102:MoFang:后端完成对短信验证码的校验&基于celery完成异步短信发送&flask_jwt_extended&用户登录的API接口
目录 1.用户注册 1.后端完成对短信验证码的校验 2.基于celery实现短信异步发送 2.用户登录 1.jwt登录验证:flask_jwt_extended 2.服务端提供用户登录的API接口 1 ...
- python基于LeanCloud的短信验证
python基于LeanCloud的短信验证 1. 获取LeanCloud的Id.Key 2. 安装Flask框架和Requests库 pip install flask pip install re ...
- java实现注册的短信验证码
最近在做只能净化器的后台用户管理系统,需要使用手机号进行注册,找了许久才大致了解了手机验证码实现流程,今天在此和大家分享一下. 我们使用的是榛子云短信平台, 官网地址:http://smsow.zhe ...
随机推荐
- centos 防火墙的操作
systemctl start firewalld systemctl restart firewalld systemctl status firewalld systemctl enable fi ...
- python 之常用模块
一 认识模块 二 常用模块 (1)re模块 (2)collections模块 一 认识模块 (1)什么是模块 (2)模块的导入和使用 (1)模块是:一个模块就是一个包含 ...
- EBS採购模块中的高速接收和高速接收事务
EBS採购模块中的高速接收和高速接收事务 (版权声明.本人原创或者翻译的文章如需转载,如转载用于个人学习,请注明出处.否则请与本人联系,违者必究) 高速功能是一个高速输入收货和接收事务的方法. 在收货 ...
- centos7下安装docker(18.3docker日志---logging driver---fluentd)
前面我们学的ELK中用filebeat收集docker容器日志,利用的是dcoker默认的logging driver json-file,下面我们用fluentd来收集容器日志 Fluentd是一个 ...
- [Python] 个人TIPS
1.查询当前代码路径 import os os.getcwd() 2.尝试大文件读取时,可对pandas显示设置进行调整,使之得到更为全局或者局部的细节显示 pd.options.display.ma ...
- move或rebuild lob分区对象
当使用如下语法移动或重建一个lob分区对象,将报错: SQL> alter table SHOW_LOB_STORAGE move lob(DDD) store as (tablespace P ...
- 【转】CocoaPods的使用教程
转载自:https://www.jianshu.com/p/dfe970588f95 前言 前几天发布我的开源库<最简单方便的iOS轮播开源库:JYCarousel>到CocoaPods的 ...
- Spring Security(二十八):9.4 Authentication in a Web Application
Now let’s explore the situation where you are using Spring Security in a web application (without we ...
- [MicroPython]TurnipBit开发板DIY自动浇水系统
1.实验目的: ?学习在PC机系统中扩展简单I/O 接口的方法 ?学习TurnipBit拼插编程 ?学习土壤传感器的工作原理以及使用方法 2.所需原器件: TurnipBit一块 TurnipBit扩 ...
- Java内存模型(和堆栈等不是同一层次的划分)
什么叫Java内存模型? 现代计算机通过指令的重排序来提升计算机的性能,而没有限制条件的指令重排序会使得程序的行为不可预测,JMM就是通过一系列的操作规则限制指令重排序的方式使得指令重排序不会破坏JM ...