直奔主题:
maven地址:
<!--aliyun 短信发送-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.7.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-ecs</artifactId>
<version>4.11.0</version>
</dependency>
util 类:
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import java.io.IOException;
import java.util.Properties;
/*
pom.xml
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.0.3</version>
</dependency>
*/
public class SendSms {
public static String RunSend(String Telstr,String Codestr) {
ResourceLoader loader = new DefaultResourceLoader();
Resource resource = loader.getResource("classpath:oss.properties");
Properties ossProps = new Properties();
try {
ossProps.load(resource.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
String accessKeyId = ossProps.getProperty("oss.access_key_id");
String accessKeySecret = ossProps.getProperty("oss.access_key_secret");
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.setMethod(MethodType.POST);
request.setDomain("dysmsapi.aliyuncs.com");
request.setVersion("2017-05-25");
request.setAction("SendSms");
request.putQueryParameter("RegionId", "cn-hangzhou");
request.putQueryParameter("PhoneNumbers", Telstr);
request.putQueryParameter("SignName", "阿里云短信测试专用");
request.putQueryParameter("TemplateCode", "SMS_109685164");
request.putQueryParameter("TemplateParam", "{code:"+Codestr+"}");
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
return "";
}
}
java接口 验证码发送后需要存起来验证 这里存的是 数据库:
/**
* 登录时发送验证码
*
* @param mbMob
* @param session
* @return
*/
@RequestMapping("/loginSendCode")
@ResponseBody
public Map<String, Object> loginSendCode(String mbMob) {
//6位短信验证码
int number = (int) ((Math.random() * 9 + 1) * 100000);
int valnum = (int) ((Math.random() * 9 + 1) * 10);
//手机号是否存在
Member meb = iMemberService.ValTel(mbMob);
if (meb == null) {
return resultMap(Type.error, "手机号不存在,请注册!", "", null, "", "404");
}
Member valmb = new Member();
valmb.setMemberMob(mbMob);
List<Member> meblist = iMemberService.selectMemberList(valmb);
if (StringUtils.isEmpty(meblist)) {
return resultMap(Type.error, "手机号不存在,请注册!", "", null, "", "405");
} else {
//如果存在update
valmb.setId(meblist.get(0).getId());
valmb.setMemberPin(String.valueOf(number));
valmb.setMemberLot(Integer.valueOf(valnum));
iMemberService.updateMember(valmb);
}
//发送验证码
SendSms.RunSend(mbMob, String.valueOf(number));
return resultMap(Type.success, "验证码已发送,请注意查收", "", valmb.getMemberLot(), "", "101");
}