修复登录
This commit is contained in:
1 parent
e0cab6deb8
commit
20a1ff40d2
3 files changed
+30
-22
No files matched your search
@@ -14,8 +14,9 @@
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-jwt</artifactId>
|
||||
<groupId>com.auth0</groupId>
|
||||
<artifactId>java-jwt</artifactId>
|
||||
<version>4.4.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 幂等插件使用 -->
|
||||
|
||||
+19
-12
@@ -1,7 +1,9 @@
|
||||
package com.bsd.pjcloud.integrated.config;
|
||||
|
||||
import cn.hutool.jwt.JWT;
|
||||
import cn.hutool.jwt.JWTUtil;
|
||||
import com.auth0.jwt.JWT;
|
||||
import com.auth0.jwt.algorithms.Algorithm;
|
||||
import com.auth0.jwt.interfaces.DecodedJWT;
|
||||
import com.auth0.jwt.exceptions.JWTVerificationException;
|
||||
import com.bsd.pjcloud.integrated.service.VisitorsService;
|
||||
import com.bsd.pjcloud.integrated.service.VisitorUserService;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
@@ -32,34 +34,39 @@ public class VisitorTokenInterceptor implements HandlerInterceptor {
|
||||
response.getWriter().write("Unauthorized: token missing");
|
||||
return false;
|
||||
}
|
||||
JWT jwt = JWTUtil.parseToken(token);
|
||||
boolean verify = jwt.setKey(secret.getBytes()).verify();
|
||||
if (!verify) {
|
||||
DecodedJWT jwt = null;
|
||||
try {
|
||||
jwt = JWT.require(Algorithm.HMAC256(secret)).build().verify(token);
|
||||
} catch (JWTVerificationException e) {
|
||||
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
||||
response.getWriter().write("Unauthorized: token invalid");
|
||||
return false;
|
||||
}
|
||||
// 校验过期时间
|
||||
Object expObj = jwt.getPayload("exp");
|
||||
if (expObj == null || Long.parseLong(expObj.toString()) < System.currentTimeMillis()) {
|
||||
Long exp = null;
|
||||
try {
|
||||
exp = jwt.getClaim("exp").asLong();
|
||||
} catch (Exception e) {
|
||||
exp = null;
|
||||
}
|
||||
if (exp == null || exp < System.currentTimeMillis()) {
|
||||
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
||||
response.getWriter().write("Unauthorized: token expired");
|
||||
return false;
|
||||
}
|
||||
Object userType = jwt.getPayload("userType");
|
||||
if (userType == null || !"visitor".equals(userType.toString())) {
|
||||
String userType = jwt.getClaim("userType").asString();
|
||||
if (userType == null || !"visitor".equals(userType)) {
|
||||
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
||||
response.getWriter().write("Unauthorized: not visitor");
|
||||
return false;
|
||||
}
|
||||
// 账号状态校验
|
||||
Object idObj = jwt.getPayload("id");
|
||||
if (idObj == null) {
|
||||
Long userId = jwt.getClaim("id").asLong();
|
||||
if (userId == null) {
|
||||
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
||||
response.getWriter().write("Unauthorized: no user id");
|
||||
return false;
|
||||
}
|
||||
Long userId = Long.valueOf(idObj.toString());
|
||||
com.bsd.pjcloud.integrated.entity.VisitorUserEntity user = visitorUserService.getById(userId);
|
||||
if (user == null || "1".equals(user.getStatus())) {
|
||||
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
||||
|
||||
+8
-8
@@ -1,7 +1,8 @@
|
||||
package com.bsd.pjcloud.integrated.controller;
|
||||
|
||||
|
||||
import cn.hutool.jwt.JWT;
|
||||
import com.auth0.jwt.JWT;
|
||||
import com.auth0.jwt.algorithms.Algorithm;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.bsd.pjcloud.common.core.constant.CacheConstants;
|
||||
import com.bsd.pjcloud.common.core.util.R;
|
||||
@@ -88,13 +89,12 @@ public class AppVisitorUserController {
|
||||
}
|
||||
|
||||
String token = JWT.create()
|
||||
.setPayload("id", account.getId())
|
||||
.setPayload("phone", account.getPhone())
|
||||
.setPayload("userType", "visitor")
|
||||
.setPayload("realName", account.getRealName())
|
||||
.setPayload("exp", System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 7)
|
||||
.setKey(secret.getBytes())
|
||||
.sign();
|
||||
.withClaim("id", account.getId())
|
||||
.withClaim("phone", account.getPhone())
|
||||
.withClaim("userType", "visitor")
|
||||
.withClaim("realName", account.getRealName())
|
||||
.withClaim("exp", System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 7)
|
||||
.sign(Algorithm.HMAC256(secret));
|
||||
|
||||
response.put("code", 200);
|
||||
response.put("message", "登录成功");
|
||||
|
||||
Reference in new issue
Block a user