一、整合微服务
创建service-sms微服务
添加依赖:
<!--阿里云短信-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
</dependency>
配置yml文件:
server:
port: 8120 # 服务端口
spring:
profiles:
active: dev # 环境设置
application:
name: service-sms # 服务名
redis:
host: localhost
port: 6379
database: 0
timeout: 3000ms #最大等待时间,超时则抛出异常,否则请求一直等待
lettuce:
pool:
max-active: 20 #最大连接数,负值表示没有限制,默认8
max-wait: -1 #最大阻塞等待时间,负值表示没限制,默认-1
max-idle: 8 #最大空闲连接,默认8
min-idle: 0 #最小空闲连接,默认0
#阿里云短信
aliyun:
sms:
region-id: cn-shenzhen
key-id: 你的key-id
key-secret: 你的key-secret
template-code: SMS_154950909
sign-name: 阿里云短信测试
编写工具类:获取配置文件的内容
@Data
@Component
@ConfigurationProperties(prefix = "aliyun.sms")
public class SmsProperties implements InitializingBean {
private String regionId;
private String keyId;
private String keySecret;
private String templateCode;
private String signName;
public static String REGION_ID;
public static String KEY_ID;
public static String KEY_SECRET;
public static String TEMPLATE_CODE;
public static String SIGN_NAME;
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("当读取完所有属性后");
System.out.println(regionId);
System.out.println(keyId);
System.out.println(keySecret);
System.out.println(templateCode);
System.out.println(signName);
System.out.println("将读取到的属性赋值静态属性");
KEY_ID = keyId;
REGION_ID = regionId;
KEY_SECRET = keySecret;
TEMPLATE_CODE = templateCode;
SIGN_NAME = signName;
}
}
service层:
public interface SmsService {
void sendCode(String mobile);
}
@Service
public class SmsServiceImpl implements SmsService {
@Autowired
RedisTemplate redisTemplate;
@Override
public void sendCode(String mobile) {
// 校验手机号码
boolean b = RegexValidateUtils.checkCellphone(mobile);
Assert.isTrue(b, ResponseEnum.MOBILE_ERROR);
// 生成验证码
String fourBitRandom = RandomUtils.getFourBitRandom();
// 调用阿里云api
// 公共参数
DefaultProfile defaultProfile = DefaultProfile.getProfile(SmsProperties.REGION_ID,SmsProperties.KEY_ID,SmsProperties.KEY_SECRET);
// 请求客户端
IAcsClient iAcsClient = new DefaultAcsClient(defaultProfile);
// 请求参数
CommonRequest commonRequest = new CommonRequest();
commonRequest.setSysMethod(MethodType.POST);
commonRequest.setSysDomain("dysmsapi.aliyuncs.com");
commonRequest.setSysAction("SendSms");
commonRequest.setSysVersion("2017-05-25");
commonRequest.putQueryParameter("PhoneNumbers",mobile);//19967505202
commonRequest.putQueryParameter("SignName",SmsProperties.SIGN_NAME);
commonRequest.putQueryParameter("TemplateCode",SmsProperties.TEMPLATE_CODE);
Map<String,String> map = new HashMap<>();
map.put("code",fourBitRandom);
commonRequest.putQueryParameter("TemplateParam", JSON.toJSONString(map));// 如果是短信验证码,需要加入参数
// 发送请求,返回结果
CommonResponse commonResponse = null;
try {
commonResponse = iAcsClient.getCommonResponse(commonRequest);
} catch (ClientException e) {
e.printStackTrace();
}
System.out.println(commonResponse);
// 发送给redis
String data = commonResponse.getData();
Gson gson = new Gson();
HashMap<String, String> resultMap = gson.fromJson(data, HashMap.class);
String commonResponseCode = resultMap.get("Code");
Assert.isTrue((null!=commonResponseCode)&&(commonResponseCode.equals("OK")),ResponseEnum.ERROR);
redisTemplate.opsForValue().set("srb:sms:code:" + mobile,fourBitRandom);
}
}
controller层:
@RestController
@RequestMapping("/api/sms")
@CrossOrigin
public class ApiSmsController {
@Autowired
SmsService smsService;
@GetMapping("sendCode/{mobile}")
public R sendCode(@PathVariable String mobile){
smsService.sendCode(mobile);
return R.ok();
}
}
二、如何实现使用验证码注册的功能,业务流程?
A 注册登录阿里云,申请签名模板,接口授权,整合(短信)
B 新建sms短信微服务(读取公共参数配置文件),导入sdk,实现短信验证码接口
C 短信注册业务流程:
1 前端输入手机号,点击获取验证码
2 短信服务生成验证码,调用阿里云(sms接口服务),发送验证码
3 验证码发送成功后,将验证码存储到服务器缓存(redis)
4 用户输入验证码,点击注册
5 后端用户服务验证验证码,注册用户