idea SMS verification

idea SMS verification (SMS interface: Alibaba cloud; message middleware: activeMQ)

It is mainly divided into two steps: 1. Create sending message 2. Create receiving message

1. Create a message

1.1 create maven project

1.2 pom.xml file import dependency

Note: sometimes the package of aliyun will not be automatically downloaded by idea, so it needs to be downloaded by itself.  

   <modelVersion>4.0.0</modelVersion>

    <groupId>com.zc</groupId>
    <artifactId>launchCode</artifactId>
    <version>1.0-SNAPSHOT</version>
//idea Automatic generation

//rely on
    <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 create application.properties file under src/main/resources

     

server.port=8088  //Port number
spring.activemq.broker-url=tcp://172.25.1.110:61616  //ip It's middleware. ip

 

1.4 create startup class

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

 

1.5 create test class

@RestController
@RequestMapping("/testJms")
public class TestJms {
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
    @RequestMapping("/sendSms")
    public void sendSms(){
        Map map = new HashMap();
        //Phone number
        map.put("mobile","13598609053");
        //Template number
        map.put("template_code","SMS_175573224");
        //autograph
        map.put("sign_name","Online music website");
        //Content of short message
        map.put("param","{\"code\":\"666666\"}");
        //Store relevant SMS information into message middleware sms in
        jmsMessagingTemplate.convertAndSend("sms",map);
    }

 

2. Create receiving message

2.1 create maven project

2.2 pom.xml file import dependency

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.zc</groupId>
    <artifactId>adoptCode</artifactId>
    <version>1.0-SNAPSHOT</version>
//The above paragraph is average idea Automatically generate content for creation maven Time written
<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 create application.properties file under src/main/resources

server.port=9003
spring.activemq.broker-url=tcp://172.25.1.110:61616 //Middle id
#Ali AK
accessKeyId= Sub account No.
accessKeySecret=Password of sub account

 

2.4 create 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 create listener package and test class in src/main/java

 

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"),
                    // autograph
                    map.get("param") );
            // Content of short message
            System.out.println("code"+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 create util toolkit and tool class 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 communication SMS API product,Developers do not need to replace
        static final String product = "Dysmsapi";
        //Product domain name,Developers do not need to replace
        static final String domain = "dysmsapi.aliyuncs.com";

        @Autowired
        private Environment env;

// TODO It needs to be replaced by the developer's own AK(Search on Alibaba cloud Access Console)

        /**
         * Sending SMS
         * @param mobile Cell-phone number
         * @param template_code Template number
         * @param sign_name autograph
         * @param param parameter
         * @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");

//Self adjustable timeout
            System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
            System.setProperty("sun.net.client.defaultReadTimeout", "10000");

//Initialization acsClient,Temporary support region turn
            IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
            IAcsClient acsClient = new DefaultAcsClient(profile);

//Assemble request object-Please refer to the console for details-Part of the document
            SendSmsRequest request = new SendSmsRequest();
//Must fill:Mobile number to be sent
            request.setPhoneNumbers(mobile);
//Must fill:SMS signature-Can be found in SMS console
            request.setSignName(sign_name);
//Must fill:SMS template-Can be found in SMS console
            request.setTemplateCode(template_code);
//Optional:Variable substitution in templates JSON strand,If the template content is"Dear ${name},Your verification code is ${code}"Time,The value here is
            request.setTemplateParam(param);

//Selective filling-Uplink SMS extension code(Users without special requirements please ignore this field)
//request.setSmsUpExtendCode("90997");

//Optional:outId Extension field provided to business party,Finally, this value will be brought back to the caller in the SMS receipt message.
            request.setOutId("yourOutId");

//hint An exception may be thrown here. Note catch
            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);

            return sendSmsResponse;
        }
    }

2.7 boot

1) start the main method in the receiving message and then the main method in the sending message

2) input ip address: 8088/testJms/sendSms for testing

 

 

 

 

3) when console output

 

4) when the browser accesses, it can receive the SMS.

   over~~

-----Original: light tea

Keywords: Java Spring Mobile Maven

Added by romzer on Wed, 23 Oct 2019 18:39:23 +0300