Commit 476f1a11 authored by 廖旭伟's avatar 廖旭伟

手机验证码登录

parent a393bc9e
package com.mortals.xhx.base.framework.security; package com.mortals.xhx.base.framework.security;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder; import org.apache.tomcat.util.net.openssl.ciphers.Authentication;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
/** /**
...@@ -16,7 +16,7 @@ public class SecurityUtils { ...@@ -16,7 +16,7 @@ public class SecurityUtils {
* 获取Authentication * 获取Authentication
*/ */
public static Authentication getAuthentication() { public static Authentication getAuthentication() {
return SecurityContextHolder.getContext().getAuthentication(); return null;
} }
/** /**
......
...@@ -65,4 +65,21 @@ public interface CustomerService extends ICRUDService<CustomerEntity,Long>{ ...@@ -65,4 +65,21 @@ public interface CustomerService extends ICRUDService<CustomerEntity,Long>{
* @return * @return
*/ */
Map<String,Object> getCustomerCensus(); Map<String,Object> getCustomerCensus();
/**
* 发送手机验证码
* @param mobileNumber
* @throws AppException
*/
void sendSmsVerifyCode(String mobileNumber) throws AppException;
/**
* 手机验证码登录
* @param mobileNumber
* @param verifyCode
* @param loginIp
* @return
* @throws AppException
*/
CustomerEntity doSmsLogin(String mobileNumber, String verifyCode, String loginIp) throws AppException;
} }
\ No newline at end of file
package com.mortals.xhx.module.customer.service.impl; package com.mortals.xhx.module.customer.service.impl;
import cn.hutool.core.util.PhoneUtil; import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.exception.AppException; import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context; import com.mortals.framework.model.Context;
import com.mortals.framework.model.PageInfo; import com.mortals.framework.model.PageInfo;
import com.mortals.framework.model.Result; import com.mortals.framework.model.Result;
import com.mortals.framework.service.ICacheService;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl; import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.util.DateUtils; import com.mortals.framework.util.DateUtils;
import com.mortals.framework.util.HttpUtil;
import com.mortals.framework.util.SecurityUtil; import com.mortals.framework.util.SecurityUtil;
import com.mortals.framework.util.StringUtils; import com.mortals.framework.util.StringUtils;
import com.mortals.xhx.common.code.CustomerSatusEnum; import com.mortals.xhx.common.code.CustomerSatusEnum;
import com.mortals.xhx.common.code.UserStatus;
import com.mortals.xhx.module.customer.dao.CustomerDao; import com.mortals.xhx.module.customer.dao.CustomerDao;
import com.mortals.xhx.module.customer.model.CustomerEntity; import com.mortals.xhx.module.customer.model.CustomerEntity;
import com.mortals.xhx.module.customer.model.CustomerEntityExt; import com.mortals.xhx.module.customer.model.CustomerEntityExt;
...@@ -20,6 +23,8 @@ import com.mortals.xhx.module.customer.model.vo.CustomerDesignCensusVo; ...@@ -20,6 +23,8 @@ import com.mortals.xhx.module.customer.model.vo.CustomerDesignCensusVo;
import com.mortals.xhx.module.customer.service.CustomerService; import com.mortals.xhx.module.customer.service.CustomerService;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.*; import java.util.*;
...@@ -34,6 +39,19 @@ import java.util.*; ...@@ -34,6 +39,19 @@ import java.util.*;
@Service("customerService") @Service("customerService")
public class CustomerServiceImpl extends AbstractCRUDServiceImpl<CustomerDao, CustomerEntity, Long> implements CustomerService { public class CustomerServiceImpl extends AbstractCRUDServiceImpl<CustomerDao, CustomerEntity, Long> implements CustomerService {
@Value("${sms.apiUrl:http://sms.wx3.com.cn/api/index/index}")
private String smsApiUrl;
@Value("${sms.appid:ADsUXLrS81vZDU95}")
private String appid;
/** 短信模板ID**/
private static String SMS_TPYE = "30";
private static String SMS_VERIFY_CODE_KEY ="login:sms:verify:";
@Autowired
private ICacheService cacheService;
@Override @Override
protected void saveBefore(CustomerEntity entity, Context context) throws AppException { protected void saveBefore(CustomerEntity entity, Context context) throws AppException {
this.validData(entity, context); this.validData(entity, context);
...@@ -222,4 +240,100 @@ public class CustomerServiceImpl extends AbstractCRUDServiceImpl<CustomerDao, Cu ...@@ -222,4 +240,100 @@ public class CustomerServiceImpl extends AbstractCRUDServiceImpl<CustomerDao, Cu
result.put("videosCount",designCensusVo.getVideosCount()); result.put("videosCount",designCensusVo.getVideosCount());
return result; return result;
} }
@Override
public void sendSmsVerifyCode(String mobileNumber) throws AppException {
if(StringUtils.isEmpty(mobileNumber)){
throw new AppException("手机号不能为空");
}
if(!PhoneUtil.isPhone(mobileNumber)){
throw new AppException("手机号码格式不正确");
}
CustomerEntity customer = this.selectOne(new CustomerQuery().contactTelphone(mobileNumber));
if(customer==null){
throw new AppException("手机号码:" + mobileNumber + "没有注册用户");
}
try {
Map<String, String> params = new HashMap<>();
params.put("appid",appid);
params.put("phone",mobileNumber);
params.put("type",SMS_TPYE);
String[] json = new String[2];
String vCode = RandomUtil.randomNumbers(6);
json[0] = vCode;
json[1] = "1";
params.put("json",JSONObject.toJSON(json).toString());
String resp = HttpUtil.doPost(smsApiUrl,params);
JSONObject respJson = JSONObject.parseObject(resp);
if(respJson.getIntValue("code")==0){
throw new AppException("短信发送失败:" + respJson.getString("message"));
}
//有效期60秒
cacheService.setnx(SMS_VERIFY_CODE_KEY+mobileNumber,vCode,60);
}catch (Exception e){
log.error("短信发送异常",e);
throw new AppException("短信发送异常");
}
}
@Override
public CustomerEntity doSmsLogin(String mobileNumber, String verifyCode, String loginIp) throws AppException {
if(StringUtils.isEmpty(mobileNumber)){
throw new AppException("手机号不能为空");
}
if(StringUtils.isEmpty(verifyCode)){
throw new AppException("验证码不能为空");
}
if(!PhoneUtil.isPhone(mobileNumber)){
throw new AppException("手机号码格式不正确");
}
CustomerEntity customer = this.selectOne(new CustomerQuery().contactTelphone(mobileNumber));
if(customer==null){
throw new AppException("手机号码:" + mobileNumber + "没有注册用户");
}
String vCode = cacheService.get(SMS_VERIFY_CODE_KEY + mobileNumber);
if(StringUtils.isEmpty(vCode)){
throw new AppException("验证码已失效");
}
if(!verifyCode.equals(vCode)){
throw new AppException("验证码不正确");
}
CustomerEntity update = new CustomerEntity();
update.setId(customer.getId());
update.setLastLoginAddress(loginIp);
update.setLastLoginTime(new Date());
this.update(update);
return customer;
}
public static void main(String[] args){
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("appid","ADsUXLrS81vZDU95");
jsonObject.put("phone","18981841597");
jsonObject.put("type",SMS_TPYE);
Map<String, String> params = new HashMap<>();
params.put("appid","ADsUXLrS81vZDU95");
params.put("phone","18981841597");
params.put("type",SMS_TPYE);
String vCode = RandomUtil.randomNumbers(6);
params.put("json","[\"123456\",\"1\"]");
List<String> jj = new ArrayList<>();
jj.add("1111");
jj.add("2");
System.out.println(JSONObject.toJSON(jj).toString());
String[] json = new String[2];
json[0] = vCode;
json[1] = "1";
System.out.println(JSONObject.toJSON(json).toString());
params.put("json",JSONObject.toJSON(jj).toString());
// String resp = HttpUtil.doPost("http://sms.wx3.com.cn/api/index/index",params);
// System.out.println(resp);
}catch (Exception e){
}
}
} }
\ No newline at end of file
...@@ -317,9 +317,9 @@ public class CustomerController extends BaseCRUDJsonBodyMappingController<Custom ...@@ -317,9 +317,9 @@ public class CustomerController extends BaseCRUDJsonBodyMappingController<Custom
public void exportExcel(@RequestBody CustomerEntity query) { public void exportExcel(@RequestBody CustomerEntity query) {
Context context = this.getContext(); Context context = this.getContext();
String busiDesc = "导出" + this.getModuleDesc(); String busiDesc = "导出" + this.getModuleDesc();
ArrayList properties = new ArrayList();
try { try {
this.doExportBefore(context, query); this.doExportBefore(context, query,properties);
String name = StringUtils.trim(this.moduleDesc); String name = StringUtils.trim(this.moduleDesc);
if (StringUtils.isEmpty(name)) { if (StringUtils.isEmpty(name)) {
name = System.currentTimeMillis() + ""; name = System.currentTimeMillis() + "";
......
...@@ -17,7 +17,6 @@ import com.mortals.xhx.common.utils.StringUtils; ...@@ -17,7 +17,6 @@ import com.mortals.xhx.common.utils.StringUtils;
import com.mortals.xhx.module.picture.model.vo.PictureGroupCountVo; import com.mortals.xhx.module.picture.model.vo.PictureGroupCountVo;
import com.mortals.xhx.module.picture.service.PictureMaterialService; import com.mortals.xhx.module.picture.service.PictureMaterialService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
......
package com.mortals.xhx.module.thirdlog.sms;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.common.Rest;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.IAuthTokenService;
import com.mortals.framework.service.ICacheService;
import com.mortals.framework.util.DateUtils;
import com.mortals.framework.util.StringUtils;
import com.mortals.framework.web.BaseJsonBodyController;
import com.mortals.xhx.base.login.web.LoginForm;
import com.mortals.xhx.module.customer.model.CustomerEntity;
import com.mortals.xhx.module.customer.service.CustomerService;
import com.mortals.xhx.module.thirdlog.sms.pdu.SMSLoginPdu;
import org.springframework.beans.factory.annotation.Autowired;
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;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("sms")
public class SMSLoginController extends BaseJsonBodyController {
@Autowired
private CustomerService customerService;
@Autowired
private IAuthTokenService authTokenService;
@PostMapping({"send"})
@UnAuth
public Rest<Object> sendSmsVerifyCode(@RequestBody SMSLoginPdu smsLoginPdu){
Rest<Object> ret = new Rest();
Map<String, Object> model = new HashMap();
Context context = this.getContext();
String busiDesc = "发送手机验证码";
int code=1;
try {
customerService.sendSmsVerifyCode(smsLoginPdu.getMobileNumber());
model.put("message_info", busiDesc + "成功");
this.recordSysLog(this.request, busiDesc + " 【成功】");
} catch (Exception var9) {
code = -1;
this.doException(this.request, busiDesc, model, var9);
}
ret.setCode(code);
ret.setData(model);
ret.setDict(model.get("dict"));
ret.setMsg(model.get("message_info") == null ? "" : model.remove("message_info").toString());
return ret;
}
@PostMapping("login")
@UnAuth
public String login(@RequestBody SMSLoginPdu smsLoginPdu) throws Exception {
JSONObject ret = new JSONObject();
Map<String, Object> data = new HashMap<>();
String ip = super.getRequestIP(request);
CustomerEntity customerEntity = null;
try {
customerEntity = customerService.doSmsLogin(smsLoginPdu.getMobileNumber(), smsLoginPdu.getVerifyCode(), ip);
customerEntity.setLastLoginAddress(ip);
customerEntity.setLoginTime(System.currentTimeMillis());
customerEntity.setToken(IdUtil.fastSimpleUUID());
customerEntity.setExpireTime(DateUtils.addCurrDate(7).getTime());
String token = authTokenService.createToken(customerEntity);
data.put("token", token);
data.put("customer", customerEntity);
recordSysLog(request, customerEntity, "客户登录系统成功!");
ret.put(KEY_RESULT_DATA, data);
ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
ret.put(KEY_RESULT_MSG, "用户登录系统成功!");
return ret.toJSONString();
} catch (Exception e) {
log.error("login error ", e);
ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
ret.put(KEY_RESULT_MSG, super.convertException(e));
return ret.toJSONString();
}
}
}
package com.mortals.xhx.module.thirdlog.sms.pdu;
import lombok.Data;
@Data
public class SMSLoginPdu {
/** 手机号码 */
private String mobileNumber;
/** 验证码 */
private String verifyCode;
}
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
<parent> <parent>
<groupId>com.mortals</groupId> <groupId>com.mortals</groupId>
<artifactId>mortals-common</artifactId> <artifactId>mortals-common</artifactId>
<version>1.1.5-SNAPSHOT</version> <version>1.1.7-SNAPSHOT</version>
</parent> </parent>
<groupId>com.mortals.xhx</groupId> <groupId>com.mortals.xhx</groupId>
<artifactId>easy-affair-show</artifactId> <artifactId>easy-affair-show</artifactId>
...@@ -50,14 +50,14 @@ ...@@ -50,14 +50,14 @@
</dependency> </dependency>
<!-- 引入 Spring Cloud Alibaba Nacos Config 相关依赖,将 Nacos 作为配置中心,并实现对其的自动配置 --> <!-- 引入 Spring Cloud Alibaba Nacos Config 相关依赖,将 Nacos 作为配置中心,并实现对其的自动配置 -->
<dependency> <!-- <dependency>-->
<groupId>com.alibaba.cloud</groupId> <!-- <groupId>com.alibaba.cloud</groupId>-->
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <!-- <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>-->
</dependency> <!-- </dependency>-->
<dependency> <!-- <dependency>-->
<groupId>com.alibaba.cloud</groupId> <!-- <groupId>com.alibaba.cloud</groupId>-->
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <!-- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>-->
</dependency> <!-- </dependency>-->
</dependencies> </dependencies>
......
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