1、前期准备:配置阿里云短信服务
2、在项目中添加依赖
com.aliyun
aliyun-java-sdk-core
4.4.0
com.aliyun
aliyun-java-sdk-dysmsapi
1.0.0
3、添加阿里云短信服务类
@Component
public class SendNoteUtil {
@Autowired
private RedisService redisService;
@Value("${redis.key.prefix.authCode}")
private String REDIS_KEY_PREFIX_AUTH_CODE;
@Value("${redis.key.expire.authCode}")
private Long AUTH_CODE_EXPIRE_SECONDS;
//验证平台信息 开发者无需任何更改
private static final String dysmsapi = "dysmsapi.aliyuncs.com";
DefaultProfile profile = DefaultProfile.getProfile("default", "LTAI4G9pshKm3Hv9nM5ALaxg", "n9SvNggc4rcMb3Fi2QKNkVPFFMdV3L");
IAcsClient client = new DefaultAcsClient(profile);
//这一步的两个参数,一个是要发送验证码的手机号 一个是模板Code用来区分登录注册
public String sendNoteMessgae(String PhoneNumbers,String template){
StringBuilder sb = new StringBuilder();
Random random = new Random();
for(int i=0;i<6;i++){
sb.append(random.nextInt(10));
}
CommonRequest request = new CommonRequest();
//request.setSysProtocol(ProtocolType.HTTPS);
request.setSysMethod(MethodType.POST);
request.setSysDomain(dysmsapi);
request.setSysVersion("2017-05-25");
request.setSysAction("SendSms");
request.putQueryParameter("PhoneNumbers", PhoneNumbers);//接受验证码的手机号
request.putQueryParameter("SignName", "校园助手");//签名
//模板代码,我暂时用的参数,你可以直接写成模板码,模板码参考第八步
request.putQueryParameter("TemplateCode", template);
//用户定义的验证码内容
request.putQueryParameter("TemplateParam","{code:"+sb.toString()+"}");
//验证码绑定手机号并存储到redis
redisService.set(REDIS_KEY_PREFIX_AUTH_CODE+PhoneNumbers,sb.toString());
redisService.expire(REDIS_KEY_PREFIX_AUTH_CODE+PhoneNumbers,AUTH_CODE_EXPIRE_SECONDS);
try {
CommonResponse response = client.getCommonResponse(request);
String returnStr = response.getData();
System.out.println(returnStr);
JSONObject jsonObject = JSONObject.parseObject(returnStr);
//返回的信息
return jsonObject.getString("Message");
} catch (ServerException e) {
return e.getErrMsg();
} catch (ClientException e) {
return e.getErrMsg();
}
};
}
4、添加redis工具类
@Component
public class RedisUtils {
@Autowired
private RedisTemplate redisTemplate;
/**
* 读取缓存
*
* @param key
* @return
*/
public String get(final String key) {
return redisTemplate.opsForValue().get(key);
}
/**
* 写入缓存
*/
public boolean set(final String key, String value) {
boolean result = false;
try {
redisTemplate.opsForValue().set(key, value);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 更新缓存
*/
public boolean getAndSet(final String key, String value) {
boolean result = false;
try {
redisTemplate.opsForValue().getAndSet(key, value);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 删除缓存
*/
public boolean delete(final String key) {
boolean result = false;
try {
redisTemplate.delete(key);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
5、编写service层服务类
@Service
@Transactional
public class RedisServiceImpl implements RedisService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Override
public void set(String key, String value) {
stringRedisTemplate.opsForValue().set(key, value);
}
@Override
public String get(String key) {
return stringRedisTemplate.opsForValue().get(key);
}
@Override
public boolean expire(String key, long expire) {
return stringRedisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
@Override
public void remove(String key) {
stringRedisTemplate.delete(key);
}
@Override
public Long increment(String key, long delta) {
return stringRedisTemplate.opsForValue().increment(key,delta);
}
}
6、controller层
RestController
@RequestMapping(value = "/api/note")
public class SendNoteController {
@Autowired
private SendNoteUtil sendNoteUtil;
@Autowired
private RedisService redisService;
@Value("${redis.key.prefix.authCode}")
private String REDIS_KEY_PREFIX_AUTH_CODE;
@RequestMapping(value = "/sendNote",method = RequestMethod.GET)
public void sendNote(String phone, HttpServletResponse response){
String template = "SMS_191735081";
try {
response.getWriter().write(sendNoteUtil.sendNoteMessgae(phone,template));
} catch (IOException e) {
e.printStackTrace();
}
}
@RequestMapping(value = "/login",method = RequestMethod.POST)
public String login(String phone,String authCode){
//验证验证码
if(!verifyAuthCode(authCode,phone)){
return"验证码错误";
}
return "登陆成功";
}
//对输入的验证码进行校验
private boolean verifyAuthCode(String authCode, String telephone){
if(StringUtils.isEmpty(authCode)){
return false;
}
String realAuthCode = redisService.get(REDIS_KEY_PREFIX_AUTH_CODE + telephone);
return authCode.equals(realAuthCode);
}
}