首页 > 其他分享 >短信发送

短信发送

时间:2023-09-06 23:24:59浏览次数:19  
标签:短信 spring boot http springframework 发送 org import

依赖注入

 <properties>
        <spring.boot.version>2.5.5</spring.boot.version>
        <lombok.version>1.18.16</lombok.version>
    </properties>

    
    <dependencies>
        <!--springBoot初始化-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
        <!--springBootWeb-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
            <version>${spring.boot.version}</version>
        </dependency>


        <!--lombok工具-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>
        <!-- OSS各个项目单独加依赖,根据需要进行添加-->
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.10.2</version>
        </dependency>

        <!--测试相关-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.26-SNAPSHOT</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <version>${spring.boot.version}</version>
            <scope>test</scope>
        </dependency>
        <!--guava-->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>30.1-jre</version>
        </dependency>
    </dependencies>

application.properties

server.port=8001

spring.application.name=Sms-service

#----sms 短信配置-------
sms.app-code=8b620f10437441f5889d91caa0267a06
sms.template-id=JM1000372

SmsComponent

import com.xtw.config.SmsConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
@Slf4j
public class SmsComponent {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private SmsConfig smsConfig;

    /**
     * 短信发送api接口
     */
    private static final String URL_TEMPLATE = "https://jmsms.market.alicloudapi.com/sms/send?mobile=%s&templateId=%s&value=%s";

    /**
     * 发送验证码
     */
    public void send(String to,String template,String value){

        String url = String.format(URL_TEMPLATE, to, template, value);

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.set("Authorization","APPCODE "+smsConfig.getAppCode());

        HttpEntity<Object> entity = new HttpEntity<>(httpHeaders);
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);

        if(responseEntity.getStatusCode().is2xxSuccessful()){
            log.info("验证码发送成功");
        }else {
            log.error("发送短信验证码失败:{}",responseEntity.getBody());
        }

    }

}

SmsConfig

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

@ConfigurationProperties(prefix = "sms")
@Configuration
@Data
public class SmsConfig {
    /**
     * 短信验证码发送 appcode
     */
    private String appCode;

    /**
     * 短信内容模板
     */
    private String templateId;

}

RestTemplateConfig(使用HTTP协议请求)

import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfig {
    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory httpRequestFactory){
        return new RestTemplate(httpRequestFactory);
    }

    @Bean
    public ClientHttpRequestFactory httpRequestFactory(){
        return new HttpComponentsClientHttpRequestFactory();
    }
    @Bean
    public HttpClient httpClient(){
        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", SSLConnectionSocketFactory.getSocketFactory())
                .build();

        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);


        //设置连接池最大是500个连接
        connectionManager.setMaxTotal(500);
        //MaxPerRoute是对maxtotal的细分,每个主机的并发最大是300,route是指域名
        connectionManager.setDefaultMaxPerRoute(300);


        RequestConfig requestConfig = RequestConfig.custom()
                //返回数据的超时时间
                .setSocketTimeout(20000)
                //连接上服务器的超时时间
                .setConnectTimeout(10000)
                //从连接池中获取连接的超时时间
                .setConnectionRequestTimeout(1000)
                .build();


        return HttpClientBuilder.create().setDefaultRequestConfig(requestConfig)
                .setConnectionManager(connectionManager)
                .build();
    }

}

测试

import com.xtw.SmsApplication;
import com.xtw.component.SmsComponent;
import com.xtw.config.SmsConfig;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes= SmsApplication.class)
@Slf4j
public class SmsTest {

    @Autowired
    private SmsConfig smsConfig;
    @Autowired
    private SmsComponent smsComponent;


    @Test
    public void testProperties(){
        System.out.println(smsConfig.getAppCode());
    }

    @Test
    public void testSend(){
        smsComponent.send("15070159890",smsConfig.getTemplateId(),"652421");
    }
}

标签:短信,spring,boot,http,springframework,发送,org,import
From: https://www.cnblogs.com/xietingwei/p/17683647.html

