前提条件
要确保用户有这个权限
还要确保组要有这个权限
讲反了要先保证组有这个权限然后保证用户有这个权限,然后就可以使用这个用户的权限的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());