使用activeMQ发送短信验证码

package cn.itcast.bos.web.action;
/**
 * 1.获取用户电话号码,生成4位数的验证(随机数),保存到session中,用吉信通发送验证码给客户
 * 2.客户点击注册,获取用户页面填写的验证码,得到之前session存储的验证码,进行比较,若不同,结束action;
 * 3.若验证码相同,则生成32位数的激活码(随机数),将激活码把存到redis(key值就是用户的电话号码)中,设置保存时间为24小时,然后发送激活邮件给用户填写的邮箱;
 * 4.在激活邮件中的地址,拼入的数据有电话号码和激活码
 * 5.激活邮件:需要考虑用户激活码是否正确,是否重复激活,若激活成功,将customer中的type字段改为1(未激活:为null)
 * */
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.TimeUnit;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang.math.RandomUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.utils.MailUtils;
import cn.itcast.crm.domain.Customer;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class CustomerAction extends ActionSupport implements
		ModelDriven<Customer> {

	private Customer customer = new Customer();

	@Override
	public Customer getModel() {
		return customer;
	}
	//注入jmsTemplate
	@Autowired
	@Qualifier("jmsQueueTemplate")
	private JmsTemplate jmsTemplate;

	@Action(value = "customer_sendSms")
	public String sendSms() throws UnsupportedEncodingException {
		// 手机号保存在Customer对象当中
		// 生成短信验证码
		String randomNumeric = RandomStringUtils.randomNumeric(4);
		// 将短信验证码保存到session当中
		ServletActionContext.getRequest().getSession()
				.setAttribute(customer.getTelephone(), randomNumeric);
		// 在控制台输出生成的验证码
		System.out.println(randomNumeric);

		// 编辑短信的内容
		final String msg = "hello,your number" + randomNumeric + "服务电话:88888888";
		System.out.println(msg);
		
		//调用MQ发送短信(使用第三方接口来发送短信:bos_sms)
		jmsTemplate.send("bos_sms",new MessageCreator() {
			@Override
			public Message createMessage(Session session) throws JMSException {
				MapMessage mapMessage = session.createMapMessage();
				mapMessage.setString("telephone", customer.getTelephone());
				mapMessage.setString("msg", msg);
				return mapMessage;
				
			}
		});
		return NONE;
		/*
		 * 
		 *		不使用MQ来发送短信验证码
		 *
		// 调用sms服务发送短信
		// 为了方便测试,不进行发送短信
		// String sendSmsByHTTP =
		// SmsUtils.sendSmsByHTTP(customer.getTelephone(), msg);

		String sendSmsByHTTP = "000/***";
		// 发送短信若返回的是"000",则表明发送成功
		if (sendSmsByHTTP.startsWith("000")) {
			// 发送成功
			return NONE;
		} else {
			throw new RuntimeException("发送失败" + sendSmsByHTTP);
		}
		 **/
	}
	// 点击注册,验证验证码是否正确,以及发送激活邮件

	// 1.验证验证码

	// 属性驱动
	private String checkcode;

	public void setCheckcode(String checkcode) {
		this.checkcode = checkcode;
	}

	@Autowired
	private RedisTemplate<String, String> redisTemplate;// 用于验证激活码(将激活码存储到redis)

	@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(customer.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(customer);
		System.out.println("验证码正确,注册成功");

		// 2.发送激活邮件
		// 生成激活码
		String randomNumeric = RandomStringUtils.randomNumeric(32);
		// 将激活码保存到redis当中,设置时间为24小时
		System.out.println(customer.getTelephone());
		System.out.println(randomNumeric);
		redisTemplate.opsForValue().set(customer.getTelephone(), randomNumeric,
				24, TimeUnit.HOURS);

		// 调用MailUtils发送邮件
		String content = "尊敬的客户您好,请于24小时内,进行邮箱账户的绑定,点击下面地址完成绑定:<br/><a href='"
				+ MailUtils.activeUrl + "?telephone=" + customer.getTelephone()
				+ "&activecode=" + randomNumeric + "'>速运快递邮箱绑定地址</a>";
		MailUtils.sendMail("木子快递", content, customer.getEmail());
		return SUCCESS;
	}

	// 用户激活邮件
	// 得到注册码activecode(属性驱动)
	private String activecode;

	public void setActivecode(String activecode) {
		this.activecode = activecode;
	}

	@Action(value = "customer_activeMail")
	public String activeMail() throws IOException {
		ServletActionContext.getResponse().setContentType(
				"text/html;charset=utf-8");
		// 获取数据库(本地的)激活码
		String randomNumeric = redisTemplate.opsForValue().get(
				customer.getTelephone());
		if (randomNumeric == null || !randomNumeric.equals(activecode)) {
			// 激活码为空,或者与本地的激活码不一致-->激活失败
			ServletActionContext.getResponse().getWriter().print("激活码错误,请重新注册");
		} else {
		
			// 激活码正确,进行判断用户是否重复激活
			Customer customer2 = WebClient
					.create("http://localhost:9002/crm_management/services"
							+ "/customerService/customer/telephone/"
							+ customer.getTelephone())
					.accept(MediaType.APPLICATION_JSON).get(Customer.class);
			if(customer2.getType()==null || customer2.getType() != 1){
				//激活码为激活,调用CRM进行激活
				WebClient
				.create("http://localhost:9002/crm_management/services"
						+ "/customerService/customer/updatetype/"
						+ customer.getTelephone()).get();
				ServletActionContext.getResponse().getWriter().print("激活成功");
			}else{
				ServletActionContext.getResponse().getWriter().print("已经激活了,不需要在进行激活");
			}
			//激活成功后删除对应的激活码
			redisTemplate.delete(customer.getTelephone());
		}
		return NONE;
	}

}

1.页面点击'获取验证码',在Customer处理业务,得到一个4位随机数作为验证码,保存到session域当中,用于注册时校验验证码是否正确.

2.发送短信只需要将电话号码,短信内容交于'bos_sms'接口进行发送(运用MQ).

package cn.itcast.bos.mq;

import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;

import org.springframework.stereotype.Service;


@Service("smsConsumer")
public class SmsConsumer implements MessageListener {
	
	@Override
	public void onMessage(Message message) {
		MapMessage mapMessage = (MapMessage) message;
		try {
		
			/*
			 * String result = SmsUtils.sendSmsByHTTP(
			 * mapMessage.getString("telephone"), mapMessage.getString("msg"));
			 */

			// 为了测试方便,不进行发送短信;
			String result = "000/***";
			if (result.startsWith("000")) {
				System.out.println("电话号码:" + mapMessage.getString("telephone")
						+ "==验证码:" + mapMessage.getString("msg")
						+ "==============发送成功");
			} else {
				// 发送失败
				throw new RuntimeException("发送失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


3.如若短息发送成功,则会返回'000'字符串,因此,为了方便测试,将返回值直接写成'000/***'






  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值