Java 实现阿里云短信

版权声明:本文首发 http://asing1elife.com ,转载请注明出处。 https://blog.csdn.net/asing1elife/article/details/82788132

Java 实现阿里云短信

阿里云提供一套短信发送的服务可通过 Java 进行对接

更多精彩

官网

短信发送API(SendSms)—阿里云

准备步骤

  1. 创建 阿里云账号
  2. 根据 流程 完成实名认证,以确保可以使用阿里云相应服务
  3. 在密钥管理页面获取阿里云访问密钥,AccessKeyId 和 AccessKeySecret
  4. 想要成功发送一条短信需要获取 短信签名短信模版

在项目 pom 中引入对接所需要的 jar 包

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>3.2.2</version>
</dependency>
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
    <version>1.0.0</version>
</dependency>
</dependencies>

获取对接需要的数据

  1. 将 AccessKeyId 、AccessKeySecret 、SignName 、TemplateCode 进行相应存储

新建 MessageServiceImpl 类用于和阿里云短信接口对接

@Service
public class MessageServiceImpl extends AbstractBaseService {	
	...
}

在上述类中生成一个静态块用于在初始化对接信息

  1. Constants.ALI_ACCESS_KEY_IDConstants.ALI_ACCESS_SECRET 是密钥,成对生成和使用
  2. 其他参数信息按照阿里云开发手册说明,均不需要改变
// 用于从阿里云获取数据的权限客户端
private static IAcsClient iAcsClient;

static {
    // 超时时间
    System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
    System.setProperty("sun.net.client.defaultReadTimeout", "10000");

    // 初始化配置
    String regionId = "cn-hangzhou";
    DefaultProfile profile = DefaultProfile.getProfile(regionId, Constants.ALI_ACCESS_KEY_ID, Constants.ALI_ACCESS_SECRET);

    try {
        String productId = "Dysmsapi";
        String domain = "dysmsapi.aliyuncs.com";

        DefaultProfile.addEndpoint(regionId, regionId, productId, domain);
    } catch (ClientException e) {
        throw new TSharkException("初始化短信接口配置失败!", e);
    }

    // 初始化权限客户端
    iAcsClient = new DefaultAcsClient(profile);
}

创建用于发送短信的 sendMessage 方法

  1. 发送短信之前通常需要验证短信是否已发送,防止重复发送
  2. 为了验证短信是否发送可将通过短信发送的验证码存放于 Redis 中
  3. Constants.ALI_MESSAGE_SIGN_NAME 是从阿里云获取的短信签名
  4. 为提高公用性,发送短信的方法支持从外部调用时传入具体模版编号
  5. 短信接口调用时如果出现异常反馈,请参照 短信接口调用错误码
@Autowired
private RedisHelper redisHelper;

public Boolean sendMessage(String mobile, String template) {
    // 有效性验证
    if (checkMobile(mobile)) {
        throw new TSharkException("验证码已发送,请稍后再试!");
    }

    SendSmsRequest request = new SendSmsRequest();
    SendSmsResponse response = null;

    // 生成随机数
    String random = String.valueOf(new Random().nextInt(999999));

    request.setMethod(MethodType.POST);
    request.setPhoneNumbers(mobile);
    request.setSignName(Constants.ALI_MESSAGE_SIGN_NAME);
    request.setTemplateCode(template);
    request.setTemplateParam("{\"name\":\"" + mobile + "\", \"code\":\"" + random + "\"}");

    try {
        response = iAcsClient.getAcsResponse(request);
    } catch (ClientException e) {
        throw new TSharkException("接收短信回执失败!", e);
    }

    boolean result = response.getCode() != null && response.getCode().equals("OK");

    if (result) {
        // 保存随机数
        saveRandom(mobile, random);
    }

    return result;
}

通过 Redis 验证短信是否发送

private Boolean checkMobile(String mobile) {
    String randomKey = "random:" + mobile;

    return redisHelper.get(randomKey) != null;
}

短信发送成功后需要将手机号和验证码成对存储于 Redis 中,以供之后使用

private void saveRandom(String mobile, String random) {
    String randomKey = "random:" + mobile;

    if (checkMobile(mobile)) {
        return;
    }

    redisHelper.set(randomKey, random);

    // 5分钟失效
    redisHelper.expire(randomKey, 5, TimeUnit.MINUTES);
}

猜你喜欢

转载自blog.csdn.net/asing1elife/article/details/82788132
今日推荐