springBoot-JMS之手机短信验证码-阿里云

**

话不多说,直接撸代码。

	**1.建立工具类**
		1.需要依赖
		2.配置文件
		3.工具类
		4.编写接收类
		5.编写启动类
	**2.建立测试类**
	 	1.依赖
	 	2.配置文件
	 	3.编写接收类
		4. 编写启动类		 	

**

1.1需要的依赖。

	
	<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>

1.2配置文件

#设置端口
server.port=9003  
#虚拟机IP
spring.activemq.broker-url=tcp://192.168.200.128:61616

#产品密钥,自动生成=》www.alidayu.com -》请读者自行注册
#注:这里的键的首字母一定是小写,写大写会报缺少键的异常
accessKeyId=********
accessKeySecret=***************

1.3工具类【模板,不需要修改,阿里云提供,复制粘贴即用】

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 {

    //产品名称:云通信短信API产品,开发者无需替换
    static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    static final String domain = "dysmsapi.aliyuncs.com";

    @Autowired
    private Environment env;

// TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)

    /**
     * 发送短信
     * @param mobile 手机号
     * @param template_code 模板号
     * @param sign_name 签名
     * @param param 参数
     * @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");

//可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

//初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

//组装请求对象-具体描述见控制台-文档部分内容
        SendSmsRequest request = new SendSmsRequest();
//必填:待发送手机号
        request.setPhoneNumbers(mobile);
//必填:短信签名-可在短信控制台中找到
        request.setSignName(sign_name);
//必填:短信模板-可在短信控制台中找到
        request.setTemplateCode(template_code);
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
        request.setTemplateParam(param);

//选填-上行短信扩展码(无特殊需求用户请忽略此字段)
//request.setSmsUpExtendCode("90997");

//可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
        request.setOutId("yourOutId");

//hint 此处可能会抛出异常,注意catch
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);

        return sendSmsResponse;
    }

}

1.4 接收类【也是模板类型,复制粘贴即用】

import cn.hs.core.util.SmsUtil;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
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{
            /**
             *模板编号:请登录:www.alidayu.com 注册获取
             * sign_name:请登录:www.alidayu.com 注册获取
             * param:请登录:www.alidayu.com 注册获取【可以自己设置,也可以使用阿里提供的模板】
             */

            SendSmsResponse response = smsUtil.sendSms(
                    /**
                     * 中间数据的传输使用MAP集合
                     */
                    //获取手机号码
                    map.get("mobile"),//手机号码=》mobile:要和测试类里保持一致
                    //获取模板签名
                    map.get("template_code"),//模板编号=》template_code:要和测试类里保持一致【键名】
                    //获取签名
                    map.get("sign_name"),//签名=》sign_name:要和测试类里保持一致【键名】
                   //获取短信模板内容
                    map.get("param")  );// 短信内容=》param:要和测试类里保持一致【键名】
            /**
             * 下面的是输出打印结果,可以需要,也可以不需要
             */
            System.out.println("Code=" + response.getCode());
            System.out.println("Message=" + response.getMessage());
            System.out.println("RequestId=" + response.getRequestId());
            System.out.println("BizId=" + response.getBizId());

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

    }
}

1.5 启动类,这个测试时必须要先运行

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编写测试类

2.1需要的依赖文件

 <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>

2.2编写配置文件

#设置tomcat端口
server.port=8088
#我的虚拟机Ip
spring.activemq.broker-url=tcp://192.168.200.128:61616


2.3 编写发送类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/testJms")
public class SmsTest {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @RequestMapping("/sendSms")
    public void sendSms(){
        Map map = new HashMap();
        /**
         * 下面的参数要和接收类的键值保持一致
         * 模板编号+签名==》请读者去www.alidayu.com 注册获取。
         */
        map.put("mobile","接收短信的手机号");
        map.put("template_code","模板编号");
        map.put("sign_name","签名");
        map.put("param","{\"code\":\"验证码\"}");
        jmsMessagingTemplate.convertAndSend("sms",map);
    }
}

2.4 编写启动类

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

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

注:一定要写启动接受类,在启动发送类,然后访问测试类路径

末尾注释:有的小伙伴是不是不知道阿里的注册流程,推荐一篇文章:https://blog.csdn.net/Afflatus_f/article/details/102630352 =》狗凡的文章。
这里面有注册流程,需要的小伙伴可以看一下哦!
注:阿里网站域名:www.alidayu.com 可以去访问哦。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值