fix
This commit is contained in:
parent
6a135421f5
commit
4be29b3d85
@ -60,8 +60,8 @@ CREATE TABLE IF NOT EXISTS user_token (
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '主键,自增',
|
||||
user_id BIGINT NOT NULL COMMENT '用户ID,业务逻辑关联user表',
|
||||
token VARCHAR(128) NOT NULL UNIQUE COMMENT '用户登录token',
|
||||
login_at BIGINT NOT NULL COMMENT '登录时间(毫秒时间戳)',
|
||||
expired_at BIGINT NOT NULL COMMENT '过期时间(毫秒时间戳)',
|
||||
login_at DATETIME NOT NULL COMMENT '登录时间',
|
||||
expired_at DATETIME NOT NULL COMMENT '过期时间',
|
||||
add_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '添加时间',
|
||||
update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
|
||||
INDEX idx_user_id(user_id),
|
||||
|
||||
@ -1,11 +1,12 @@
|
||||
package com.knowledge.base.application.filter;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.knowledge.base.domain.user.service.UserService;
|
||||
import com.knowledge.base.infrastructure.north.controller.UserController;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
@ -20,6 +21,8 @@ public class AuthFilter implements Filter {
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
private static final String COOKIE_KEY = "auth_token";
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
|
||||
throws IOException, ServletException {
|
||||
@ -32,7 +35,20 @@ public class AuthFilter implements Filter {
|
||||
// 需要鉴权的路径前缀
|
||||
if (path.startsWith("/search")) {
|
||||
String token = request.getHeader("Authorization");
|
||||
if (!userService.isValidToken(token)) {
|
||||
|
||||
if (StrUtil.isBlank(token)) {
|
||||
Cookie[] cookies = request.getCookies();
|
||||
if (cookies != null) {
|
||||
for (Cookie cookie : cookies) {
|
||||
if (COOKIE_KEY.equals(cookie.getName())) {
|
||||
token = cookie.getValue();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (token == null || !userService.isValidToken(token)) {
|
||||
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
||||
response.getWriter().write("Invalid or expired token");
|
||||
return;
|
||||
@ -42,4 +58,3 @@ public class AuthFilter implements Filter {
|
||||
chain.doFilter(req, res);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -16,9 +16,11 @@ public class UserToken {
|
||||
|
||||
private String token;
|
||||
|
||||
private Long loginAt;
|
||||
@TableField("login_at")
|
||||
private LocalDateTime loginAt;
|
||||
|
||||
private Long expiredAt;
|
||||
@TableField("expired_at")
|
||||
private LocalDateTime expiredAt;
|
||||
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
private LocalDateTime addTime;
|
||||
|
||||
@ -9,6 +9,7 @@ import com.knowledge.base.infrastructure.util.crypto.SM3Util;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@ -44,12 +45,12 @@ public class UserServiceImpl implements UserService {
|
||||
@Override
|
||||
public UserToken createToken(Long userId, long expireMs) {
|
||||
String token = UUID.randomUUID().toString().replace("-", "");
|
||||
long now = System.currentTimeMillis();
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
UserToken userToken = new UserToken();
|
||||
userToken.setUserId(userId);
|
||||
userToken.setToken(token);
|
||||
userToken.setLoginAt(now);
|
||||
userToken.setExpiredAt(now + expireMs);
|
||||
userToken.setExpiredAt(now.plusNanos(expireMs * 1_000_000));
|
||||
tokenRepository.save(userToken);
|
||||
return userToken;
|
||||
}
|
||||
@ -67,7 +68,7 @@ public class UserServiceImpl implements UserService {
|
||||
@Override
|
||||
public boolean isValidToken(String token) {
|
||||
return tokenRepository.findByToken(token)
|
||||
.filter(t -> System.currentTimeMillis() < t.getExpiredAt())
|
||||
.filter(t -> LocalDateTime.now().isBefore(t.getExpiredAt()))
|
||||
.isPresent();
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user