|
|
@@ -1,15 +1,18 @@
|
|
|
package com.jpsoft.employment.modules.common.utils;
|
|
|
|
|
|
|
|
|
-import com.aliyuncs.DefaultAcsClient;
|
|
|
-import com.aliyuncs.IAcsClient;
|
|
|
-import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
|
|
|
-import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
|
|
|
-import com.aliyuncs.profile.DefaultProfile;
|
|
|
+import com.aliyun.auth.credentials.Credential;
|
|
|
+import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
|
|
|
+import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
|
|
|
+import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
|
|
|
+import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
|
|
|
+import com.google.gson.Gson;
|
|
|
+import darabonba.core.client.ClientOverrideConfiguration;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
-import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
|
+import java.util.concurrent.CompletableFuture;
|
|
|
+
|
|
|
@Configuration
|
|
|
public class AliyunSmsConfig {
|
|
|
@Value("${aliyun.accessKeyId}")
|
|
|
@@ -18,23 +21,52 @@ public class AliyunSmsConfig {
|
|
|
@Value("${aliyun.accessKeySecret}")
|
|
|
private String accessKeySecret;
|
|
|
|
|
|
- @Value("${aliyun.regionId}")
|
|
|
- private String regionId;
|
|
|
|
|
|
- @Bean
|
|
|
- public IAcsClient acsClient() {
|
|
|
- DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
|
|
|
- return new DefaultAcsClient(profile);
|
|
|
- }
|
|
|
+ public SendSmsResponse sendSms(String phoneNumber, String code) throws Exception {
|
|
|
+
|
|
|
+ // Configure Credentials authentication information, including ak, secret, token
|
|
|
+ StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
|
|
|
+ // Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
|
|
|
+ .accessKeyId(accessKeyId)
|
|
|
+ .accessKeySecret(accessKeySecret)
|
|
|
+ //.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // use STS token
|
|
|
+ .build());
|
|
|
+
|
|
|
+ // Configure the Client
|
|
|
+ AsyncClient client = AsyncClient.builder()
|
|
|
+ .region("cn-qingdao") // 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();
|
|
|
|
|
|
- public SendSmsResponse sendSms(String phoneNumber, String signName, String templateCode, String templateParam) throws Exception {
|
|
|
- IAcsClient client = acsClient();
|
|
|
- SendSmsRequest request = new SendSmsRequest();
|
|
|
- request.setPhoneNumbers(phoneNumber);
|
|
|
- request.setSignName(signName);
|
|
|
- request.setTemplateCode(templateCode);
|
|
|
- request.setTemplateParam(templateParam);
|
|
|
+ // Parameter settings for API request
|
|
|
+ SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
|
|
|
+ .phoneNumbers(phoneNumber)
|
|
|
+ .signName("十八匠")
|
|
|
+ .templateCode("SMS_478595063")
|
|
|
+ .templateParam("{\"code\":\"" + code + "\"}")
|
|
|
+ // Request-level configuration rewrite, can set Http request parameters, etc.
|
|
|
+ // .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
|
|
|
+ .build();
|
|
|
|
|
|
- return client.getAcsResponse(request);
|
|
|
+ // 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));
|
|
|
+
|
|
|
+ client.close();
|
|
|
+
|
|
|
+ return resp;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
}
|