1. 程式人生 > >阿裏雲短信服務Java版

阿裏雲短信服務Java版

ica cati tran msu .get bstr after ram 無需

短信服務管理平臺 https://dysms.console.aliyun.com/dysms.htm

java短信發送API https://help.aliyun.com/document_detail/55284.html?spm=5176.10629532.106.1.614b1cbe9VbjhP

過程註冊信息API都有

1.發送短信實現類

package com.xmg.p2p.base.service.impl;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

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.xmg.p2p.base.service.IPhoneService; @Service public class PhoneServiceImpl implements IPhoneService { @Value("${AccessKeyId}") private String mYAccessKeyId; @Value("${AccessKeySecret}") private String mYAccessKeySecret; @Value("${SignName}") private String SignName; @Value(
"${TemplateCode}") private String TemplateCode; @Override public void sendPhone(String username,String PhoneNumbers,String code) { try { //2: 編寫樣例程序 //註:有備註無需修改的位置請勿改動。 //設置超時時間-可自行調整 System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000"); //初始化ascClient需要的幾個參數 final String product = "Dysmsapi";//短信API產品名稱(短信產品名固定,無需修改) final String domain = "dysmsapi.aliyuncs.com";//短信API產品域名(接口地址固定,無需修改) //替換成你的AK final String accessKeyId = mYAccessKeyId;//你的accessKeyId,參考本文檔步驟2 final String accessKeySecret = mYAccessKeySecret;//你的accessKeySecret,參考本文檔步驟2 //初始化ascClient,暫時不支持多region(請勿修改) IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret); DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); IAcsClient acsClient = new DefaultAcsClient(profile); //組裝請求對象 SendSmsRequest request = new SendSmsRequest(); //使用post提交 request.setMethod(MethodType.POST); request.setEncoding("UTF-8"); //必填:待發送手機號。支持以逗號分隔的形式進行批量調用,批量上限為1000個手機號碼,批量調用相對於單條調用及時性稍有延遲,驗證碼類型的短信推薦使用單條調用的方式 request.setPhoneNumbers(PhoneNumbers); //必填:短信簽名-可在短信控制臺中找到 request.setSignName(SignName); //必填:短信模板-可在短信控制臺中找到 request.setTemplateCode(TemplateCode); //可選:模板中的變量替換JSON串,如模板內容為"親愛的${name},您的驗證碼為${code}"時,此處的值為 //友情提示:如果JSON中需要帶換行符,請參照標準的JSON協議對換行符的要求,比如短信內容中包含\r\n的情況在JSON中需要表示成\\r\\n,否則會導致JSON在服務端解析失敗 request.setTemplateParam("{\"name\":\"111\", \"code\":\""+code+"\"}"); //可選-上行短信擴展碼(擴展碼字段控制在7位或以下,無特殊需求用戶請忽略此字段) //request.setSmsUpExtendCode("90997"); //可選:outId為提供給業務方擴展字段,最終在短信回執消息中將此值帶回給調用者 // request.setOutId("yourOutId"); //請求失敗這裏會拋ClientException異常 System.out.println("============"); try { SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); System.out.println("Code:"+sendSmsResponse.getCode()); if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) { //請求成功 System.out.println("請求成功"); } } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { } } }

2.配置文件

#均為阿裏雲API註冊信息
AccessKeyId=***
AccessKeySecret=***
SignName=***
TemplateCode=SMS_***

3.測試類

package com.xmg.test;

import java.util.UUID;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.xmg.p2p.base.service.IMailService;
import com.xmg.p2p.base.service.IPhoneService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SendMailTest {
    

    @Autowired
    private IPhoneService phoneService;
    
    
    @Test
    public void testSendPhone(){
        //生成一個驗證碼
        String verifyCode = UUID.randomUUID().toString().substring(0,4);
        phoneService.sendPhone("zzz","130****6583",  verifyCode);
    }
}

但是采用測試類直接運行會報錯

java.lang.RuntimeException: HMAC-SHA1 not supported.
    at com.aliyuncs.auth.ShaHmac1.signString(ShaHmac1.java:44)
    at com.aliyuncs.RpcAcsRequest.signRequest(RpcAcsRequest.java:138)
    at com.aliyuncs.DefaultAcsClient.doAction(DefaultAcsClient.java:232)
    at com.aliyuncs.DefaultAcsClient.doAction(DefaultAcsClient.java:175)
    at com.aliyuncs.DefaultAcsClient.doAction(DefaultAcsClient.java:79)
    at com.aliyuncs.DefaultAcsClient.getAcsResponse(DefaultAcsClient.java:124)
    at com.xmg.p2p.base.service.impl.PhoneServiceImpl.sendPhone(PhoneServiceImpl.java:69)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    at com.sun.proxy.$Proxy30.sendPhone(Unknown Source)
    at com.xmg.test.SendMailTest.testSendPhone(SendMailTest.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:232)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:175)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

百度查找原因也說找不到jar包 不過工程jar包都在(不知道測試類直接運行為什麽報錯 望大神解釋下)

然而 我在maven install把工程打成jar包時卻能正常運行 並且能正常返回結果碼並且正常發送短信

阿裏雲短信服務Java版