引入依赖
< dependency> < groupId> com.aliyun</ groupId> < artifactId> aliyun-java-sdk-core</ artifactId> < version> 4.6.3</ version>
</ dependency>
< dependency> < groupId> com.aliyun</ groupId> < artifactId> aliyun-java-sdk-dysmsapi</ artifactId> < version> 2.2.1</ version>
</ dependency>
配置文件
sms : aliyun : enabled : true endpoint : cn- hangzhouaccess-key-id : xxxxxxxxaccess-key-secret : xxxxxxxxsign-name : xxxxxxxxtemplate-code : xxxxxxxx
属性类
package com. qiangesoft. sms. config ; import lombok. Data ;
import org. springframework. boot. context. properties. ConfigurationProperties ;
@Data
@ConfigurationProperties ( prefix = "sms.aliyun" )
public class AliyunSmsProperties { private boolean enabled = true ; private String endpoint; private String accessKeyId; private String accessKeySecret; private String signName; private String templateCode; }
配置类
package com. qiangesoft. sms. config ; import com. aliyuncs. DefaultAcsClient ;
import com. aliyuncs. IAcsClient ;
import com. aliyuncs. profile. DefaultProfile ;
import com. aliyuncs. profile. IClientProfile ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. boot. autoconfigure. condition. ConditionalOnMissingBean ;
import org. springframework. boot. autoconfigure. condition. ConditionalOnProperty ;
import org. springframework. boot. context. properties. EnableConfigurationProperties ;
import org. springframework. context. annotation. Bean ;
import org. springframework. context. annotation. Configuration ;
@Configuration
@EnableConfigurationProperties ( AliyunSmsProperties . class )
@ConditionalOnProperty ( prefix = "sms.aliyun" , name = "enabled" , havingValue = "true" )
public class AliyunSmsConfiguration { private static final String PRODUCT = "Dysmsapi" ; private static final String DOMAIN = "dysmsapi.aliyuncs.com" ; @Autowired private AliyunSmsProperties aliyunSmsProperties; @Bean @ConditionalOnMissingBean public IAcsClient acsClient ( ) { String endPoint = aliyunSmsProperties. getEndpoint ( ) ; String accessKeyId = aliyunSmsProperties. getAccessKeyId ( ) ; String accessKeySecret = aliyunSmsProperties. getAccessKeySecret ( ) ; IClientProfile profile = DefaultProfile . getProfile ( endPoint, accessKeyId, accessKeySecret) ; DefaultProfile . addEndpoint ( endPoint, PRODUCT , DOMAIN ) ; return new DefaultAcsClient ( profile) ; } }
短信发送实现
package com. qiangesoft. sms. handler ; import org. apache. commons. lang3. StringUtils ; import java. util. Collection ;
import java. util. Collections ;
import java. util. Map ;
public interface SendHandler { boolean send ( Collection < String > phones, Map < String , Object > param) ; default boolean send ( String phone, Map < String , Object > param) { if ( StringUtils . isBlank ( phone) ) { return false ; } return send ( Collections . singletonList ( phone) , param) ; } boolean send ( Collection < String > phones, String templateCode, Map < String , Object > param) ; default boolean send ( String phone, String templateCode, Map < String , Object > param) { if ( StringUtils . isBlank ( phone) ) { return false ; } return send ( Collections . singletonList ( phone) , param) ; }
}
package com. qiangesoft. sms. handler ; import com. alibaba. fastjson2. JSONObject ;
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. qiangesoft. sms. config. AliyunSmsProperties ;
import lombok. RequiredArgsConstructor ;
import lombok. extern. slf4j. Slf4j ;
import org. apache. commons. lang3. StringUtils ;
import org. springframework. stereotype. Service ; import java. util. Collection ;
import java. util. Map ;
@Slf4j
@RequiredArgsConstructor
@Service
public class AliyunSendHandler implements SendHandler { private final IAcsClient acsClient; private final AliyunSmsProperties aliyunSmsProperties; @Override public boolean send ( Collection < String > phones, Map < String , Object > param) { return this . send ( phones, aliyunSmsProperties. getTemplateCode ( ) , param) ; } @Override public boolean send ( Collection < String > phones, String templateCode, Map < String , Object > param) { SendSmsRequest request = new SendSmsRequest ( ) ; request. setSysMethod ( MethodType . POST ) ; request. setPhoneNumbers ( StringUtils . join ( phones, "," ) ) ; request. setSignName ( aliyunSmsProperties. getSignName ( ) ) ; request. setTemplateCode ( aliyunSmsProperties. getTemplateCode ( ) ) ; request. setTemplateParam ( JSONObject . toJSONString ( param) ) ; try { SendSmsResponse sendSmsResponse = acsClient. getAcsResponse ( request) ; if ( "OK" . equals ( sendSmsResponse. getCode ( ) ) ) { return true ; } log. debug ( "send fail[code={}, message={}]" , sendSmsResponse. getCode ( ) , sendSmsResponse. getMessage ( ) ) ; } catch ( Exception e) { log. debug ( e. getMessage ( ) , e) ; } return false ; } }
测试
package com. qiangesoft. sms. controller ; import com. qiangesoft. sms. handler. SendHandler ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. web. bind. annotation. GetMapping ;
import org. springframework. web. bind. annotation. RequestMapping ;
import org. springframework. web. bind. annotation. RestController ; import java. util. HashMap ;
import java. util. Map ;
@RequestMapping ( "/sms" )
@RestController
public class SmsController { @Autowired private SendHandler sendHandler; @GetMapping ( "/send" ) public Boolean send ( ) { Map < String , Object > param = new HashMap < > ( ) ; param. put ( "name" , "张三" ) ; param. put ( "jds" , 111 ) ; param. put ( "adds" , "顶顶顶帆帆" ) ; return sendHandler. send ( "15222222222" , param) ; } }