添加登录&token校验逻辑
This commit is contained in:
parent
0db7d8370b
commit
1c8b29e249
@ -0,0 +1,40 @@
|
|||||||
|
package com.doc.parser.application.filter;
|
||||||
|
|
||||||
|
import com.doc.parser.infrastructure.north.controller.LoginController;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.servlet.*;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Luke.ye
|
||||||
|
* @date 2025/5/25 10:05
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class AuthFilter implements Filter {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
|
||||||
|
throws IOException, ServletException {
|
||||||
|
|
||||||
|
HttpServletRequest request = (HttpServletRequest) req;
|
||||||
|
HttpServletResponse response = (HttpServletResponse) res;
|
||||||
|
|
||||||
|
String path = request.getRequestURI();
|
||||||
|
|
||||||
|
// 需要鉴权的路径前缀
|
||||||
|
if (path.startsWith("/search")) {
|
||||||
|
String token = request.getHeader("Authorization");
|
||||||
|
if (!LoginController.isValidToken(token)) {
|
||||||
|
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
||||||
|
response.getWriter().write("Invalid or expired token");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
chain.doFilter(req, res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,66 @@
|
|||||||
|
package com.doc.parser.infrastructure.north.controller;
|
||||||
|
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Luke.ye
|
||||||
|
* @date 2025/5/25 10:02
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/user")
|
||||||
|
public class LoginController {
|
||||||
|
|
||||||
|
private static final Map<String, String> USER_DB = Map.of(
|
||||||
|
"admin", "123456",
|
||||||
|
"user", "abc123"
|
||||||
|
);
|
||||||
|
|
||||||
|
// token -> timestamp
|
||||||
|
private static final Map<String, Long> tokenStore = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
// token -> username
|
||||||
|
private static final Map<String, String> userMap = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
private static final long TOKEN_EXPIRE_MS = 24 * 60 * 60 * 1000L; // 24 小时
|
||||||
|
|
||||||
|
@PostMapping("/login")
|
||||||
|
public ResponseEntity<?> login(@RequestBody Map<String, String> body) {
|
||||||
|
String username = body.getOrDefault("username", "").trim();
|
||||||
|
String password = body.getOrDefault("password", "").trim();
|
||||||
|
|
||||||
|
if (!USER_DB.containsKey(username) || !USER_DB.get(username).equals(password)) {
|
||||||
|
return ResponseEntity.status(401).body(Map.of("message", "用户名或密码错误"));
|
||||||
|
}
|
||||||
|
|
||||||
|
String token = UUID.randomUUID().toString();
|
||||||
|
token = token.replace("-", "");
|
||||||
|
tokenStore.put(token, System.currentTimeMillis());
|
||||||
|
userMap.put(token, username);
|
||||||
|
|
||||||
|
return ResponseEntity.ok(Map.of(
|
||||||
|
"token", token,
|
||||||
|
"username", username
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isValidToken(String token) {
|
||||||
|
if (token == null || !tokenStore.containsKey(token)) return false;
|
||||||
|
long loginAt = tokenStore.get(token);
|
||||||
|
long now = System.currentTimeMillis();
|
||||||
|
if (now - loginAt > TOKEN_EXPIRE_MS) {
|
||||||
|
tokenStore.remove(token);
|
||||||
|
userMap.remove(token);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Optional<String> getUsername(String token) {
|
||||||
|
return Optional.ofNullable(userMap.get(token));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user