当前位置:   article > 正文

SpringBoot使用阿里云实现短信功能

SpringBoot使用阿里云实现短信功能

引入依赖

<!-- 阿里云依赖 -->
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>4.6.3</version>
</dependency>
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
    <version>2.2.1</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

配置文件

# 阿里云短信配置
sms:
  aliyun:
    enabled: true
    endpoint: cn-hangzhou
    access-key-id: xxxxxxxx
    access-key-secret: xxxxxxxx
    sign-name: xxxxxxxx
    template-code: xxxxxxxx
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

属性类

package com.qiangesoft.sms.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * 阿里云短信属性
 *
 * @author qiangesoft
 * @date 2024-04-30
 */
@Data
@ConfigurationProperties(prefix = "sms.aliyun")
public class AliyunSmsProperties {

    /**
     * 是否启用
     */
    private boolean enabled = true;

    /**
     * Endpoint
     */
    private String endpoint;

    /**
     * accessKeyId
     */
    private String accessKeyId;

    /**
     * accessKeySecret
     */
    private String accessKeySecret;

    /**
     * 短信签名
     */
    private String signName;

    /**
     * 模板code
     */
    private String templateCode;

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

配置类

package com.qiangesoft.sms.config;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 阿里云短信配置
 *
 * @author qiangesoft
 * @date 2024-04-30
 */
@Configuration
@EnableConfigurationProperties(AliyunSmsProperties.class)
@ConditionalOnProperty(prefix = "sms.aliyun", name = "enabled", havingValue = "true")
public class AliyunSmsConfiguration {

    private static final String PRODUCT = "Dysmsapi";

    private static final String DOMAIN = "dysmsapi.aliyuncs.com";

    @Autowired
    private AliyunSmsProperties aliyunSmsProperties;

    @Bean
    @ConditionalOnMissingBean
    public IAcsClient acsClient() {
        String endPoint = aliyunSmsProperties.getEndpoint();
        String accessKeyId = aliyunSmsProperties.getAccessKeyId();
        String accessKeySecret = aliyunSmsProperties.getAccessKeySecret();

        IClientProfile profile = DefaultProfile.getProfile(endPoint, accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint(endPoint, PRODUCT, DOMAIN);
        return new DefaultAcsClient(profile);
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

短信发送实现

package com.qiangesoft.sms.handler;

import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;

/**
 * 短信处理器
 *
 * @author qiangesoft
 * @date 2024-04-30
 */
public interface SendHandler {

    /**
     * 发送短信
     *
     * @param phones
     * @param param
     * @return
     */
    boolean send(Collection<String> phones, Map<String, Object> param);

    /**
     * 发送短信
     *
     * @param phone
     * @param param
     * @return
     */
    default boolean send(String phone, Map<String, Object> param) {
        if (StringUtils.isBlank(phone)) {
            return false;
        }

        return send(Collections.singletonList(phone), param);
    }

    /**
     * 发送短信
     *
     * @param phones
     * @param templateCode
     * @param param
     * @return
     */
    boolean send(Collection<String> phones, String templateCode, Map<String, Object> param);

    /**
     * 发送短信
     *
     * @param phone
     * @param templateCode
     * @param param
     * @return
     */
    default boolean send(String phone, String templateCode, Map<String, Object> param) {
        if (StringUtils.isBlank(phone)) {
            return false;
        }

        return send(Collections.singletonList(phone), param);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
package com.qiangesoft.sms.handler;

import com.alibaba.fastjson2.JSONObject;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.http.MethodType;
import com.qiangesoft.sms.config.AliyunSmsProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Map;

/**
 * 阿里云短信处理器
 *
 * @author qiangesoft
 * @date 2023-05-16
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class AliyunSendHandler implements SendHandler {

    private final IAcsClient acsClient;

    private final AliyunSmsProperties aliyunSmsProperties;

    @Override
    public boolean send(Collection<String> phones, Map<String, Object> param) {
        return this.send(phones, aliyunSmsProperties.getTemplateCode(), param);
    }

    @Override
    public boolean send(Collection<String> phones, String templateCode, Map<String, Object> param) {
        SendSmsRequest request = new SendSmsRequest();
        request.setSysMethod(MethodType.POST);
        request.setPhoneNumbers(StringUtils.join(phones, ","));
        request.setSignName(aliyunSmsProperties.getSignName());
        request.setTemplateCode(aliyunSmsProperties.getTemplateCode());
        request.setTemplateParam(JSONObject.toJSONString(param));

        try {
            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
            if ("OK".equals(sendSmsResponse.getCode())) {
                return true;
            }

            log.debug("send fail[code={}, message={}]", sendSmsResponse.getCode(), sendSmsResponse.getMessage());

        } catch (Exception e) {
            log.debug(e.getMessage(), e);
        }
        return false;
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61

测试

package com.qiangesoft.sms.controller;

import com.qiangesoft.sms.handler.SendHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 短信测试
 *
 * @author qiangesoft
 * @date 2024-04-30
 */
@RequestMapping("/sms")
@RestController
public class SmsController {

    @Autowired
    private SendHandler sendHandler;

    @GetMapping("/send")
    public Boolean send() {
        Map<String, Object> param = new HashMap<>();
        param.put("name", "张三");
        param.put("jds", 111);
        param.put("adds", "顶顶顶帆帆");
        return sendHandler.send("15222222222", param);
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/535928
推荐阅读
相关标签
  

闽ICP备14008679号