|
@@ -19,9 +19,14 @@ import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
import javax.annotation.PostConstruct;
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
+
|
|
@Component
|
|
@Component
|
|
public class JwtUtil {
|
|
public class JwtUtil {
|
|
public static final String TOKEN_HEADER = "Authorization";
|
|
public static final String TOKEN_HEADER = "Authorization";
|
|
@@ -78,6 +83,36 @@ public class JwtUtil {
|
|
return returnToken;
|
|
return returnToken;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 注销用户,退出登录
|
|
|
|
+ * 将token置空,在校验的时候会处理没有token的情况
|
|
|
|
+ *
|
|
|
|
+ * @param token
|
|
|
|
+ */
|
|
|
|
+ public static void logOff(String token) {
|
|
|
|
+ Map<String, Object> map = getDetails(token);
|
|
|
|
+ Long user_id = (Long) map.get("id");
|
|
|
|
+ String type = (String) map.get("type");
|
|
|
|
+ QueryWrapper<LoginRecord> qw = new QueryWrapper<>();
|
|
|
|
+ qw.eq("user_id", user_id);
|
|
|
|
+ qw.eq("type", type);
|
|
|
|
+ LoginRecord record = jwtUtil.lrs.getOne(qw);
|
|
|
|
+ // 没找到数据不需要处理
|
|
|
|
+ if (null == record) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ // token不一致,不需要处理
|
|
|
|
+ String dbToken = record.getToken();
|
|
|
|
+ if (null==dbToken || !dbToken.equals(token)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ // token置空
|
|
|
|
+ LoginRecord lr = new LoginRecord();
|
|
|
|
+ lr.setToken(null);
|
|
|
|
+ lr.setId(record.getId());
|
|
|
|
+ jwtUtil.lrs.updateById(lr);
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 创建登录数据
|
|
* 创建登录数据
|
|
*
|
|
*
|
|
@@ -89,30 +124,31 @@ public class JwtUtil {
|
|
String type = (String) map.get("type");
|
|
String type = (String) map.get("type");
|
|
// 第一次登录是创建,以后都是更新, 每个用户都有1个登录数据,但是能否使用要看过期时间
|
|
// 第一次登录是创建,以后都是更新, 每个用户都有1个登录数据,但是能否使用要看过期时间
|
|
QueryWrapper<LoginRecord> qw = new QueryWrapper<>();
|
|
QueryWrapper<LoginRecord> qw = new QueryWrapper<>();
|
|
- // qw.eq("user_id", user_id);
|
|
|
|
- // qw.eq("type", type);
|
|
|
|
- Map<String,Object> qm = new HashMap<>();
|
|
|
|
- qm.put("user_id",user_id);
|
|
|
|
- qm.put("type",type);
|
|
|
|
- qw.allEq(qm);
|
|
|
|
|
|
+ qw.eq("user_id", user_id);
|
|
|
|
+ qw.eq("type", type);
|
|
LoginRecord histroy = jwtUtil.lrs.getOne(qw);
|
|
LoginRecord histroy = jwtUtil.lrs.getOne(qw);
|
|
- LocalDateTime last_login_time = LocalDateTime.now();
|
|
|
|
|
|
+ // 准备数据
|
|
|
|
+ LocalDateTime last_time = LocalDateTime.now();
|
|
LocalDateTime expire_time = LocalDateTime.now().plusMinutes(EXPIRE_MIN);
|
|
LocalDateTime expire_time = LocalDateTime.now().plusMinutes(EXPIRE_MIN);
|
|
|
|
+ HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
|
|
|
+ String ip = request.getRemoteAddr();
|
|
if (null == histroy) {
|
|
if (null == histroy) {
|
|
// 创建数据
|
|
// 创建数据
|
|
LoginRecord lr = new LoginRecord();
|
|
LoginRecord lr = new LoginRecord();
|
|
lr.setExpire_time(expire_time);
|
|
lr.setExpire_time(expire_time);
|
|
- lr.setLast_login_time(last_login_time);
|
|
|
|
|
|
+ lr.setLast_time(last_time);
|
|
lr.setToken(token);
|
|
lr.setToken(token);
|
|
lr.setType(type);
|
|
lr.setType(type);
|
|
lr.setUser_id(user_id);
|
|
lr.setUser_id(user_id);
|
|
|
|
+ lr.setLast_ip(ip);
|
|
jwtUtil.lrs.save(lr);
|
|
jwtUtil.lrs.save(lr);
|
|
} else {
|
|
} else {
|
|
// 修改数据
|
|
// 修改数据
|
|
LoginRecord lr = new LoginRecord();
|
|
LoginRecord lr = new LoginRecord();
|
|
lr.setExpire_time(expire_time);
|
|
lr.setExpire_time(expire_time);
|
|
- lr.setLast_login_time(last_login_time);
|
|
|
|
|
|
+ lr.setLast_time(last_time);
|
|
lr.setToken(token);
|
|
lr.setToken(token);
|
|
|
|
+ lr.setLast_ip(ip);
|
|
lr.setId(histroy.getId());
|
|
lr.setId(histroy.getId());
|
|
jwtUtil.lrs.updateById(lr);
|
|
jwtUtil.lrs.updateById(lr);
|
|
}
|
|
}
|
|
@@ -127,18 +163,24 @@ public class JwtUtil {
|
|
Map<String, Object> map = getDetails(token);
|
|
Map<String, Object> map = getDetails(token);
|
|
Long user_id = (Long) map.get("id");
|
|
Long user_id = (Long) map.get("id");
|
|
String type = (String) map.get("type");
|
|
String type = (String) map.get("type");
|
|
- LocalDateTime expire_time = LocalDateTime.now().plusMinutes(EXPIRE_MIN);
|
|
|
|
QueryWrapper<LoginRecord> qw = new QueryWrapper<>();
|
|
QueryWrapper<LoginRecord> qw = new QueryWrapper<>();
|
|
qw.eq("user_id", user_id).eq("type", type);
|
|
qw.eq("user_id", user_id).eq("type", type);
|
|
LoginRecord histroy = jwtUtil.lrs.getOne(qw);
|
|
LoginRecord histroy = jwtUtil.lrs.getOne(qw);
|
|
|
|
+ // 准备数据
|
|
|
|
+ LocalDateTime expire_time = LocalDateTime.now().plusMinutes(EXPIRE_MIN);
|
|
|
|
+ LocalDateTime last_time = LocalDateTime.now();
|
|
|
|
+ HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
|
|
|
+ String ip = request.getRemoteAddr();
|
|
if (null == histroy) {
|
|
if (null == histroy) {
|
|
// 没有数据,转至创建
|
|
// 没有数据,转至创建
|
|
createLoginRecord(token);
|
|
createLoginRecord(token);
|
|
} else {
|
|
} else {
|
|
- // 修改过期时间
|
|
|
|
|
|
+ // 修改过期时间,使用时间,使用ip
|
|
LoginRecord lr = new LoginRecord();
|
|
LoginRecord lr = new LoginRecord();
|
|
lr.setExpire_time(expire_time);
|
|
lr.setExpire_time(expire_time);
|
|
lr.setId(histroy.getId());
|
|
lr.setId(histroy.getId());
|
|
|
|
+ lr.setLast_ip(ip);
|
|
|
|
+ lr.setLast_time(last_time);
|
|
jwtUtil.lrs.updateById(lr);
|
|
jwtUtil.lrs.updateById(lr);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -166,12 +208,22 @@ public class JwtUtil {
|
|
if (null == histroy) {
|
|
if (null == histroy) {
|
|
throw new CustomizationException(ExceptionEnum.NO_LOGIN_RECORD);
|
|
throw new CustomizationException(ExceptionEnum.NO_LOGIN_RECORD);
|
|
}
|
|
}
|
|
|
|
+ // 获取数据库的token
|
|
|
|
+ String dbToken = histroy.getToken();
|
|
|
|
+ if (null == dbToken) {
|
|
|
|
+ throw new CustomizationException(ExceptionEnum.ACCOUNT_IS_LOGOUT);
|
|
|
|
+ }
|
|
// 取出过期时间,和当前时间进行比较
|
|
// 取出过期时间,和当前时间进行比较
|
|
LocalDateTime nowTime = LocalDateTime.now();
|
|
LocalDateTime nowTime = LocalDateTime.now();
|
|
- boolean is_before = histroy.getExpire_time().isBefore(nowTime);
|
|
|
|
- if (!is_before) {
|
|
|
|
|
|
+ boolean is_after = histroy.getExpire_time().isAfter(nowTime);
|
|
|
|
+ if (!is_after) {
|
|
throw new CustomizationException(ExceptionEnum.TOKEN_INVALID);
|
|
throw new CustomizationException(ExceptionEnum.TOKEN_INVALID);
|
|
}
|
|
}
|
|
|
|
+ // token对比,如果时间允许,但是token码不一致,则说明在其他地点登录
|
|
|
|
+ if (!dbToken.equals(token)) {
|
|
|
|
+ throw new CustomizationException(ExceptionEnum.TOKEN_ERROR);
|
|
|
|
+ }
|
|
|
|
+ // 如果需要校准 ip,再加上即可
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -199,4 +251,22 @@ public class JwtUtil {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 根据请求头获取token
|
|
|
|
+ *
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String getToken() {
|
|
|
|
+ HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
|
|
|
+ String tokenHeader = request.getHeader(TOKEN_HEADER);
|
|
|
|
+ if (tokenHeader == null) {
|
|
|
|
+ throw new CustomizationException(ExceptionEnum.TOKEN_NOT_FOUND);
|
|
|
|
+ }
|
|
|
|
+ String token = tokenHeader.replace(JwtUtil.TOKEN_PREFIX, "");
|
|
|
|
+ if (token == null) {
|
|
|
|
+ throw new CustomizationException(ExceptionEnum.TOKEN_NOT_FOUND);
|
|
|
|
+ }
|
|
|
|
+ return token;
|
|
|
|
+ }
|
|
}
|
|
}
|