阿里云发送短信
1.具備條件
1.阿里云開通短信服務
進入阿里云搜索短信就有對應的短信服務,支付寶購買后進入短信控制臺,會有對應的短信服務等信息。包括國內學習、業務統計、系統設置等。
注意:簽名和模板不支持個人用戶申請未上線業務
?2.控制臺測試
填寫對應信息即可,手機就會收到驗證碼信息。
?2.java整合短信服務
1.代碼開發
1.獲取?AccessKey和secret-key
因為之前做oss對象存儲的時候已經獲取過AccessKey和secret-key,所以需要在用戶中綁定并授權短信服務即可。
?2.SpringBoot整合阿里云短信
首先看下官方文檔的介紹與說明。
1.pom文件
<dependency><groupId>com.aliyun</groupId><artifactId>dysmsapi20170525</artifactId><version>2.0.18</version> </dependency>2.配置文件
?spring.cloud.nacos.discovery.server-addr:nacos服務注冊地址
?alicloud.secrect-key:密鑰
?alicloud.access-key:密鑰
?sms.endpoint:地域節點
?sms.templateCode:模板
3.封裝組件
其實就是把官方文檔的代碼拿過來自己用。
@Component @Data @ConfigurationProperties(prefix = "spring.cloud.alicloud.sms") //和配置文件綁定 public class SmsComponent {// private String host; // private String path; // private String templateId; // private String appcode;@Value("${spring.cloud.alicloud.access-key}")private String accessId;@Value("${spring.cloud.alicloud.secret-key}")private String accessKey;/*** 配置文件對應的*/private String endpoint;private String templateCode; //public void sendSmsCode(String phone,String code) throws Exception { // // // String method = "POST"; // // Map<String, String> headers = new HashMap<String, String>(); // //最后在header中的格式(中間是英文空格)為Authorization:APPCODE 83359fd73fe94948385f570e3c139105 // headers.put("Authorization", "APPCODE " + appcode); // //根據API的要求,定義相對應的Content-Type // headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); // Map<String, String> querys = new HashMap<String, String>(); // Map<String, String> bodys = new HashMap<String, String>(); // bodys.put("mobile", phone); // bodys.put("tag", code); // bodys.put("templateId", templateId); // // // try { // // HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys); // System.out.println(response.toString()); // //獲取response的body // System.out.println(EntityUtils.toString(response.getEntity())); // } catch (Exception e) { // e.printStackTrace(); // } // }Config config = new Config()// 您的 AccessKey ID.setAccessKeyId(accessId)// 您的 AccessKey Secret.setAccessKeySecret(accessKey);// 訪問的域名config.endpoint = endpoint;Client client = new Client(config);SendSmsRequest sendSmsRequest = new SendSmsRequest().setSignName("阿里云短信測試").setTemplateCode(templateCode).setPhoneNumbers(phone).setTemplateParam("{\"code\":\""+code+"\"}");RuntimeOptions runtime = new RuntimeOptions();SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);System.out.println(sendSmsResponse);}}4.發送短信接口
@ResponseBody@GetMapping("/sms/sendCode")public R sendCode(@RequestParam("phone") String phone) throws Exception {String redisCode = stringRedisTemplate.opsForValue().get(AuthConstant.SMS_CODE_CACHE_PREFIX + phone);if (!StringUtils.isEmpty(redisCode)) {long currentTime = Long.parseLong(redisCode.split("_")[1]);//系統時間減去當前時間小于60s不能發送if (System.currentTimeMillis() - currentTime < 60000) {return R.error(BizCodeEnum.SMS_CODE_EXCEPTION.getCode(), BizCodeEnum.SMS_CODE_EXCEPTION.getMsg());}}//接口防刷//驗證碼的再次校驗 redis保存 存key->手機號 value->驗證碼 sms:code:1234567898--->123456String code = UUID.randomUUID().toString().substring(0, 5);String subString = code + "_" + System.currentTimeMillis();//redis緩存驗證碼 防止同一個手機號在60s內再次發送驗證碼stringRedisTemplate.opsForValue().set(AuthConstant.SMS_CODE_CACHE_PREFIX + phone, subString, 10, TimeUnit.MINUTES);feignService.sendCode(phone, code);return R.ok();}?需要將前端傳遞的手機號進行發送短信收取驗證碼判斷,如果redis中為空調取短信發送服務(通過feign),如果redis不為空,判斷系統時間減去當前時間小于60s不能發送,這樣有效防止接口防刷惡意操作。前端注冊通過手機號調起后端接口,后端通過uuid等技術生成驗證碼并調取短信服務接口,使用戶收取驗證碼。
3.MD5&MD5鹽值加密
1.MD5
Message Digest algorithm 5,信息摘要算法
encode產生的和123456始終都會匹配。
@Overridepublic MemberEntity login(MemberLoginVo vo) {//獲取用戶名String loginAccount = vo.getLoginAccount();//獲取用戶的密碼String pagePassword = vo.getPassword();//去數據庫查詢MemberEntity memberEntity = baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("username", loginAccount).or().eq("mobile", loginAccount));if (memberEntity == null) {//登錄失敗return null;} else {//從數據庫返回的密碼字段值String password = memberEntity.getPassword();BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();//用頁面提交的代碼和數據庫注冊的鹽值進行匹配boolean b = passwordEncoder.matches(pagePassword, password);if (b) {return memberEntity;} else {return null;}}總結
- 上一篇: RED5与tomcat整合
- 下一篇: GET日志服务如何使用让你获得建立DT时