目錄java
data:{ userEntity:{ username:'', password:'', phone:'', smscode:'' }, password2:'' }, <div class="control-group"> <label class="control-label">用戶名:</label> <div class="controls"> <input type="text" placeholder="請輸入你的用戶名" v-model="userEntity.username" class="input-xfat input-xlarge"> </div> </div> <div class="control-group"> <label class="control-label">登陸密碼:</label> <div class="controls"> <input type="password" placeholder="設置登陸密碼" v-model="userEntity.password" class="input-xfat input-xlarge"> </div> </div> <div class="control-group"> <label class="control-label">確認密碼:</label> <div class="controls"> <input type="password" placeholder="再次確認密碼" v-model="password2" class="input-xfat input-xlarge"> </div> </div> <div class="control-group"> <label class="control-label">手機號:</label> <div class="controls"> <input type="text" placeholder="請輸入你的手機號" class="input-xfat input-xlarge"> </div> </div> <div class="control-group"> <label class="control-label">短信驗證碼:</label> <div class="controls"> <input type="text" placeholder="短信驗證碼" v-model="userEntity.smscode" class="input-xfat input-xlarge"> <a href="#" @click="sendCode()">獲取短信驗證碼</a> </div> </div>
sendCode:function () { if(this.userEntity.phone==null || this.userEntity.phone==""){ alert("請填寫手機號碼"); return ; } axios.get('/user/sendCode.do',{params:{phone:this.userEntity.phone}}).then(function (response) { //獲取服務端響應的結果 console.log(response.data); }).catch(function (reason) { console.log(reason); }); }
<dependency> <groupId>com.itxk</groupId> <artifactId>dao</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.itxk</groupId> <artifactId>interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >ios
<web-app> <display-name>Archetype Created Web Application</display-name> <!-- 加載spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <!--classpath* 加載本身和本身依賴的classpath內容--> <param-value>classpath*:spring/applicationContext*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jms="http://www.springframework.org/schema/jms" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 真正能夠產生Connection的ConnectionFactory,由對應的 JMS服務廠商提供--> <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://192.168.1.88:61616"/> </bean> <!-- Spring用於管理真正的ConnectionFactory的ConnectionFactory --> <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <!-- 目標ConnectionFactory對應真實的能夠產生JMS Connection的ConnectionFactory --> <property name="targetConnectionFactory" ref="targetConnectionFactory"/> </bean> <!-- Spring提供的JMS工具類,它能夠進行消息發送、接收等 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <!-- 這個connectionFactory對應的是咱們定義的Spring提供的那個ConnectionFactory對象 --> <property name="connectionFactory" ref="connectionFactory"/> </bean> <!--這個是隊列目的地,點對點的 文本信息--> <bean id="smsDestination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg value="sms"/> </bean> </beans>
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <dubbo:protocol name="dubbo" port="20886"></dubbo:protocol> <dubbo:application name="mystore-user-service"/> <dubbo:registry address="zookeeper://192.168.1.88:2181"/> <dubbo:annotation package="com.itxk.core.service" /> </beans>
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 開啓事務控制的註解支持 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
public class PhoneFormatCheckUtils { /** * 大陸號碼或香港號碼都可 */ public static boolean isPhoneLegal(String str)throws PatternSyntaxException { return isChinaPhoneLegal(str) || isHKPhoneLegal(str); } /** * 大陸手機號碼11位數,匹配格式:前三位固定格式+後8位任意數 * 此方法中前三位格式有: * 13+任意數 * 15+除4的任意數 * 18+除1和4的任意數 * 17+除9的任意數 * 147 */ public static boolean isChinaPhoneLegal(String str) throws PatternSyntaxException { String regExp = "^((13[0-9])|(15[^4])|(18[0,2,3,4,5-9])|(17[0-8])|(147))\\d{8}$"; Pattern p = Pattern.compile(regExp); Matcher m = p.matcher(str); return m.matches(); } /** * 香港手機號碼8位數,5|6|8|9開頭+7位任意數 */ public static boolean isHKPhoneLegal(String str)throws PatternSyntaxException { String regExp = "^(5|6|8|9)\\d{7}$"; Pattern p = Pattern.compile(regExp); Matcher m = p.matcher(str); return m.matches(); } }
public interface UserService { public void sendCode(String phone); }
@Service public class UserServiceImp implements UserService { @Autowired private RedisTemplate redisTemplate; @Autowired private JmsTemplate jmsTemplate; @Autowired private ActiveMQQueue smsDestination; @Override public void sendCode(final String phone) { //1. 生成一個隨機6爲數字, 做爲驗證碼 StringBuffer sb = new StringBuffer(); for (int i =1; i < 7; i++) { int s = new Random().nextInt(10); sb.append(s); } //2. 手機號做爲key, 驗證碼做爲value保存到redis中, 生存時間爲10分鐘 redisTemplate.boundValueOps(phone).set(sb.toString(), 60 * 10, TimeUnit.SECONDS); final String smsCode = sb.toString(); //3. 將手機號, 短信內容, 模板編號, 簽名封裝成map消息發送給消息服務器 jmsTemplate.send(smsDestination, new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { MapMessage message = session.createMapMessage(); message.setString("mobile", phone);//手機號 message.setString("template_code", "SMS_169111508");//模板編碼 message.setString("sign_name", "瘋碼");//簽名 Map map=new HashMap(); map.put("code", smsCode); //驗證碼 message.setString("param", JSON.toJSONString(map)); return (Message) message; } }); } }
@RequestMapping("/sendCode") public Result sendCode(String phone) { try { if (phone == null || "".equals(phone)) { return new Result(false, "手機號不能爲空!"); } if (!PhoneFormatCheckUtils.isPhoneLegal(phone)) { return new Result(false, "手機號格式不正確"); } userService.sendCode(phone); return new Result(true, "發送成功!"); } catch (Exception e) { e.printStackTrace(); return new Result(false, "發送失敗!"); } }
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>com.fmjava</groupId> <artifactId>dao</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>com.fmjava</groupId> <artifactId>interface</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.1.0</version> </dependency>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!-- 加載spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring/applicationContext*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jms="http://www.springframework.org/schema/jms" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 真正能夠產生Connection的ConnectionFactory,由對應的 JMS服務廠商提供--> <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://192.168.1.88:61616"/> </bean> <!-- Spring用於管理真正的ConnectionFactory的ConnectionFactory --> <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <!-- 目標ConnectionFactory對應真實的能夠產生JMS Connection的ConnectionFactory --> <property name="targetConnectionFactory" ref="targetConnectionFactory"/> </bean> <!-- 點對點模式,接收sms消息--> <bean id="queueSMSDestination" class="org.apache.activemq.command.ActiveMQQueue"> <!--指定從這個隊列中去接收SMS信息--> <constructor-arg value="sms"/> </bean> <!-- 點對點模式, 消息監聽容器 發送短信--> <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="queueSMSDestination" /> <property name="messageListener" ref="smsListener" /> </bean> <bean id="smsListener" class="com.fmjava.core.listener.PageListener"></bean> </beans>
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <dubbo:protocol name="dubbo" port="20889"></dubbo:protocol> <dubbo:application name="fmjava-sms-service"/> <dubbo:annotation package="com.fmjava.core.service" /> </beans>
public class SmsUtil { /** * 發送短信 * @param mobile 手機號 * @param template_code 模板號 * @param sign_name 簽名 * @param param 驗證碼 */ public static void sendSms(String mobile,String template_code,String sign_name,String param) { DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAIFrvvzwdyvzcK", "mjRPox3Y5opiwdV6lCZu6KEBmO0Q0M"); IAcsClient client = new DefaultAcsClient(profile); CommonRequest request = new CommonRequest(); request.setMethod(MethodType.POST); request.setDomain("dysmsapi.aliyuncs.com"); request.setVersion("2017-05-25"); request.setAction("SendSms"); request.putQueryParameter("RegionId", "cn-hangzhou"); request.putQueryParameter("PhoneNumbers", mobile); request.putQueryParameter("SignName", sign_name); request.putQueryParameter("TemplateCode", template_code); request.putQueryParameter("TemplateParam",param); try { CommonResponse response = client.getCommonResponse(request); System.out.println(response.getData()); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } } }
public class SMSListener implements MessageListener { @Override public void onMessage(Message message) { MapMessage map = (MapMessage)message; try { SmsUtil.sendSms( map.getString("mobile"), map.getString("template_code"), map.getString("sign_name"), map.getString("param")); } catch (JMSException e) { e.printStackTrace(); } System.out.println(map); } }