登录增加验证码切换

This commit is contained in:
2026-04-06 23:02:59 +08:00
parent dd1bda704f
commit fae01fdf43
4 changed files with 19 additions and 5 deletions

View File

@@ -25,6 +25,8 @@ public class User extends BaseEntity {
private Integer status;
private String role; // admin-管理员, user-普通用户
@TableField("storage_used")
private Long storageUsed;

View File

@@ -1,5 +1,7 @@
package com.filesystem.security;
import com.filesystem.entity.User;
import com.filesystem.mapper.UserMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
@@ -25,6 +27,9 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
@Resource
private JwtUtil jwtUtil;
@Resource
private UserMapper userMapper;
private static final List<AntPathRequestMatcher> EXCLUDE_MATCHERS = List.of(
new AntPathRequestMatcher("/api/auth/login"),
new AntPathRequestMatcher("/api/auth/register"),
@@ -71,7 +76,14 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
String username = jwtUtil.getUsernameFromToken(token);
Long userId = jwtUtil.getUserIdFromToken(token);
UserPrincipal principal = new UserPrincipal(userId, username);
// 查询用户角色
String role = "user";
User user = userMapper.selectById(userId);
if (user != null && "admin".equals(user.getRole())) {
role = "admin";
}
UserPrincipal principal = new UserPrincipal(userId, username, role);
UsernamePasswordAuthenticationToken authentication =
new UsernamePasswordAuthenticationToken(principal, null, new ArrayList<>());

View File

@@ -8,4 +8,5 @@ import lombok.Data;
public class UserPrincipal {
private Long userId;
private String username;
private String role; // admin-管理员, user-普通用户
}

View File

@@ -14,8 +14,7 @@ CREATE TABLE IF NOT EXISTS sys_user (
email VARCHAR(100) COMMENT '邮箱',
phone VARCHAR(20) COMMENT '手机号',
status INT DEFAULT 1 COMMENT '状态 0-禁用 1-启用',
storage_used BIGINT DEFAULT 0 COMMENT '已用存储空间(字节)',
storage_limit BIGINT DEFAULT 10737418240 COMMENT '存储限制(字节) 默认10GB',
role VARCHAR(20) DEFAULT 'user' COMMENT '角色 admin-管理员 user-普通用户',
register_ip VARCHAR(50) COMMENT '注册IP',
allowed_ips VARCHAR(500) COMMENT '允许的IP段逗号分隔',
create_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
@@ -75,6 +74,6 @@ CREATE TABLE IF NOT EXISTS sys_message (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='消息表';
-- 插入默认管理员账户 (密码: admin123)
INSERT INTO sys_user (username, password, nickname, status, storage_limit, allowed_ips)
VALUES ('admin', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iAt6Z5EH', '管理员', 1, 10737418240, '0.0.0.0/0')
INSERT INTO sys_user (username, password, nickname, status, storage_limit, role, allowed_ips)
VALUES ('admin', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iAt6Z5EH', '管理员', 1, 10737418240, 'admin', '0.0.0.0/0')
ON DUPLICATE KEY UPDATE username = username;