1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| package com.bohu.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.http.MethodType; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import com.netflix.client.ClientException; import lombok.Data; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;
@Component @Data public class SMSUtils implements InitializingBean {
@Value("${aliyun.keysecret}") private String keysecret;
@Value("${aliyun.keyid}") private String keyId;
public static String KEYID; public static String KEYSECRET;
public static boolean sendShortMessage(String templateCode, String phoneNumbers, String param ,String SignName) { System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000"); final String product = "Dysmsapi"; final String domain = "dysmsapi.aliyuncs.com";
final String accessKeyId = KEYID; final String accessKeySecret = KEYSECRET; IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret); try { DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); } catch (com.aliyuncs.exceptions.ClientException e) { e.printStackTrace(); } IAcsClient acsClient = new DefaultAcsClient(profile); SendSmsRequest request = new SendSmsRequest(); request.setMethod(MethodType.POST); request.setPhoneNumbers(phoneNumbers); request.setSignName(SignName); request.setTemplateCode(templateCode); request.setTemplateParam("{\"code\":\"" + param + "\"}"); SendSmsResponse sendSmsResponse = null; try { sendSmsResponse = acsClient.getAcsResponse(request); } catch (com.aliyuncs.exceptions.ClientException e) { e.printStackTrace(); } if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) { System.out.println("请求成功"); return true; } return false; }
@Override public void afterPropertiesSet() throws Exception { KEYID = keyId; KEYSECRET = keysecret;
} }
```java
package com;
import org.aspectj.weaver.ast.Var; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.util.DigestUtils;
public class text { public static void main(String[] args)throws Exception { SMSUtils.sendShortMessage("模版CODE","发送的手机号","发送的消息");
} }
|