java集成短信服务

验证码登录是比较广泛的登录模式,分享一下验证码登录流程
先获取验证码
1.请求资源

	@ApiOperation("获取验证码")
  @RequestMapping(value = "/getAuthCode", method = RequestMethod.GET)
  public CommonResult getAuthCode(@RequestParam String telephone) {
    String authCode = heUserService.generateAuthCode(telephone);
    AliMessageUtils.sendSMS(telephone,authCode);
    return CommonResult.success(authCode, "获取验证码成功");
  }

2.生成验证码service

String generateAuthCode(String telephone);

3.impl

  @Override
  public String generateAuthCode(String telephone) {
    StringBuilder sb = new StringBuilder();
    Random random = new Random();
    for (int i = 0; i < 6; i++) {
      sb.append(random.nextInt(10));
    }
    //设置验证码到缓存
    userCacheService.setAuthCode(telephone,sb.toString());
    return sb.toString();
  }

4.添加到缓存

 @CacheException
  @Override
  public void setAuthCode(String telephone, String authCode) {
    String key = REDIS_DATABASE + ":" + REDIS_KEY_AUTH_CODE + ":" + telephone;
    //向redis中存入验证码有效时间和key, value 5分钟
    redisService.set(key,authCode,REDIS_EXPIRE_AUTH_CODE);
  }

5.获取验证码

 @ApiOperation("获取验证码")
  @RequestMapping(value = "/getAuthCode", method = RequestMethod.GET)
  public CommonResult getAuthCode(@RequestParam String telephone) {
    String authCode = heUserService.generateAuthCode(telephone);
    AliMessageUtils.sendSMS(telephone,authCode);
    return CommonResult.success(authCode, "获取验证码成功");
  }

6.发送短信

/*
 @describe 阿里云短信工具类
 @params
 @return
 @author 何章怀晓
 @date 2020/8/26  18:48
 @other
 */
@Slf4j
public class AliMessageUtils {

  public static final String CONFIG_FILE = "aliyun-message.properties";
  public static String REGION_ID;
  public static String ACCESS_KEY_ID;
  public static String ACCESS_KEY_SECRET;
  public static String SIGN_NAME;
  public static String TEMPLATE_CODE;

  static {
    Properties prop = new Properties();
    InputStream is = null;
    // 加载配置
    ClassPathResource classPathResource = new ClassPathResource("aliyun-message.properties");
    try {
      is = classPathResource.getInputStream();
      if (null == is) {
        log.error("[阿里云短信工具类-初始化]失败,请提供配置文件:{}", CONFIG_FILE);
      } else {
        prop.load(is);
      }
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    REGION_ID = prop.getProperty("region.id", "");
    ACCESS_KEY_ID = prop.getProperty("access.key.id", "");
    ACCESS_KEY_SECRET = prop.getProperty("access.key.secret", "");
    SIGN_NAME = prop.getProperty("sign.name", "");
    TEMPLATE_CODE = prop.getProperty("template.code", "");
    log.info("[阿里云工具类-初始化]完成");
  }

  //发送短信
  public static void sendSMS(String phone, String authCode) {
    DefaultProfile profile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
    IAcsClient client = new DefaultAcsClient(profile);
    String codeSMS = "{\"code\":\"" + authCode + "\"}";
    CommonRequest request = new CommonRequest();
    request.setSysMethod(MethodType.POST);
    request.setSysDomain("dysmsapi.aliyuncs.com");
    request.setSysVersion("2017-05-25");
    request.setSysAction("SendSms");
    request.putQueryParameter("RegionId", REGION_ID);
    request.putQueryParameter("PhoneNumbers", phone);
    request.putQueryParameter("SignName", SIGN_NAME);
    request.putQueryParameter("TemplateCode", TEMPLATE_CODE);
    request.putQueryParameter("TemplateParam", codeSMS);
    try {
      CommonResponse response = client.getCommonResponse(request);
      System.out.println(response.getData());
    } catch (ServerException e) {
      e.printStackTrace();
    } catch (ClientException e) {
      e.printStackTrace();
    }
  }
}

在这里插入图片描述
7.登录

  @ApiOperation("会员登录(手机验证码登录)")
  @RequestMapping(value = "/CodeLogin", method = RequestMethod.POST)
  public CommonResult CodeLogin(@RequestParam String phone,
      @RequestParam String authCode) {
    HeUser heUser = heUserService.CodeLogin(phone, authCode);
    if (heUser.getToken() == null || heUser.getToken().equals("")) {
      return CommonResult.validateFailed("手机号码或验证码错误");
    }
    Map<String, Object> tokenMap = new HashMap<>();
    tokenMap.put("user", heUser);
    tokenMap.put("tokenHead", tokenHead);
    return CommonResult.success(tokenMap);
  }

在这里插入图片描述

注意:
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值