提交 188a5aca 编写于 作者: rushui.chen's avatar rushui.chen

20190827 获取短信验证码接口和注册接口

上级 99fd1988
流水线 #13714 已失败 于阶段
in 0 second
......@@ -22,20 +22,19 @@ public class RegisterController extends AccountBaseController {
@Autowired
private RegisterService registerService;
@ApiOperation("注册接口")
@PostMapping(value = "")
public PicaResponse<String> register(@RequestBody EncryptEntity entity) throws Exception {
BaseRequest request = CryptoUtil.decrypt(entity);
BaseRequest request = CryptoUtil.decrypt(entity, BaseRequest.class);
AccountUtils.checkMobilePhone(request.getMobile());
AccountUtils.getAuthCodeKey(request.getSysCode(), EnumsType.SYSCODE_TYPE_REGISTER.getCode() + "");
AccountUtils.checkPassword(request.getPassword());
request.setFlag(EnumsType.SYSCODE_TYPE_REGISTER.getCode());
request.setProductType(super.getProductType());
request.setSourceType(super.getSourceType());
PicaResponse picaResponse = registerService.register(request);
return picaResponse;
}
}
package com.pica.cloud.account.account.server.controller;
import com.pica.cloud.account.account.server.entity.AccountInfoEntity;
import com.pica.cloud.account.account.server.entity.EncryptEntity;
import com.pica.cloud.account.account.server.mapper.AccountInfoDetailMapper;
import com.pica.cloud.account.account.server.req.BaseRequest;
import com.pica.cloud.account.account.server.util.AccountUtils;
import com.pica.cloud.account.account.server.util.CryptoUtil;
import com.pica.cloud.foundation.entity.PicaResponse;
import com.pica.cloud.foundation.redis.ICacheClient;
import com.pica.cloud.foundation.utils.utils.CommonUtil;
import com.pica.cloud.foundation.utils.utils.EncryptCreateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Api("短信验证码资源")
@RestController
public class SysCodeController {
@RequestMapping(value = "/authCode")
public class SysCodeController extends AccountBaseController {
private final String AUTH_CODE_PREFIX = "authCode-";
@Autowired
private AccountInfoDetailMapper accountInfoDetailMapper;
@Autowired
@Qualifier("cacheMigrateClient")
private ICacheClient cacheClient;
@ApiOperation("获取短信验证码")
@PostMapping(value = "")
public PicaResponse getSysCode(@RequestBody EncryptEntity entity) throws Exception {
BaseRequest request = CryptoUtil.decrypt(entity);
request.setFlag(0);
AccountUtils.checkMobilePhone(request.getMobile());
processSysCode(request.getMobile(), request.getFlag());
return PicaResponse.toResponse();
}
/**
* 验证码发送逻辑
*
* @param mobilePhone
* @param flag
*/
private void processSysCode(String mobilePhone, Integer flag) {
//随机生成验证码
String authCode = CommonUtil.createValidateCode();
String message = "您的验证码是" + authCode + ",在10分钟内有效。如非本人操作,请忽略本短信!";
//判断账号是否已经存在
AccountInfoEntity accountInfoEntity = accountInfoDetailMapper.selectByMobile(mobilePhone);
long senderId = accountInfoEntity == null ? 0L : accountInfoEntity.getId();
//验证码保存到redis,失效时间10分钟
cacheClient.set(this.getAuthCodeKey(mobilePhone, flag + ""), authCode, 600);
//发送短信
super.sendMobileMessage(mobilePhone, message, senderId);
}
//获取验证码redis key
private String getAuthCodeKey(String mobilePhone, String flag) {
return AUTH_CODE_PREFIX + flag + "-" + EncryptCreateUtil.encrypt(mobilePhone);
}
}
......@@ -13,6 +13,8 @@ public class Account {
private Long id;
private Integer acctId;
private Integer sex;
private String name;
......@@ -75,6 +77,14 @@ public class Account {
this.id = id;
}
public Integer getAcctId() {
return acctId;
}
public void setAcctId(Integer acctId) {
this.acctId = acctId;
}
public Integer getSex() {
return sex;
}
......
......@@ -19,7 +19,7 @@ public class AccountInfoEntity {
private Date birthday;
private Byte sex;
private Integer sex;
private String idCard;
......@@ -99,11 +99,11 @@ public class AccountInfoEntity {
this.birthday = birthday;
}
public Byte getSex() {
public int getSex() {
return sex;
}
public void setSex(Byte sex) {
public void setSex(int sex) {
this.sex = sex;
}
......
......@@ -5,22 +5,22 @@ package com.pica.cloud.account.account.server.entity;
*/
public class EncryptEntity {
/**
* 加密后的密文
*/
private String key;
// /**
// * 加密后的密文
// */
// private String key;
/**
* 加密后的数据
*/
private String content;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
// public String getKey() {
// return key;
// }
//
// public void setKey(String key) {
// this.key = key;
// }
public String getContent() {
return content;
......@@ -30,11 +30,11 @@ public class EncryptEntity {
this.content = content;
}
@Override
public String toString() {
return "EncryptEntity{" +
"key='" + key + '\'' +
", content='" + content + '\'' +
'}';
}
// @Override
// public String toString() {
// return "EncryptEntity{" +
// "key='" + key + '\'' +
// ", content='" + content + '\'' +
// '}';
// }
}
......@@ -10,8 +10,27 @@ public interface AccountInfoDetailMapper {
int insertSelective(AccountInfoEntity record);
/**
* 通过电话号码查询账号信息
*
* @param mobile
* @return
*/
AccountInfoEntity selectByMobile(String mobile);
/**
* 插入注册人信息
*
* @param acctId
*/
void insertCreateInfo(int acctId);
AccountInfoEntity selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(AccountInfoEntity record);
int updateByPrimaryKey(AccountInfoEntity record);
......
......@@ -7,6 +7,7 @@ import io.swagger.annotations.ApiModelProperty;
public class BaseRequest {
private String mobile;
private String password;
private String sysCode;
private int flag;
@ApiModelProperty("产品线类型")
......@@ -14,6 +15,7 @@ public class BaseRequest {
@ApiModelProperty("渠道来源")
private int sourceType;
public String getMobile() {
return mobile;
}
......@@ -22,6 +24,14 @@ public class BaseRequest {
this.mobile = mobile;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSysCode() {
return sysCode;
}
......
package com.pica.cloud.account.account.server.service;
import com.alibaba.fastjson.JSONObject;
import com.pica.cloud.account.account.server.entity.Account;
import com.pica.cloud.account.account.server.entity.AccountInfoEntity;
import com.pica.cloud.account.account.server.entity.AccountUserInfoEntity;
import com.pica.cloud.account.account.server.enums.ExceptionType;
import com.pica.cloud.account.account.server.mapper.AccountInfoDetailMapper;
import com.pica.cloud.account.account.server.req.BaseRequest;
import com.pica.cloud.account.account.server.util.AccountUtils;
import com.pica.cloud.foundation.entity.PicaException;
import com.pica.cloud.foundation.entity.PicaResponse;
import com.pica.cloud.foundation.redis.ICacheClient;
import com.pica.cloud.foundation.utils.utils.EncryptCreateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.*;
@Service
public class RegisterServiceImpl implements RegisterService {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private AccountInfoDetailMapper accountInfoDetailMapper;
@Autowired
private UserInfoService userInfoService;
@Autowired
@Qualifier("cacheMigrateClient")
private ICacheClient cacheClient;
@Override
public PicaResponse register(BaseRequest baseRequest) {
return null;
String mobile = EncryptCreateUtil.encrypt(baseRequest.getMobile());
//校验用户是否已经注册
AccountInfoEntity accountInfoEntity = accountInfoDetailMapper.selectByMobile(mobile);
if (accountInfoEntity != null) {
AccountInfoEntity accountInfo = new AccountInfoEntity();
accountInfo.setMobilePhone(baseRequest.getMobile());
accountInfo.setPassword(baseRequest.getPassword());
accountInfo.setCreatedTime(new Date());
accountInfo.setCreatedId(0);
accountInfo.setModifiedId(0);
accountInfo.setModifiedTime(new Date());
accountInfo.setRegTime(new Date());
accountInfo.setDeleteFlag(1);
accountInfo.setRegisterProduct(baseRequest.getProductType());
accountInfo.setRegisterSource(baseRequest.getSourceType());
accountInfoDetailMapper.insertSelective(accountInfo);
//账户id
Integer acctId = accountInfo.getId();
accountInfoDetailMapper.insertCreateInfo(acctId);
AccountUserInfoEntity accountUserInfoEntity = new AccountUserInfoEntity();
accountUserInfoEntity.setAcctId(acctId);
accountUserInfoEntity.setDeleteFlag(1);
accountUserInfoEntity.setCreateId(acctId);
accountUserInfoEntity.setModifyId(acctId);
accountUserInfoEntity.setCreateTime(new Date());
accountUserInfoEntity.setModifyTime(new Date());
userInfoService.insertUserInfo(accountUserInfoEntity);
Integer userId = accountUserInfoEntity.getId();
Account account = new Account();
account.setId(userId.longValue());
account.setAcctId(acctId);
account.setCreatTime(new Date());
account.setMobilePhone(mobile);
account.setRegisterSource(baseRequest.getSourceType());
String newToken = this.generateToken(account);
JSONObject jsonObject = new JSONObject();
jsonObject.put("token", newToken);
jsonObject.put("userId", userId);
return PicaResponse.toResponse(jsonObject.toJSONString());
//用户信息表记录
} else {
throw new PicaException(ExceptionType.PICA_ALREADY_REGISTER.getCode(), ExceptionType.PICA_ALREADY_REGISTER.getMessage());
}
}
/**
* 生成新的token
*
* @param account
* @return
*/
private String generateToken(Account account) {
String sourceType = AccountUtils.getSourceType(account.getRegisterSource());
String newToken = org.apache.commons.lang3.StringUtils.EMPTY;
try {
String value = "token-doctor-" + account.getId().toString();
//生成新token
int expiredSeconds = 30 * 24 * 60 * 60;//token有效期30天
newToken = UUID.randomUUID().toString().replace("-", "").toUpperCase();
String Key = "token-" + newToken;
//存储token:(token-FF9FCB0D93A642328A01C279701B7607:token-doctor-1)
cacheClient.set(Key, value, expiredSeconds);
//存储token:(token-doctor-12345678-app:token-FF9FCB0D93A642328A01C279701B7607)
cacheClient.set(value + sourceType, Key, expiredSeconds);
//用户数据放入缓存
String userData = cacheClient.hget(value, "id");
if (org.apache.commons.lang3.StringUtils.isEmpty(userData)) {
Map<String, String> data = new HashMap<>();
data.put("token", newToken);
data.put("id", account.getId() + "");
data.put("acctId", account.getAcctId() + "");
data.put("mobile", account.getMobilePhone());
data.put("name", account.getMobilePhone().replaceAll("(\\d{3})\\d{4}(\\w{4})", "$1****$2"));
data.put("created_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(account.getCreatTime()));
data.put("sysCode", sourceType);
Iterator<Map.Entry<String, String>> iterator = data.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> map = iterator.next();
String key = map.getKey();
String valueInfo = map.getValue();
//存储token:(token-doctor-1:用户数据)
cacheClient.hset(value, key, valueInfo);
}
}
} catch (Exception ex) {
logger.error("生成token异常:{}" + ex.getMessage(), ex);
}
return newToken;
}
}
package com.pica.cloud.account.account.server.service;
import com.alibaba.fastjson.JSONObject;
import com.pica.cloud.account.account.server.entity.EncryptEntity;
import com.pica.cloud.account.account.server.req.BaseRequest;
public class Test {
public static void main(String[] args) {
// String json="{\"key\":\"密钥\",\"content\": {\"mobilePhone\" : \"1302412588\", \"flag\": \"1\"}}\n" +
// " }";
BaseRequest request = new BaseRequest();
request.setFlag(1);
request.setMobile("13024112588");
String string = JSONObject.toJSONString(request);
System.out.println(string);
EncryptEntity encryptEntity = new EncryptEntity();
encryptEntity.setContent(string);
System.out.println(JSONObject.toJSONString(encryptEntity));
}
}
package com.pica.cloud.account.account.server.service;
import com.pica.cloud.account.account.server.entity.AccountUserInfoEntity;
public interface UserInfoService {
AccountUserInfoEntity insertUserInfo(AccountUserInfoEntity accountUserInfoEntity);
}
package com.pica.cloud.account.account.server.service.impl;
import com.pica.cloud.account.account.server.entity.AccountUserInfoEntity;
import com.pica.cloud.account.account.server.service.UserInfoService;
import org.springframework.stereotype.Service;
@Service
public class UserInfoServerImpl implements UserInfoService {
@Override
public AccountUserInfoEntity insertUserInfo(AccountUserInfoEntity accountUserInfoEntity) {
return null;
}
}
package com.pica.cloud.account.account.server.util;
import com.alibaba.fastjson.JSONObject;
import com.pica.cloud.account.account.server.enums.ExceptionType;
import com.pica.cloud.foundation.entity.PicaException;
import com.pica.cloud.foundation.entity.PicaResultCode;
import com.pica.cloud.foundation.redis.ICacheClient;
......@@ -30,6 +31,13 @@ public class AccountUtils {
}
}
//手机格式校验
public static void checkPassword(String password) {
if (StringUtils.isBlank(password)) {
throw new PicaException(ExceptionType.PICA_NOT_EMPTY.getCode(), ExceptionType.PICA_NOT_EMPTY.getMessage());
}
}
//获取验证码redis key
public static String getAuthCodeKey(String mobilePhone, String flag) {
return AUTH_CODE_PREFIX + flag + "-" + EncryptCreateUtil.encrypt(mobilePhone);
......
......@@ -27,7 +27,8 @@ public class CryptoUtil {
String finalKey = RSAUtil.encrypt(keyString);
EncryptEntity encryptEntity = new EncryptEntity();
encryptEntity.setContent(content);
encryptEntity.setKey(finalKey);
// TODO: 2019/8/27 :获取验证码接口
// encryptEntity.setKey(finalKey);
return encryptEntity;
}
......@@ -54,13 +55,14 @@ public class CryptoUtil {
* @return
* @throws Exception
*/
public static BaseRequest decrypt(EncryptEntity encryptEntity) throws Exception {
public static BaseRequest decrypt(EncryptEntity encryptEntity,Class zClass) throws Exception {
// TODO: 2019/8/27 : 暂时不处理这一块的逻辑
//获取解密密钥
String decryptKey = RSAUtil.decrypt(encryptEntity.getKey());
//String decryptKey = RSAUtil.decrypt(encryptEntity.getKey());
//解密数据
String content = AESUtil.decrypt(decryptKey, encryptEntity.getContent());
//String content = AESUtil.decrypt(decryptKey, encryptEntity.getContent());
//反序列化成对象
BaseRequest request = (BaseRequest) JSONObject.toJSON(content);
BaseRequest request = (BaseRequest) JSONObject.parseObject(encryptEntity.getContent(),zClass);
return request;
}
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册