0


spring boot实现短信验证码功能

1、到阿里云网站申请

https://market.aliyun.com/products/5700000
2/cmapi00046920.html

2、配置文件,可申请测试

sms:
  app-code: xxxxxxxxx
  template-id: xxxxxxx

3、使用restTemplate用于第三方接口调用

package com.example.rsocketclient.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfig {
    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory requestFactory){
        return new RestTemplate(requestFactory);
    }
    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(10000);
        factory.setConnectTimeout(10000);
        return factory;
    }
}

4、装smsConfig,使用了Lombok

package com.example.rsocketclient.entity;

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

@ConfigurationProperties(prefix = "sms")
@Configuration
@Data
public class SmsConfig {
    private String appCode;

    private String templateId;
}

5、封装发送业务,信息打印使用了slf4j

package com.example.rsocketclient.config;

import com.example.rsocketclient.entity.SmsConfig;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
@Slf4j
public class SmsComponent {
    private static final String URL_TEMPLATE = "https://jmsms.market.alicloudapi.com/sms/send?mobile=%s&templateId=%s&value=%s";

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private SmsConfig smsConfig;

    public void send(String to, String templateId, String value) {
        String url = String.format(URL_TEMPLATE, to, templateId, value);
        HttpHeaders headers = new HttpHeaders();
        //最后在header中的格式(中间是英⽂空格)为
        headers.set("Authorization", "APPCODE " + smsConfig.getAppCode());
        HttpEntity<String> entity = new HttpEntity<>(headers);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
        log.info("url={},body={}", url, response.getBody());
        if (response.getStatusCode() == HttpStatus.OK) {
            log.info("发送短信成功,响应信息:{}", response.getBody());
        } else {
            log.error("发送短信失败,响应信息:{}", response.getBody());
        }
    }
}

6、测试控制器代码

package com.example.rsocketclient.controller;

import com.example.rsocketclient.config.SmsComponent;
import com.example.rsocketclient.entity.SmsConfig;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ShortMessageController {
    @Resource
    private SmsConfig smsConfig;
    @Resource
    private SmsComponent smsComponent;

    @GetMapping("/send/{iphone}/code/{code}")
    public String testSmsSend(@PathVariable("iphone") String iphone,@PathVariable("code") String code){
        smsComponent.send(iphone,smsConfig.getTemplateId(),code);
        return "OK";
    }
}

7、接口地址在这

在这里插入图片描述

标签: spring boot java spring

本文转载自: https://blog.csdn.net/weixin_41463944/article/details/140200661
版权归原作者 程序员阿明 所有, 如有侵权,请联系我们删除。

“spring boot实现短信验证码功能”的评论:

还没有评论