痛點
- 阿里大於 SDK 未上傳 Maven Central。
- 專案打包時找不到本地 Jar。
解決方案
1. 下載 SDK 並安裝到本地倉庫
# 解壓獲得 alidayu-sdk.jar、top-sdk.jar
mvn install:install-file \
-DgroupId=com.taobao.sms1 -DartifactId=sms1 -Dversion=1.0.0 \
-Dpackaging=jar -Dfile=/path/alidayu-sdk.jar
mvn install:install-file \
-DgroupId=com.taobao.sms2 -DartifactId=sms2 -Dversion=1.0.0 \
-Dpackaging=jar -Dfile=/path/top-sdk.jar
2. pom.xml 關鍵配置
<dependencies>
<dependency><groupId>com.taobao.sms1</groupId><artifactId>sms1</artifactId><version>1.0.0</version></dependency>
<dependency><groupId>com.taobao.sms2</groupId><artifactId>sms2</artifactId><version>1.0.0</version></dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
</dependencies>
<build>
<plugins>
<plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin>
<!-- 解決打包找不到本地 jar -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArguments>
<extdirs>src/main/webapp/WEB-INF/lib</extdirs>
</compilerArguments>
</configuration>
</plugin>
</plugins>
</build>
3. 核心服務實現
@Service
public class DaYuService {
public Result sendSmsCode(String phone) {
String code = RandomUtil.randomCode(6);
TaobaoClient client = new DefaultTaobaoClient(
"https://gw.api.taobao.com/router/rest", "appkey", "secret");
AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
req.setSmsType("normal");
req.setSmsFreeSignName("我的應用");
req.setRecNum(phone);
req.setSmsTemplateCode("SMS_8926569");
req.setSmsParamString("{\"code\":\"" + code + "\"}");
try {
client.execute(req);
smsCache.save(phone, code); // 自行實現驗證碼存儲
return Result.ok();
} catch (ApiException e) {
return Result.fail("發送失敗");
}
}
}
關鍵提示
- groupId/artifactId 自定義即可,但需與
install:install-file 參數一致。
maven-compiler-plugin 的 extdirs 指向存放 jar 的目錄,確保打包時編譯器能找到。
- 生產環境建議改用阿里云短信服務新版 SDK(已上 Maven Central)。