idea SMS verification

 idea SMS verification (SMS Interface: Ali cloud; messaging middleware: activeMQ)

Is divided into two steps: Create message 2, the received message created

1. Create a message

 1.1 created maven project

 1.2 pom.xml file import-dependent   

  Note : aliyun packages sometimes idea does not automatically download, you need to download. 

   <modelVersion>4.0.0</modelVersion>

    <groupId>com.zc</groupId>
    <artifactId>launchCode</artifactId>
    <version>1.0-SNAPSHOT</version>
// IDEA automatically generated

// dependence
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
    </parent>
    <properties>
            <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
    </dependencies>

 

 1.3  Creating application.properties file in src / main / resources

     

8088 = the server.port   // port number 
spring.activemq.broker-TCP = URL: // 172.25.1.110:61616   // ip ip middleware

 

  1.4 create a startup class 

/ ** startup class * /
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

 

 1.5 Creating a Test Class  

@RestController
@RequestMapping("/testJms")
public class TestJms {
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
    @RequestMapping("/sendSms")
    public void sendSms(){
        The Map the Map = new new HashMap ();
         // phone number 
        map.put ( "Mobile", "13,598,609,053" );
         // template number 
        map.put ( "template_code", "SMS_175573224" );
         // signature 
        map.put ( " sign_name "," online music sites " );
         // message content 
        map.put (" param "," {\ "code \": \ "666666 \"} " );
         // the relevant information into the SMS message center sms member in 
        jmsMessagingTemplate.convertAndSend ( "sms" , Map);
    }

 

2. Create a message received

 2.1 created maven project

 2.2 pom.xml file import dependence 

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.zc</groupId>
    <artifactId>adoptCode</artifactId>
    <version>1.0-SNAPSHOT</version>
// period of the above general idea is automatically generated when you create content to write a maven
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
    </parent>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>3.2.5</version>
        </dependency>
    </dependencies>

 

 2.3 Creating application.properties file in src / main / resources

9003 = server.port 
spring.activemq.broker -url = tcp: // 172.25.1.110:61616 // middle of the above mentioned id 
# Ali AK
accessKeyId = sub-account number
accessKeySecret = sub-account password

 

2.4 create a startup class 

package com.zc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

 

2.5 in src / main / java and create listener package test class

 

package com.zc.listener;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.zc.util.SmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public  class SmsListener {
    @Autowired
    private SmsUtil smsUtil;
    @JmsListener(destination = "sms")
    public void sendSms(Map<String,String> map){
        try{

            SendSmsResponse Response = smsUtil.sendSms (
                    map.get("mobile"),
                    map.get ( "template_code" ),
                     // template number 
                    map.get ( "SIGN_NAME" ),
                     // signature 
                    map.get ( "param" ));
             // message content 
            System.out.println ( "code" + the Response .getCode ());
            System.out.println("message="+response.getMessage());
            System.out.println("RequetId="+response.getRequestId());
            System.out.println("Bizid="+response.getBizId());


        }catch (ClientException e){
            e.printStackTrace ();
        }
        {

        }
    }
}

 

2.6 Creating util kits and tools in src / main / java    

package com.zc.util;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public  class SmsUtil {

        // Product Name: Cloud Communications SMS API product, developers do not need to replace 
        static  Final String Product = "Dysmsapi" ;
         // product domain, developers do not need to replace 
        static  Final String Domain = "dysmsapi.aliyuncs.com" ;

        @Autowired
        private Environment env;

// TODO here need to replace the developer's own AK (looking at the console access Ali cloud)

        /**
         * send messages
         * @Param Mobile phone number
         * @Param template_code template number
         * @Param SIGN_NAME signature
         * @Param param parameters
         * @return
         * @throws ClientException
         */
        public SendSmsResponse sendSms(String mobile,String template_code,String sign_name,String param) throws ClientException {

            String accessKeyId =env.getProperty("accessKeyId");
            String accessKeySecret = env.getProperty("accessKeySecret");

// can self-adjust the timeout 
            System.setProperty ( "sun.net.client.defaultConnectTimeout", "10000" );
            System.setProperty("sun.net.client.defaultReadTimeout", "10000");

// initialize acsClient, does not support region of 
            IClientProfile Profile = DefaultProfile.getProfile ( "CN-Hangzhou" , accessKeyId, accessKeySecret);
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
            IAcsClient acsClient = new DefaultAcsClient(profile);

// Assembly request object - particularly described in the console - parts of the documentation 
            SendSmsRequest Request = new new SendSmsRequest ();
 // Required: phone number to be sent 
            request.setPhoneNumbers (Mobile);
 // Required: SMS signature - can SMS console find 
            request.setSignName (SIGN_NAME);
 // required: SMS templates - can be found in the SMS console 
            request.setTemplateCode (template_code);
 // optional: template variable substitution JSON string, such as the template content as "dear when the $ {name}, your verification code is $ {code} ", where the value 
            request.setTemplateParam (param);

// Optional - up message spreading code (no special needs of users ignore this field)
 // request.setSmsUpExtendCode ( "90 997");

// Optional: outId is provided to the business side extension field, and ultimately back to the message receipt message this value to the caller 
            request.setOutId ( "yourOutId" );

// hint here might throw an exception, note the catch 
            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse (Request);

            return sendSmsResponse;
        }
    }

 2.7 start

  1) Start the main method of the received message, and then starts the main method of message

  2) Enter the URL ip: 8088 / testJms / sendSms test 

 

 

 

 

  3) When the console output

 

  4) When after the browser access you can receive text messages

   over~~

----- Original: Zhancha light sip

Guess you like

Origin www.cnblogs.com/zscbk/p/11728115.html