本文以容联云为例发送短信验证码,阿里云要收费
加入maven依赖
< ! -- 加入容联云通讯依赖 发送短信-- >
< dependency>
< groupId> com. cloopen< / groupId>
< artifactId> java- sms- sdk< / artifactId>
< version> 1.0 .3 < / version>
< / dependency>
新建配置文件保存相关配置信息
import org. springframework. boot. context. properties. ConfigurationProperties;
import org. springframework. stereotype. Component;
@Component
@ConfigurationProperties ( prefix = "sms" )
public class SmsConfig {
private String serverip;
private String port;
private String accountsid;
private String accounttoken;
private String appid;
private String templateid;
private String expiretime;
public String getServerip ( ) {
return serverip;
}
public void setServerip ( String serverip) {
this . serverip = serverip;
}
public String getPort ( ) {
return port;
}
public void setPort ( String port) {
this . port = port;
}
public String getAccountsid ( ) {
return accountsid;
}
public void setAccountsid ( String accountsid) {
this . accountsid = accountsid;
}
public String getAccounttoken ( ) {
return accounttoken;
}
public void setAccounttoken ( String accounttoken) {
this . accounttoken = accounttoken;
}
public String getAppid ( ) {
return appid;
}
public void setAppid ( String appid) {
this . appid = appid;
}
public String getTemplateid ( ) {
return templateid;
}
public void setTemplateid ( String templateid) {
this . templateid = templateid;
}
public String getExpiretime ( ) {
return expiretime;
}
public void setExpiretime ( String expiretime) {
this . expiretime = expiretime;
}
}
在application.yml中配置相关参数 (在容联云注册账号并获取相关参数)
sms:
serverip: app. cloopen. com
port: 8883
accountsid:
accounttoken:
appid:
templateid: 1
expiretime: 2
创建工具类
import cn. itrip. auth. config. SmsConfig;
import com. cloopen. rest. sdk. BodyType;
import com. cloopen. rest. sdk. CCPRestSmsSDK;
import org. slf4j. Logger;
import org. slf4j. LoggerFactory;
import org. springframework. stereotype. Service;
import javax. annotation. Resource;
import java. util. HashMap;
@Service ( "registerSendMessageService" )
public class RegisterSendMessageServiceImpl implements RegisterSendMessageService {
private static final Logger logger = LoggerFactory. getLogger ( RegisterSendMessageServiceImpl. class ) ;
@Resource
private SmsConfig smsConfig;
@Override
public boolean sendMesageByPhone ( String userCode, String code) throws Exception {
logger. info ( "send message by phone userCode:{},code:{}" , userCode, code) ;
CCPRestSmsSDK sdk = new CCPRestSmsSDK ( ) ;
sdk. init ( smsConfig. getServerip ( ) , smsConfig. getPort ( ) ) ;
sdk. setAccount ( smsConfig. getAccountsid ( ) , smsConfig. getAccounttoken ( ) ) ;
sdk. setAppId ( smsConfig. getAppid ( ) ) ;
sdk. setBodyType ( BodyType. Type_JSON) ;
String[ ] datas = new String [ 2 ] ;
datas[ 0 ] = code;
datas[ 1 ] = smsConfig. getExpiretime ( ) ;
HashMap< String, Object> result = sdk. sendTemplateSMS ( userCode, smsConfig. getTemplateid ( ) , datas) ;
logger. info ( "状态码={},message=:{}" , result. get ( "statusCode" ) , result. get ( "statusMsg" ) ) ;
if ( "000000" . equals ( result. get ( "statusCode" ) ) ) {
return true ;
}
return false ;
}
}
在Service中注入工具类并传参,调用,返回boolean类型结果
@Resource
private RegisterSendMessageService sendMessageService;
String code = String. valueOf ( MD5. getRandomCode ( ) ) ;
boolean result = sendMessageService. sendMesageByPhone ( "手机号" , "验证码" ) ;
补充:MD5工具类
import java. security. MessageDigest;
import java. security. NoSuchAlgorithmException;
import java. util. Random;
public class MD5 {
public static String getMd5 ( String plainText, int length) {
try {
MessageDigest md = MessageDigest. getInstance ( "MD5" ) ;
md. update ( plainText. getBytes ( ) ) ;
byte b[ ] = md. digest ( ) ;
int i;
StringBuffer buf = new StringBuffer ( "" ) ;
for ( int offset = 0 ; offset < b. length; offset++ ) {
i = b[ offset] ;
if ( i < 0 )
i += 256 ;
if ( i < 16 )
buf. append ( "0" ) ;
buf. append ( Integer. toHexString ( i) ) ;
}
return buf. toString ( ) . substring ( 0 , length) ;
} catch ( NoSuchAlgorithmException e) {
e. printStackTrace ( ) ;
return null;
}
}
public static int getRandomCode ( ) {
int max= 9999 ;
int min= 1111 ;
Random random = new Random ( ) ;
return random. nextInt ( max) % ( max- min+ 1 ) + min;
}
public static void main ( String[ ] args) {
System. out. println ( MD5. getMd5 ( "helloadsfdsffsf" , 32 ) ) ;
System. out. println ( getRandomCode ( ) ) ;
}
}