> 文章列表 > 尚融宝——阿里云短信验证功能(sms)

尚融宝——阿里云短信验证功能(sms)

尚融宝——阿里云短信验证功能(sms)

一、整合微服务

创建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: localhostport: 6379database: 0timeout: 3000ms #最大等待时间,超时则抛出异常,否则请求一直等待lettuce:pool:max-active: 20  #最大连接数,负值表示没有限制,默认8max-wait: -1    #最大阻塞等待时间,负值表示没限制,默认-1max-idle: 8     #最大空闲连接,默认8min-idle: 0     #最小空闲连接,默认0#阿里云短信
aliyun:sms:region-id: cn-shenzhenkey-id: 你的key-idkey-secret: 你的key-secrettemplate-code: SMS_154950909sign-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;@Overridepublic 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 {@AutowiredRedisTemplate redisTemplate;@Overridepublic 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);//19967505202commonRequest.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);// 发送给redisString 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 {@AutowiredSmsService 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 后端用户服务验证验证码,注册用户