Springboot整合阿里巴巴SMS的实现示例
作者:堕落年代
本文主要介绍了Springboot整合阿里巴巴SMS的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
前提条件
要确保用户有这个权限
还要确保组要有这个权限
讲反了要先保证组有这个权限然后保证用户有这个权限,然后就可以使用这个用户的权限的key来调取api了
申请资质、签名等
申请资质
点击这个进入声请就可以了然后等2个小时左右就可以通过了
申请签名
这个是为了之后自定义模板做准备
添加模板
当然第一次是可以注册钉钉认证之后获取免费的一些额度
api引入
依赖引入
<!--sms的服务--> <dependency> <groupId>com.aliyun</groupId> <artifactId>alibabacloud-dysmsapi20170525</artifactId> <version>2.0.24</version> </dependency>
代码部分
package com.example.lpms.tool; import com.example.lpms.common.R; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import com.aliyun.auth.credentials.Credential; import com.aliyun.auth.credentials.provider.StaticCredentialProvider; import com.aliyun.sdk.service.dysmsapi20170525.models.*; import com.aliyun.sdk.service.dysmsapi20170525.*; import com.google.gson.Gson; import darabonba.core.client.ClientOverrideConfiguration; import java.util.concurrent.CompletableFuture; /** * @author:DUOLUONIANDAI * @DATA:2023/12/13 * @Title: */ @Component public class SMSTool { @Value("${spring.sms.id}") String id; @Value("${spring.sms.secret}") String secret; @Value("${spring.sms.sign-name}") String signName; @Value("${spring.sms.templateCode}") String templateCode; public R sendSMS(String phone, String captcha) { try { // Configure Credentials authentication information, including ak, secret, token StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder() .accessKeyId(id) .accessKeySecret(secret) //.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // use STS token .build()); // Configure the Client AsyncClient client = AsyncClient.builder() .region("cn-shanghai") // Region ID //.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient) .credentialsProvider(provider) //.serviceConfiguration(Configuration.create()) // Service-level configuration // Client-level configuration rewrite, can set Endpoint, Http request parameters, etc. .overrideConfiguration( ClientOverrideConfiguration.create() // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi .setEndpointOverride("dysmsapi.aliyuncs.com") //.setConnectTimeout(Duration.ofSeconds(30)) ) .build(); // Parameter settings for API request SendSmsRequest sendSmsRequest = SendSmsRequest.builder() .signName(signName) .templateCode(templateCode) .phoneNumbers(phone) .templateParam("{\"code\":\"" + captcha + "\"}") // Request-level configuration rewrite, can set Http request parameters, etc. // .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders())) .build(); // Asynchronously get the return value of the API request CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest); // Synchronously get the return value of the API request SendSmsResponse resp = response.get(); System.out.println(new Gson().toJson(resp)); // Finally, close the client client.close(); } catch (Exception e) { e.printStackTrace(); return R.fail(); } return R.ok(); } }
注意
这下面和官网不一样但是不这样写会报错,好像是因为这个是直接注入到哪里的,而这里是不需要注入的
// Configure Credentials authentication information, including ak, secret, token StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder() .accessKeyId(id) .accessKeySecret(secret) //.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // use STS token .build());
到此这篇关于Springboot整合阿里巴巴SMS的实现示例的文章就介绍到这了,更多相关Springboot整合阿里巴巴SMS内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!