一:依赖包安装

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>dysmsapi20170525</artifactId>
    <version>4.2.0</version>
</dependency>

二:阿里短信配置

在application.yaml配置阿里短信,路径地址为:src/main/resources/application.yaml

aliyun:
  sms:
    access-key-id: xxx
    access-key-secret: xxx
    endpoint: dysmsapi.aliyuncs.com
    sign-name: xxx
    template-code: xxx # 短信模板CODE

三:短信配置属性类

package com.example.demo.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "aliyun.sms")
public class AliSmsProperties {
    private String accessKeyId;
    private String accessKeySecret;
    private String endpoint;
    private String signName;
    private String templateCode;
}

四:短信服务

package com.example.demo.service;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.aliyun.teaopenapi.models.Config;
import com.example.demo.properties.AliSmsProperties;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tools.jackson.databind.ObjectMapper;

import static com.aliyun.teautil.Common.toJSONString;

import java.util.Map;

@Service
public class AliSmsService {

    @Resource
    private AliSmsProperties aliSmsProperties;
    @Autowired
    private ObjectMapper objectMapper;

    public Client createClient() throws Exception {
        Config config = new Config()
                .setAccessKeyId(aliSmsProperties.getAccessKeyId())
                .setAccessKeySecret(aliSmsProperties.getAccessKeySecret())
                .setEndpoint(aliSmsProperties.getEndpoint());
        return new Client(config);
    }


    public boolean send(String phone, Map params) throws Exception {
        //初始化请求客户端
        Client client = createClient();
        try {
            SendSmsRequest sendSmsRequest = new SendSmsRequest();

            //手机号
            sendSmsRequest.setPhoneNumbers(phone);
            // 短信签名
            sendSmsRequest.setSignName(aliSmsProperties.getSignName());
            // 模板CODE
            sendSmsRequest.setTemplateCode(aliSmsProperties.getTemplateCode());
            // 模板参数JSON
            sendSmsRequest.setTemplateParam(objectMapper.writeValueAsString(params));

            SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);

            SendSmsResponseBody body = sendSmsResponse.getBody();

            System.out.println(toJSONString(sendSmsResponse));
            System.out.println("ok".equals(body.getCode()));
            if ("OK".equals(body.getCode())) {
                //发送成功
                return true;
            } else {
                //发送失败
                return false;
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }
}

五:短信服务调用示例

package com.example.demo.controller;

import com.example.demo.service.AliSmsService;
import jakarta.annotation.Resource;
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;

@RestController
@RequestMapping("/sms")
public class SmsController {

    @Resource
    private AliSmsService aliSmsService;

    @GetMapping("/send")
    public Boolean send() throws Exception {

        String phone = "xxxxxxxx";
        Map<String, Object> params = new HashMap<>();
        params.put("code", "1234");

        boolean ok = aliSmsService.send(phone, params);
        return ok;
    }
}

huaweichenai
698 声望116 粉丝