Commit 69a0a101 by ypenglv

冲入解决

2 parents 31b96803 1d435427
......@@ -15,12 +15,17 @@ import org.nafmii.common.utils.GuavaUtil;
import org.nafmii.common.utils.JwtUtils;
import org.nafmii.common.utils.SnowFlakeUtil;
import org.nafmii.common.constant.UserEnum;
import org.nafmii.common.vo.UserJwtVO;
import org.nafmii.enums.RedisEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* @author:zhoujh
......@@ -36,6 +41,12 @@ public class UserServiceImpl implements UserService {
@Resource
private AppUserMapper appUserMapper;
//redis缓存快递集合-key
public static final String USER_CODE_KEY="user:code";
@Autowired
StringRedisTemplate stringRedisTemplate;
/**
* 模拟用户登陆
* @return
......@@ -99,8 +110,9 @@ public class UserServiceImpl implements UserService {
log.info("用户注册参数==============>"+registerDto);
//将验证码放入到缓存当中(过期时间60s)test
// GuavaUtil.putTrackGuava(registerDto.getPhone(),registerDto.getCode());
stringRedisTemplate.opsForValue().set(new StringBuilder().append(USER_CODE_KEY).append(registerDto.getPhone()).toString(), registerDto.getCode(), RedisEnum.PHONECODE.getCode(), TimeUnit.SECONDS);
//重缓存中获取(通过手机号获取)
String code = (String) GuavaUtil.getTrackGuava(registerDto.getPhone());
String code = stringRedisTemplate.opsForValue().get(USER_CODE_KEY+registerDto.getPhone());
log.info("冲缓存中获取的验证码===============》{}",code);
if (null == code){
throw new BusinessException("验证码已过期");
......@@ -108,7 +120,6 @@ public class UserServiceImpl implements UserService {
if (!code.equals(registerDto.getCode())){
throw new BusinessException("验证码错误请重新输入");
}
int i = appUserMapper.insert(appUser);
if (i<0){
return new ApiResponse().fail("新增失败");
......@@ -133,7 +144,8 @@ public class UserServiceImpl implements UserService {
}
}else{
//验证码登陆
String code = (String) GuavaUtil.getTrackGuava(loginDto.getPhone());
String code = stringRedisTemplate.opsForValue().get(USER_CODE_KEY+loginDto.getPhone());
log.info("冲redis中获取的验证码===============》{}",code);
log.info("冲缓存中获取的验证码===============》{}",code);
if (null == code){
throw new BusinessException("验证码已过期");
......@@ -146,6 +158,11 @@ public class UserServiceImpl implements UserService {
throw new BusinessException("请先注册");
}
}
UserJwtVO vo = new UserJwtVO();
vo.setId(userVo.getId());
vo.setPhone(userVo.getPhone());
String token = JwtUtils.generateJsonWebToken(vo);
userVo.setAuthorization(token);
ApiResponse apiResponse =new ApiResponse();
apiResponse.setData(userVo);
return apiResponse;
......
......@@ -64,4 +64,6 @@ public class UserVO {
@ApiModelProperty(value = "是否有效标识 0 有效用户 1无效用户")
private String isEnable;
@ApiModelProperty(value = "token")
private String authorization;
}
......@@ -36,3 +36,19 @@ spring:
connection-timeout: 3000
pool-name: NFMII_HikariCP
connection-test-query: SELECT 1
redis:
# Redis数据库索引(默认为0)
database: 0
# Redis服务器地址
host: 127.0.0.1
port: 6379
# Redis服务器连接密码(默认为空)
password:
jedis:
pool:
# 连接池最大连接数(使用负值表示没有限制)
max-active: 20
max-wait: -1ms
max-idle: 10
min-idle: 0
timeout: 1000ms
......@@ -3,6 +3,7 @@ package org.nafmii.common.utils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.nafmii.common.vo.UserJwtVO;
import java.util.Date;
import java.util.HashMap;
......@@ -19,24 +20,24 @@ public class JwtUtils {
private static final String ROLE_CLAIMS = "rol";
// public static String generateJsonWebToken(AppUser user) {
// if (user.getId() == null || user.getUserName() == null ) {
// return null;
// }
//
// Map<String,Object> map = new HashMap<>();
// map.put(ROLE_CLAIMS, "rol");
// String token = Jwts
// .builder()
// .setSubject(SUBJECT)
// .setClaims(map)
// .claim("id", user.getId())
// .claim("name", user.getUserName())
// .setIssuedAt(new Date())
// .setExpiration(new Date(System.currentTimeMillis() + EXPIRITION))
// .signWith(SignatureAlgorithm.HS256, APPSECRET_KEY).compact();
// return token;
// }
public static String generateJsonWebToken(UserJwtVO user) {
if (user.getId() == null || user.getUserName() == null ) {
return null;
}
Map<String,Object> map = new HashMap<>();
map.put(ROLE_CLAIMS, "rol");
String token = Jwts
.builder()
.setSubject(SUBJECT)
.setClaims(map)
.claim("id", user.getId())
.claim("name", user.getUserName())
.setIssuedAt(new Date())
.setExpiration(new Date(System.currentTimeMillis() + EXPIRITION))
.signWith(SignatureAlgorithm.HS256, APPSECRET_KEY).compact();
return token;
}
/**
     * 生成token
......
package org.nafmii.common.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;
/**
* @author:zhoujh
* @Function:TODO
* @date 2021/4/25 10:22
* @ClassName:
* @version:
* @remark:
*/
@Data
@ApiModel("用户返回报文")
public class UserJwtVO {
private Long id;
@ApiModelProperty(value = "昵称")
private String userName;
@ApiModelProperty(value = "登录密码")
private String loginPwd;
@ApiModelProperty(value = "中文姓名")
private String realName;
@ApiModelProperty(value = "手机号/登录账号")
private String phone;
@ApiModelProperty(value = "出生日期")
private String birthday;
@ApiModelProperty(value = "邮箱")
private String email;
@ApiModelProperty(value = "头像图片地址")
private String headPicture;
@ApiModelProperty(value = "国籍")
private String nationality;
@ApiModelProperty(value = "单位全称")
private String unitName;
@ApiModelProperty(value = "微信OPENT_ID")
private String openId;
@ApiModelProperty(value = "是否开启指纹登录 0 未开启 1 已开启")
private String figerprintFalg;
@ApiModelProperty(value = "是否开启人脸识别登录 0 未开启 1 已开启")
private String faceidFalg;
@ApiModelProperty(value = "创建时间")
private Date createTime;
@ApiModelProperty(value = "更新时间")
private Date updateTime;
@ApiModelProperty(value = "是否有效标识 0 有效用户 1无效用户")
private String isEnable;
}
package org.nafmii.enums;
/**
* @author:zhoujh
* @Function:TODO
* @date 2021/4/27 13:48
* @ClassName:
* @version:
* @remark:
*/
public enum RedisEnum {
/**
* 验证码失效时间60s
*/
PHONECODE(60, "60秒后失效")
;
RedisEnum(Integer code, String name){
this.code = code;
this.name = name;
}
private String name;
private Integer code;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!