验证码登录是比较广泛的登录模式,分享一下验证码登录流程
先获取验证码
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);
}
注意: