记得上下班打卡 | git大法好,push需谨慎

Commit 30a9c638 authored by 张国柄's avatar 张国柄

发送验证码更换JAVA-API;

parent a6bc4291
......@@ -3,6 +3,10 @@ package com.liquidnet.service.adam.constant;
public class AdamRedisConst {
public static final String ADAM = "adam:";
public static final String VALID = ADAM.concat("valid:");
public static final String VALID_SMS_CODE_MOBILE = VALID.concat("sms:code:mobile");
public static final String IDENTITY = ADAM.concat("identity:");
public static final String IDENTITY_MOBILE = IDENTITY.concat("mobile:");
......@@ -44,7 +48,7 @@ public class AdamRedisConst {
public static final String LOCK_KEY_UMEMBER_NO = "adam:lk:member:no";
// // // // // // // // // //
public static final String LOCK_KEY_USMS_MOBILE = "adam:lk:sms:mobile:";
public static final String LOCK_KEY_SMS_CODE_MOBILE = "adam:lk:sms:code:mobile:";
public static final String LOCK_KEY_UREGISTER = "adam:lk:register:";
public static final String LOCK_KEY_UIDENTITY = "adam:lk:identity:";
public static final String LOCK_KEY_UMEMBER_CODE = "adam:lk:member:code:";
......
......@@ -30,7 +30,7 @@ public class SmsMessage implements Serializable, Cloneable {
private final static SmsMessage instance = new SmsMessage();
public static SmsMessage getNew() {
public static SmsMessage builder() {
try {
return (SmsMessage) instance.clone();
} catch (CloneNotSupportedException e) {
......
......@@ -11,6 +11,7 @@ import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.liquidnet.common.cache.redis.util.RedisUtil;
import com.liquidnet.common.cache.redisson.util.RedisLockUtil;
import com.liquidnet.common.mq.constant.MQConst;
import com.liquidnet.common.sms.constant.SmsEnum;
import com.liquidnet.common.sms.processor.SmsProcessor;
import com.liquidnet.commons.lang.constant.LnsEnum;
......@@ -24,22 +25,22 @@ import com.liquidnet.service.adam.service.AdamRdmService;
import com.liquidnet.service.adam.service.IAdamUserService;
import com.liquidnet.service.base.ErrorMapping;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.base.SmsMessage;
import com.liquidnet.service.base.UserPathDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DigestUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.HttpClientErrorException;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
......@@ -52,7 +53,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import static com.liquidnet.service.adam.constant.AdamRedisConst.LOCK_KEY_USMS_MOBILE;
import static com.liquidnet.service.adam.constant.AdamRedisConst.LOCK_KEY_SMS_CODE_MOBILE;
@ApiSupport(order = 10010)
@Api(tags = "用户登录")
......@@ -74,72 +75,60 @@ public class AdamLoginController {
@Autowired
IAdamUserService adamUserService;
@Autowired
SmsProcessor smsProcessor;
RabbitTemplate rabbitTemplate;
// @Autowired
// SmsProcessor smsProcessor;
@Value("${liquidnet.reviewer.app-login.mobile}")
private String reviewMobile;
private static final String PHP_API_SMS_CODE_SEND = "/smsCode";
private static final String PHP_API_SMS_CODE_VALID = "/smsValidation";
/*@ApiOperationSupport(order = 1)
@ApiOperation(value = "手机号密码登录")
@ApiImplicitParams({
@ApiImplicitParam(type = "form", dataType = "String", name = "mobile", value = "手机号"),
@ApiImplicitParam(type = "form", dataType = "String", name = "password", value = "密码"),
})
@PostMapping(value = {""})
public ResponseDto<AdamLoginInfoVo> loginByPwd(@RequestParam String mobile, @RequestParam String password) {
log.info("mobile:{},pwd:{}", mobile, password);
DigestUtils.md5DigestAsHex((password + "salt_").getBytes(StandardCharsets.UTF_8));
return ResponseDto.success(AdamLoginInfoVo.getNew());
}*/
@GetMapping(value = {"gen"})
public void genID() {
log.debug("-nextSnowId:{}", IDGenerator.nextSnowId());
log.debug("nextMilliId:{}", IDGenerator.nextMilliId());
log.debug("nextMilliId:{}", IDGenerator.nextTimeId());
log.debug("nextMilliId:{}", IDGenerator.get32UUID());
ObjectNode smsNode = JsonUtils.OM().createObjectNode();
smsNode.put("code", "123456");
smsProcessor.aliyunDysmsSend("13753596360",
SmsEnum.ADSignName.正在现场.name(), SmsEnum.ADTemplate.SMS_109535335.name(),
smsNode.toString());
ArrayNode smsBatchPhones = JsonUtils.OM().createArrayNode(),
smsBatchSignNames = JsonUtils.OM().createArrayNode(),
smsBatchTemplateParams = JsonUtils.OM().createArrayNode();
ObjectNode smsBatchTemplateParam = JsonUtils.OM().createObjectNode();
smsBatchPhones.add("13753596360");
smsBatchPhones.add("17701223310");
smsBatchSignNames.add(SmsEnum.ADSignName.正在现场.name());
smsBatchSignNames.add(SmsEnum.ADSignName.正在现场.name());
smsBatchTemplateParam.put("userName", "张某人");
smsBatchTemplateParam.put("aName", "大美");
smsBatchTemplateParam.put("time", "2021-07-10");
smsBatchTemplateParam.put("siteName", "北京");
smsBatchTemplateParam.put("url", "show");
smsBatchTemplateParams.add(smsBatchTemplateParam.deepCopy());
smsBatchTemplateParam.put("userName", "张某人");
smsBatchTemplateParam.put("aName", "大美2");
smsBatchTemplateParam.put("time", "2021-07-11");
smsBatchTemplateParam.put("siteName", "天津");
smsBatchTemplateParam.put("url", "show2");
smsBatchTemplateParams.add(smsBatchTemplateParam);
smsProcessor.aliyunDysmsSendBatch(smsBatchPhones.toPrettyString(), smsBatchSignNames.toString(), SmsEnum.ADTemplate.SMS_109535335.name(), smsBatchTemplateParams.toString());
}
// @GetMapping(value = {"gen"})
// public void genID() {
// log.debug("-nextSnowId:{}", IDGenerator.nextSnowId());
// log.debug("nextMilliId:{}", IDGenerator.nextMilliId());
// log.debug("nextMilliId:{}", IDGenerator.nextTimeId());
// log.debug("nextMilliId:{}", IDGenerator.get32UUID());
//
//
// ObjectNode smsNode = JsonUtils.OM().createObjectNode();
// smsNode.put("code", "123456");
// smsProcessor.aliyunDysmsSend("13753596360",
// SmsEnum.ADSignName.正在现场.name(), SmsEnum.ADTemplate.SMS_109535335.name(),
// smsNode.toString());
//
//
//
// ArrayNode smsBatchPhones = JsonUtils.OM().createArrayNode(),
// smsBatchSignNames = JsonUtils.OM().createArrayNode(),
// smsBatchTemplateParams = JsonUtils.OM().createArrayNode();
// ObjectNode smsBatchTemplateParam = JsonUtils.OM().createObjectNode();
//
// smsBatchPhones.add("13753596360");
// smsBatchPhones.add("17701223310");
//
// smsBatchSignNames.add(SmsEnum.ADSignName.正在现场.name());
// smsBatchSignNames.add(SmsEnum.ADSignName.正在现场.name());
//
// smsBatchTemplateParam.put("userName", "张某人");
// smsBatchTemplateParam.put("aName", "大美");
// smsBatchTemplateParam.put("time", "2021-07-10");
// smsBatchTemplateParam.put("siteName", "北京");
// smsBatchTemplateParam.put("url", "show");
//
// smsBatchTemplateParams.add(smsBatchTemplateParam.deepCopy());
//
// smsBatchTemplateParam.put("userName", "张某人");
// smsBatchTemplateParam.put("aName", "大美2");
// smsBatchTemplateParam.put("time", "2021-07-11");
// smsBatchTemplateParam.put("siteName", "天津");
// smsBatchTemplateParam.put("url", "show2");
// smsBatchTemplateParams.add(smsBatchTemplateParam);
//
// smsProcessor.aliyunDysmsSendBatch(smsBatchPhones.toPrettyString(), smsBatchSignNames.toString(), SmsEnum.ADTemplate.SMS_109535335.name(), smsBatchTemplateParams.toString());
// }
@ApiOperationSupport(order = 2)
@ApiOperation(value = "发送验证码")
......@@ -149,36 +138,44 @@ public class AdamLoginController {
@GetMapping(value = {"send"})
public ResponseDto<Object> sendSms(@Pattern(regexp = "\\d{11}", message = "手机号格式有误") @RequestParam String mobile) {
log.debug("send to mobile:{}", mobile);
if (RedisLockUtil.tryLock(LOCK_KEY_USMS_MOBILE + mobile, 1, 5)) {
Map<String, Object> respMap = null;
String respStr = null;
try {
LinkedMultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
paramsMap.add("mobile", mobile);
long s = System.currentTimeMillis();
respStr = HttpUtil.postToPhpApi(env.getProperty("liquidnet.url-service.url") + PHP_API_SMS_CODE_SEND, paramsMap);
log.debug("###PHP.API[{}].RESP[{}]", PHP_API_SMS_CODE_SEND, respStr);
log.debug("#PHP.API耗时:{}ms", System.currentTimeMillis() - s);
respMap = JsonUtils.fromJson(respStr, Map.class);
} catch (Exception e) {
RedisLockUtil.unlock(LOCK_KEY_USMS_MOBILE + mobile);
if (e instanceof HttpClientErrorException) {
log.error("PHP.API验证码发送异常[mobile:{},respStr:{},ex:{}]", mobile, respStr, e.getLocalizedMessage());
HttpClientErrorException ex = (HttpClientErrorException) e;
JsonNode exBody = JsonUtils.fromJson(ex.getResponseBodyAsString(), JsonNode.class);
return ResponseDto.failure("10003", exBody.get("message").asText());
}
log.error("PHP.API验证码发送异常[mobile:{},respStr:{}]", mobile, respStr, e);
return ResponseDto.failure(ErrorMapping.get("10003"));
}
if (!CollectionUtils.isEmpty(respMap) && StringUtils.equalsIgnoreCase("OK", (String) respMap.get("message"))) {
if (RedisLockUtil.tryLock(LOCK_KEY_SMS_CODE_MOBILE + mobile, 1, 5)) {
// Map<String, Object> respMap = null;
// String respStr = null;
// try {
// LinkedMultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
// paramsMap.add("mobile", mobile);
//
// long s = System.currentTimeMillis();
// respStr = HttpUtil.postToPhpApi(env.getProperty("liquidnet.url-service.url") + PHP_API_SMS_CODE_SEND, paramsMap);
// log.debug("###PHP.API[{}].RESP[{}]", PHP_API_SMS_CODE_SEND, respStr);
// log.debug("#PHP.API耗时:{}ms", System.currentTimeMillis() - s);
// respMap = JsonUtils.fromJson(respStr, Map.class);
// } catch (Exception e) {
// RedisLockUtil.unlock(LOCK_KEY_USMS_MOBILE + mobile);
// if (e instanceof HttpClientErrorException) {
// log.error("PHP.API验证码发送异常[mobile:{},respStr:{},ex:{}]", mobile, respStr, e.getLocalizedMessage());
// HttpClientErrorException ex = (HttpClientErrorException) e;
// JsonNode exBody = JsonUtils.fromJson(ex.getResponseBodyAsString(), JsonNode.class);
// return ResponseDto.failure("10003", exBody.get("message").asText());
// }
// log.error("PHP.API验证码发送异常[mobile:{},respStr:{}]", mobile, respStr, e);
// return ResponseDto.failure(ErrorMapping.get("10003"));
// }
// if (!CollectionUtils.isEmpty(respMap) && StringUtils.equalsIgnoreCase("OK", (String) respMap.get("message"))) {
// return ResponseDto.success();
// } else {
// RedisLockUtil.unlock(LOCK_KEY_USMS_MOBILE + mobile);
// log.warn("PHP.API验证码发送失败[mobile:{},respStr:{}]", mobile, respStr);
// return ResponseDto.failure(ErrorMapping.get("10003"));
// }
String smsCode = RandomStringUtils.randomNumeric(6);
SmsMessage smsMessage = SmsMessage.builder().setPhone(mobile).setSignName(SmsEnum.ADSignName.正在现场.name())
.setTemplateCode(SmsEnum.ADTemplate.SMS_109535335.name())
.setTemplateParam("code", smsCode);
rabbitTemplate.convertAndSend(MQConst.EX_LNS_SMS_SENDER, MQConst.RK_SMS_CODE, smsMessage);
adamRdmService.setSmsCodeByMobile(mobile, smsCode);
return ResponseDto.success();
} else {
RedisLockUtil.unlock(LOCK_KEY_USMS_MOBILE + mobile);
log.warn("PHP.API验证码发送失败[mobile:{},respStr:{}]", mobile, respStr);
return ResponseDto.failure(ErrorMapping.get("10003"));
}
} else {
return ResponseDto.failure(ErrorMapping.get("10000"));
}
......@@ -196,7 +193,8 @@ public class AdamLoginController {
@Pattern(regexp = "\\d{6}", message = "验证码格式有误")
@RequestParam String code) {
log.debug("mobile:{},code:{}", mobile, code);
if (!this.checkSmsCode(mobile, code)) return ResponseDto.failure(ErrorMapping.get("10004"));
ResponseDto checkSmsCodeDto = this.checkSmsCode(mobile, code);
if (!checkSmsCodeDto.isSuccess()) return checkSmsCodeDto;
String uid = adamRdmService.getUidByMobile(mobile);
boolean toRegister = StringUtils.isEmpty(uid);
......@@ -280,8 +278,9 @@ public class AdamLoginController {
loginInfoVo.setUserMemberVo(adamRdmService.getUserMemberVoByUid(uid));
// loginInfoVo.setMemberVo(adamRdmService.getMemberSimpleVo());
} else {// 新账号注册
if (!this.checkSmsCode(parameter.getMobile(), parameter.getCode())) {
return ResponseDto.failure(ErrorMapping.get("10004"));
ResponseDto checkSmsCodeDto = this.checkSmsCode(parameter.getMobile(), parameter.getCode());
if (!checkSmsCodeDto.isSuccess()) {
return checkSmsCodeDto;
}
AdamUserInfoVo registerUserInfo = adamUserService.register(parameter);
if (null == registerUserInfo) {
......@@ -369,12 +368,12 @@ public class AdamLoginController {
/* ---------------------------- Internal Method ---------------------------- */
private boolean checkSmsCode(String mobile, String code) {
private ResponseDto checkSmsCode(String mobile, String code) {
Integer switchGrayLoginSms = (Integer) redisUtil.get(AdamRedisConst.SWITCH_GRAY_LOGIN_SMS);
if (null != switchGrayLoginSms) {
if (switchGrayLoginSms == 615243) {
if (CurrentUtil.GRAY_LOGIN_SMS_CODE.equals(code)) {
return true;
return ResponseDto.success();
}
}
if (switchGrayLoginSms == 612543) {
......@@ -382,31 +381,37 @@ public class AdamLoginController {
Arrays.asList(LnsEnum.ENV.dev.name(), LnsEnum.ENV.test.name()).contains(env.getProperty(CurrentUtil.CK_ENV_ACTIVE))
) {
if (CurrentUtil.GRAY_LOGIN_SMS_CODE.equals(code)) {
return true;
return ResponseDto.success();
}
}
}
}
LinkedMultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
paramsMap.add("mobile", mobile);
paramsMap.add("code", code);
// LinkedMultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
// paramsMap.add("mobile", mobile);
// paramsMap.add("code", code);
//
// String respStr = null;
// try {
// respStr = HttpUtil.postToPhpApi(env.getProperty("liquidnet.url-service.url") + PHP_API_SMS_CODE_VALID, paramsMap);
// log.debug("###PHP.API[{}].RESP:{}", PHP_API_SMS_CODE_VALID, respStr);
// Map respMap = JsonUtils.fromJson(respStr, Map.class);
// if (!CollectionUtils.isEmpty(respMap) && StringUtils.equalsIgnoreCase("OK", (String) respMap.get("message"))) {
// return true;
// } else {
// log.warn("PHP.API验证码验证失败[mobile:{},code:{},respStr:{}]", mobile, code, respStr);
// return false;
// }
// } catch (Exception e) {
// log.error("PHP.API验证码验证异常[mobile:{},code:{},respStr:{}]", mobile, code, respStr, e);
// return false;
// }
String respStr = null;
try {
respStr = HttpUtil.postToPhpApi(env.getProperty("liquidnet.url-service.url") + PHP_API_SMS_CODE_VALID, paramsMap);
log.debug("###PHP.API[{}].RESP:{}", PHP_API_SMS_CODE_VALID, respStr);
Map respMap = JsonUtils.fromJson(respStr, Map.class);
if (!CollectionUtils.isEmpty(respMap) && StringUtils.equalsIgnoreCase("OK", (String) respMap.get("message"))) {
return true;
} else {
log.warn("PHP.API验证码验证失败[mobile:{},code:{},respStr:{}]", mobile, code, respStr);
return false;
}
} catch (Exception e) {
log.error("PHP.API验证码验证异常[mobile:{},code:{},respStr:{}]", mobile, code, respStr, e);
return false;
String smsCodeByMobile = adamRdmService.getSmsCodeByMobile(mobile);
if (null == smsCodeByMobile) {
return ResponseDto.failure(ErrorMapping.get("10003"));
}
return smsCodeByMobile.equals(code) ? ResponseDto.success() : ResponseDto.failure(ErrorMapping.get("10004"));
}
private String getMobile(String accessToken) {
......
......@@ -190,7 +190,8 @@ public class AdamUserController {
@RequestParam String code) {
log.debug("mobile:{},code:{}", mobile, code);
if (!this.checkSmsCode(mobile, code)) return ResponseDto.failure(ErrorMapping.get("10004"));
ResponseDto checkSmsCodeDto = this.checkSmsCode(mobile, code);
if (!checkSmsCodeDto.isSuccess()) return checkSmsCodeDto;
return ResponseDto.success(adamUserInfoService.editMobile(CurrentUtil.getCurrentUid(), mobile));
}
......@@ -241,10 +242,6 @@ public class AdamUserController {
public ResponseDto<List<AdamThirdPartInfoVo>> bindTpa(@Valid @RequestBody AdamThirdPartParam parameter) {
log.debug("login by tpa:{}", JsonUtils.toJson(parameter));
if (StringUtils.isBlank(parameter.getOpenId())) {
return ResponseDto.failure(ErrorMapping.get("10009"));
}
String currentUid = CurrentUtil.getCurrentUid();
String existUid = adamRdmService.getUidByPlatformOpenId(parameter.getPlatform(), parameter.getOpenId());
......@@ -307,27 +304,32 @@ public class AdamUserController {
private static final String PHP_API_SMS_CODE_VALID = "/smsValidation";
private boolean checkSmsCode(String mobile, String code) {
private ResponseDto checkSmsCode(String mobile, String code) {
if (Arrays.asList(LnsEnum.ENV.dev.name(), LnsEnum.ENV.test.name()).contains(env.getProperty(CurrentUtil.CK_ENV_ACTIVE))
&& CurrentUtil.GRAY_LOGIN_SMS_CODE.equals(code)) {
return true;
return ResponseDto.success();
}
LinkedMultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
paramsMap.add("mobile", mobile);
paramsMap.add("code", code);
LinkedMultiValueMap<String, String> headersMap = new LinkedMultiValueMap<>();
headersMap.add("token", null);
try {
String respStr = HttpUtil.get(env.getProperty("liquidnet.url-service.url") + PHP_API_SMS_CODE_VALID, paramsMap, headersMap);
log.debug("###PHP.API.RESP:{}", respStr);
Map respMap = JsonUtils.fromJson(respStr, Map.class);
return StringUtils.equalsIgnoreCase("OK", (String) respMap.get("message"));
} catch (Exception e) {
log.error("验证码验证异常[mobile:{},code:{}]", mobile, code, e);
return false;
// LinkedMultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
// paramsMap.add("mobile", mobile);
// paramsMap.add("code", code);
// LinkedMultiValueMap<String, String> headersMap = new LinkedMultiValueMap<>();
// headersMap.add("token", null);
// try {
// String respStr = HttpUtil.get(env.getProperty("liquidnet.url-service.url") + PHP_API_SMS_CODE_VALID, paramsMap, headersMap);
// log.debug("###PHP.API.RESP:{}", respStr);
//
// Map respMap = JsonUtils.fromJson(respStr, Map.class);
//
// return StringUtils.equalsIgnoreCase("OK", (String) respMap.get("message"));
// } catch (Exception e) {
// log.error("验证码验证异常[mobile:{},code:{}]", mobile, code, e);
// return false;
// }
String smsCodeByMobile = adamRdmService.getSmsCodeByMobile(mobile);
if (null == smsCodeByMobile) {
return ResponseDto.failure(ErrorMapping.get("10003"));
}
return smsCodeByMobile.equals(code) ? ResponseDto.success() : ResponseDto.failure(ErrorMapping.get("10004"));
}
}
......@@ -35,6 +35,16 @@ public class AdamRdmService {
return null == i ? 1 : i;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | <Mobile, SMS CODE> */
public boolean setSmsCodeByMobile(String mobile, String smsCode) {
return redisUtil.set(AdamRedisConst.VALID_SMS_CODE_MOBILE + mobile, smsCode, 15 * 60);
}
public String getSmsCodeByMobile(String mobile) {
return (String) redisUtil.get(AdamRedisConst.VALID_SMS_CODE_MOBILE + mobile);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | <Mobile|OPENID, UID> */
public boolean setUidByMobile(String mobile, String uid) {
......
......@@ -5,8 +5,8 @@
10000=请求频繁,稍后再试
10001=
10002=
10003=验证码发送失败
10002=验证码发送失败
10003=已超时,请重新获取验证码
10004=手机验证码错误
10005=手机号获取失败,请更换登录方式
10006=第三方账号未注册
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment