使用验证码登录
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package com.zscat.mallplus.enums;
|
package com.zscat.mallplus.enums;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author dimengzhe
|
* @Author dimengzhe
|
||||||
@@ -13,6 +14,9 @@ public class SmsEnum {
|
|||||||
*/
|
*/
|
||||||
public static final Long APP_LOGIN_CODE_TIME_LIMIT = 300L;
|
public static final Long APP_LOGIN_CODE_TIME_LIMIT = 300L;
|
||||||
|
|
||||||
|
@Value("${redis.key.prefix.loginCode}")
|
||||||
|
private static String REDIS_KEY_PREFIX_LOGIN_CODE;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送验证码
|
* 发送验证码
|
||||||
@@ -22,9 +26,10 @@ public class SmsEnum {
|
|||||||
/**
|
/**
|
||||||
* 登录
|
* 登录
|
||||||
*/
|
*/
|
||||||
LOGIN("1", "loginCode","登录"),
|
LOGIN("1", REDIS_KEY_PREFIX_LOGIN_CODE,"登录"),
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* code值
|
* code值
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import javax.xml.namespace.QName;
|
|||||||
*/
|
*/
|
||||||
public class SmsConfig {
|
public class SmsConfig {
|
||||||
|
|
||||||
static String msgSign="【汇融银链】";
|
static String msgSign="【汇融云链】";
|
||||||
public static String SendWaitWorkMsg(String mobile, String pwd,String msg) {
|
public static String SendWaitWorkMsg(String mobile, String pwd,String msg) {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
|||||||
@@ -65,6 +65,8 @@ import java.util.Map;
|
|||||||
@RequestMapping("/api/single/home")
|
@RequestMapping("/api/single/home")
|
||||||
public class SingelHomeController {
|
public class SingelHomeController {
|
||||||
|
|
||||||
|
@Value("${redis.key.prefix.loginCode}")
|
||||||
|
private String REDIS_KEY_PREFIX_LOGIN_CODE;
|
||||||
@Autowired
|
@Autowired
|
||||||
OssAliyunUtil aliyunOSSUtil;
|
OssAliyunUtil aliyunOSSUtil;
|
||||||
@Value("${jwt.tokenHeader}")
|
@Value("${jwt.tokenHeader}")
|
||||||
@@ -341,8 +343,7 @@ public class SingelHomeController {
|
|||||||
@ApiOperation(value = "手机号 密码登录")
|
@ApiOperation(value = "手机号 密码登录")
|
||||||
@PostMapping(value = "/login")
|
@PostMapping(value = "/login")
|
||||||
public Object login(@RequestParam("phone") String phone,
|
public Object login(@RequestParam("phone") String phone,
|
||||||
@RequestParam("password") String password,
|
@RequestParam("password") String password) {
|
||||||
@RequestParam(value = "type",required = false)String type) {
|
|
||||||
if (phone == null || "".equals(phone)) {
|
if (phone == null || "".equals(phone)) {
|
||||||
return new CommonResult().validateFailed("用户名或密码错误");
|
return new CommonResult().validateFailed("用户名或密码错误");
|
||||||
}
|
}
|
||||||
@@ -378,7 +379,6 @@ public class SingelHomeController {
|
|||||||
return new CommonResult().validateFailed("手机验证码为空");
|
return new CommonResult().validateFailed("手机验证码为空");
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
|
||||||
Map<String, Object> token = memberService.loginByCode(phone, authCode);
|
Map<String, Object> token = memberService.loginByCode(phone, authCode);
|
||||||
if (token.get("token") == null) {
|
if (token.get("token") == null) {
|
||||||
return new CommonResult().validateFailed("用户名或密码错误");
|
return new CommonResult().validateFailed("用户名或密码错误");
|
||||||
@@ -454,7 +454,8 @@ public class SingelHomeController {
|
|||||||
//缓存识别码
|
//缓存识别码
|
||||||
String redisKey = "";
|
String redisKey = "";
|
||||||
if(SmsEnum.SendEnum.LOGIN.getCode().equals(type)){
|
if(SmsEnum.SendEnum.LOGIN.getCode().equals(type)){
|
||||||
redisKey = SmsEnum.SendEnum.LOGIN.getRedisKey();
|
redisKey = REDIS_KEY_PREFIX_LOGIN_CODE;
|
||||||
|
// redisKey = SmsEnum.SendEnum.LOGIN.getRedisKey();
|
||||||
}
|
}
|
||||||
Date date = new Date();
|
Date date = new Date();
|
||||||
String codeRedis = redisService.get(redisKey + phone);
|
String codeRedis = redisService.get(redisKey + phone);
|
||||||
|
|||||||
@@ -33,14 +33,4 @@ public interface RedisService {
|
|||||||
*/
|
*/
|
||||||
Long increment(String key, long delta);
|
Long increment(String key, long delta);
|
||||||
|
|
||||||
/**
|
|
||||||
* 写入缓存设置时效时间
|
|
||||||
* @param key
|
|
||||||
* @param value
|
|
||||||
* @param expireTime
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
boolean set(final String key, Object value, Long expireTime);
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,17 +48,4 @@ public class RedisServiceImpl implements RedisService {
|
|||||||
return stringRedisTemplate.opsForValue().increment(key, delta);
|
return stringRedisTemplate.opsForValue().increment(key, delta);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean set(String key, Object value, Long expireTime) {
|
|
||||||
boolean result = false;
|
|
||||||
try {
|
|
||||||
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
|
|
||||||
operations.set(key, value);
|
|
||||||
stringRedisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
|
|
||||||
result = true;
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import com.zscat.mallplus.enums.SmsEnum;
|
|||||||
import com.zscat.mallplus.exception.ApiMallPlusException;
|
import com.zscat.mallplus.exception.ApiMallPlusException;
|
||||||
import com.zscat.mallplus.oms.mapper.OmsOrderMapper;
|
import com.zscat.mallplus.oms.mapper.OmsOrderMapper;
|
||||||
import com.zscat.mallplus.oms.vo.OrderStstic;
|
import com.zscat.mallplus.oms.vo.OrderStstic;
|
||||||
|
import com.zscat.mallplus.sys.entity.SysUserVo;
|
||||||
import com.zscat.mallplus.sys.mapper.SysAreaMapper;
|
import com.zscat.mallplus.sys.mapper.SysAreaMapper;
|
||||||
import com.zscat.mallplus.ums.entity.*;
|
import com.zscat.mallplus.ums.entity.*;
|
||||||
import com.zscat.mallplus.ums.mapper.SysAppletSetMapper;
|
import com.zscat.mallplus.ums.mapper.SysAppletSetMapper;
|
||||||
@@ -110,6 +111,8 @@ public class UmsMemberServiceImpl extends ServiceImpl<UmsMemberMapper, UmsMember
|
|||||||
private JwtTokenUtil jwtTokenUtil;
|
private JwtTokenUtil jwtTokenUtil;
|
||||||
@Value("${redis.key.prefix.authCode}")
|
@Value("${redis.key.prefix.authCode}")
|
||||||
private String REDIS_KEY_PREFIX_AUTH_CODE;
|
private String REDIS_KEY_PREFIX_AUTH_CODE;
|
||||||
|
@Value("${redis.key.prefix.loginCode}")
|
||||||
|
private String REDIS_KEY_PREFIX_LOGIN_CODE;
|
||||||
@Value("${authCode.expire.seconds}")
|
@Value("${authCode.expire.seconds}")
|
||||||
private Long AUTH_CODE_EXPIRE_SECONDS;
|
private Long AUTH_CODE_EXPIRE_SECONDS;
|
||||||
@Value("${jwt.tokenHead}")
|
@Value("${jwt.tokenHead}")
|
||||||
@@ -448,7 +451,8 @@ public class UmsMemberServiceImpl extends ServiceImpl<UmsMemberMapper, UmsMember
|
|||||||
String redisKey = "";
|
String redisKey = "";
|
||||||
if (SmsEnum.SendEnum.LOGIN.getCode().equals(type)) {
|
if (SmsEnum.SendEnum.LOGIN.getCode().equals(type)) {
|
||||||
content = "验证码:" + code + ",用于登录App,有效期5分钟,如非本人操作,请忽略该短信。";
|
content = "验证码:" + code + ",用于登录App,有效期5分钟,如非本人操作,请忽略该短信。";
|
||||||
redisKey = SmsEnum.SendEnum.LOGIN.getRedisKey();
|
// redisKey = SmsEnum.SendEnum.LOGIN.getRedisKey();
|
||||||
|
redisKey = REDIS_KEY_PREFIX_LOGIN_CODE;
|
||||||
}
|
}
|
||||||
String result = SmsConfig.SendWaitWorkMsg(phone, code, content);
|
String result = SmsConfig.SendWaitWorkMsg(phone, code, content);
|
||||||
if ("1".equals(result)) {
|
if ("1".equals(result)) {
|
||||||
@@ -457,7 +461,8 @@ public class UmsMemberServiceImpl extends ServiceImpl<UmsMemberMapper, UmsMember
|
|||||||
sms.setCode(sms.getCode());
|
sms.setCode(sms.getCode());
|
||||||
sms.setBizId(result);//返回的code
|
sms.setBizId(result);//返回的code
|
||||||
sms.setMessage(resultMsg);
|
sms.setMessage(resultMsg);
|
||||||
redisService.set(redisKey + phone, code + date.getTime(), SmsEnum.APP_LOGIN_CODE_TIME_LIMIT);
|
redisService.set(redisKey + phone, code + date.getTime());
|
||||||
|
redisService.expire(redisKey + phone, SmsEnum.APP_LOGIN_CODE_TIME_LIMIT);
|
||||||
log.info("发送短信结果:code:{},message:{}, 1,发送成功");
|
log.info("发送短信结果:code:{},message:{}, 1,发送成功");
|
||||||
}else{
|
}else{
|
||||||
//返回的信息
|
//返回的信息
|
||||||
@@ -1207,15 +1212,20 @@ public class UmsMemberServiceImpl extends ServiceImpl<UmsMemberMapper, UmsMember
|
|||||||
String token = null;
|
String token = null;
|
||||||
try {
|
try {
|
||||||
UserDetails userDetails = userDetailsService.loadUserByUsername(phone);
|
UserDetails userDetails = userDetailsService.loadUserByUsername(phone);
|
||||||
|
|
||||||
//验证验证码
|
//验证验证码
|
||||||
if (!verifyAuthCode(authCode, phone)) {
|
/*if (!verifyAuthCode(authCode, phone)) {
|
||||||
throw new ApiMallPlusException("验证码错误");
|
throw new ApiMallPlusException("验证码错误");
|
||||||
|
}*/
|
||||||
|
String codeFromRedis = redisService.get(REDIS_KEY_PREFIX_LOGIN_CODE+ phone);
|
||||||
|
if (StringUtils.isBlank(codeFromRedis)){
|
||||||
|
throw new ApiMallPlusException("短信验证码已失效,请重新发送");
|
||||||
|
}
|
||||||
|
if (!authCode.equals(codeFromRedis.substring(0, 4))) {
|
||||||
|
throw new ApiMallPlusException("短信验证码错误或已失效,请重新获取");
|
||||||
}
|
}
|
||||||
UmsMember member = this.getByUsername(phone);
|
UmsMember member = this.getByUsername(phone);
|
||||||
if (member == null || member.getId() == null) {
|
if (member == null || member.getId() == null) {
|
||||||
throw new ApiMallPlusException("用户不存在");
|
throw new ApiMallPlusException("用户不存在");
|
||||||
|
|
||||||
}
|
}
|
||||||
Authentication authentication = new UsernamePasswordAuthenticationToken(
|
Authentication authentication = new UsernamePasswordAuthenticationToken(
|
||||||
userDetails, null, userDetails.getAuthorities());
|
userDetails, null, userDetails.getAuthorities());
|
||||||
|
|||||||
@@ -3,11 +3,13 @@ spring.jackson.time-zone=GMT+8
|
|||||||
spring.profiles.active=test
|
spring.profiles.active=test
|
||||||
#spring.profiles.active=dev
|
#spring.profiles.active=dev
|
||||||
#===server start===
|
#===server start===
|
||||||
server.port=8083
|
#server.port=8083
|
||||||
|
server.port=8111
|
||||||
#===server end===
|
#===server end===
|
||||||
|
|
||||||
#===redis custom key start===
|
#===redis custom key start===
|
||||||
redis.key.prefix.authCode=portal:authCode:
|
redis.key.prefix.authCode=portal:authCode:
|
||||||
|
redis.key.prefix.loginCode=portal:loginCode:
|
||||||
redis.key.prefix.orderId=portal:orderId:
|
redis.key.prefix.orderId=portal:orderId:
|
||||||
authCode.expire.seconds=90
|
authCode.expire.seconds=90
|
||||||
#===redis custom key end===
|
#===redis custom key end===
|
||||||
@@ -27,8 +29,8 @@ jwt.tokenHead=Bearer
|
|||||||
# \u963F\u91CC\u4E91\u77ED\u4FE1\u914D\u7F6E
|
# \u963F\u91CC\u4E91\u77ED\u4FE1\u914D\u7F6E
|
||||||
aliyun.sms.accessKeyId=LTAI4FhoMuUqdigjDnFT1bY6
|
aliyun.sms.accessKeyId=LTAI4FhoMuUqdigjDnFT1bY6
|
||||||
aliyun.sms.accessKeySecret=JvE9FcILlHu5gClmAJr89ZkEv00l9s
|
aliyun.sms.accessKeySecret=JvE9FcILlHu5gClmAJr89ZkEv00l9s
|
||||||
aliyun.sms.sign.name=\u4EAC\u6021\u541B\u5546\u8D38
|
aliyun.sms.sign.name=YXT004911
|
||||||
aliyun.sms.template.code=SMS_164240312
|
aliyun.sms.template.code=yyundong@yuxintong
|
||||||
aliyun.sms.expire-minute=2
|
aliyun.sms.expire-minute=2
|
||||||
aliyun.sms.day-count=45
|
aliyun.sms.day-count=45
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user