相关文章

  • Prometheus+Alertmanager集成免费告警电话、短信
    Prometheus是由SoundCloud开发的开源监控报警系统和时序列数据库(TSDB)。Prometheus使用Go语言开发,是GoogleBorgMon监控系统的开源版本。 https://www.ccloudalarm.com账号后,选择Prometheus监控集成CCloudAlarm告警平台支持参数自定义,可针对自定义参数做告警处理~首先提供Prometh......
  • Niushop修改短信
    Niushop改短信教程1.打开控制器https://www.****.com//api/login/mobileCodehttps://www.****.com//api/register/mobileCode2.新增代码点击查看代码publicfunctiondxb($tel,$code){$statusStr=array("0"=>"1",//"短信发送成功&......
  • jenkins 自动发送邮件
    前提 已经设置了成功发送邮件1.进入jobs,构建后操作/构建环境/可编辑的邮件通知 选择触发的类型比如构建成功后发邮件  ......
  • 反伪造发件人邮箱地址发送邮件-SPF测试工具.zip
    反伪造发件人邮箱地址发送邮件-SPF测试工具说明:本软件是由邮箱资料站旗下邮箱反垃圾邮件过滤产品技术部独家研制开发,可伪造任意的邮箱发送邮件,对市面上的所有的邮箱都有显著的效果,当然也会有个别的邮箱的拦截系统比较完善,不可避免的可能会进入到垃圾箱里,研究此产品的目的主要是希......
  • Python:使用Resend发送邮件
    官网:https://resend.com/很简单,只需调用api接口,即可发送邮件需要提前准备好参数api_key从Resend申请的keyto_email接收邮件的邮箱地址importrequestsheaders={'Authorization':'Bearer<api_key>','Content-Type':'application/json',}json_d......
  • 图形验证码和短信验证码实战
    前言:上一篇分分享了基于阿里云实现的短信验证码文章,考虑到为了防止登录时,非人工操作,频繁获取验证码,趁热打铁,现在添加了图片验证码服务功能。借鉴网上传统的做法,把实现这两个验证的功能做成有个独立的服务,通过Http分别请求获取校验图片验证码和短信验证码。一、需求描述:图形验证码为......
  • 基于阿里云服务实现短信验证码功能
    前言:阿里云短信服务是一项基于云计算和大数据技术的企业级短信平台服务。它能够为企业和开发者提供高可用、高性能、高稳定性的短信发送服务,可以快速地将各类业务通知、验证码、营销推广等信息发送给用户。在我们经常登录一些系统或者APP时候,经常会遇到其他登录登录方式——短信验......
  • JSONCPP向浏览器前端发送服务器本地文件列表
    服务器解析了浏览器请求之后,要进行响应响应体里需要存放请求的内容HTML标签:是页面的核心内容,定义了页面有什么内容。CSS:控制HTML元素的排版布局和展示方式,是美化页面文档的。JavaScript:让用户与页面进行交互,或在网页背后默默操控网页,以便让显示的内容与效果有所改变。对网页来......
  • python---使用qq的smtp服务发送邮件
    python---使用qq的smtp服务发送邮件importsmtplibfromemail.mime.textimportMIMETextfromemail.headerimportHeader#设置邮箱的域名HOST="smtp.qq.com"#设置邮件标题SUBJECT="这是邮件标题"#设置邮件内容CONTENT="这是邮件内容"#设置发件人邮箱地......
  • 手机版 - imessage信息群发,苹果imessages短信,imessages推信,苹果手机推信,苹果imess
    Apple公司全线在macos与ios两个操作系统上内置了FaceTime与iMessage两个应用。完美替代运营商的短信与电话。并且FaceTime与iMessage的帐号不仅仅与AppleID绑定,同时也与使用这AppleID的手机号码绑定,这样的漏洞自然给无孔不入的群发垃圾信息商们提供了后门。这样iPhone的iMessa......