| | |
| | | import com.ruoyi.common.constant.Constants; |
| | | import com.ruoyi.common.core.domain.model.LoginUserApplet; |
| | | import com.ruoyi.common.core.redis.RedisCache; |
| | | import com.ruoyi.system.model.TAppUser; |
| | | import com.ruoyi.system.service.TAppUserService; |
| | | import io.jsonwebtoken.Claims; |
| | | import io.jsonwebtoken.Jwts; |
| | | import lombok.extern.slf4j.Slf4j; |
| | |
| | | @Slf4j |
| | | public class WebSocketAuthHandler { |
| | | |
| | | @Autowired |
| | | private TAppUserService appUserService; |
| | | |
| | | @Autowired |
| | | private RedisCache redisCache; |
| | |
| | | @Value("${token.secret}") |
| | | private String secret; |
| | | |
| | | /** |
| | | * 验证用户认证信息 |
| | | * @param appUserId 用户ID |
| | | * @param token 认证令牌(可选) |
| | | * @return 认证结果 |
| | | */ |
| | | public AuthResult authenticateUser(String appUserId, String token) { |
| | | try { |
| | | if (appUserId == null || appUserId.trim().isEmpty()) { |
| | | return AuthResult.failure("用户ID不能为空"); |
| | | } |
| | | |
| | | // 查询用户信息 |
| | | TAppUser appUser = appUserService.getById(appUserId); |
| | | if (appUser == null) { |
| | | return AuthResult.failure("用户不存在"); |
| | | } |
| | | |
| | | // 检查用户状态 |
| | | if (appUser.getStatus() == null || appUser.getStatus() != 1) { |
| | | return AuthResult.failure("用户已被禁用"); |
| | | } |
| | | |
| | | // 检查用户审核状态 |
| | | if (appUser.getState() == null || appUser.getState() != 1) { |
| | | return AuthResult.failure("用户未通过审核"); |
| | | } |
| | | |
| | | // 这里可以添加token验证逻辑 |
| | | if (token != null && !token.trim().isEmpty()) { |
| | | // 验证token的有效性 |
| | | if (!validateToken(appUserId, token)) { |
| | | return AuthResult.failure("认证令牌无效"); |
| | | } |
| | | } |
| | | |
| | | return AuthResult.success(appUser); |
| | | } catch (Exception e) { |
| | | log.error("用户认证失败,用户ID: {}, 错误: {}", appUserId, e.getMessage(), e); |
| | | return AuthResult.failure("认证过程中发生错误: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 验证token |
| | |
| | | return com.ruoyi.common.constant.CacheConstants.LOGIN_TOKEN_KEY + uuid; |
| | | } |
| | | |
| | | /** |
| | | * 认证结果类 |
| | | */ |
| | | public static class AuthResult { |
| | | private final boolean success; |
| | | private final String message; |
| | | private final TAppUser appUser; |
| | | |
| | | private AuthResult(boolean success, String message, TAppUser appUser) { |
| | | this.success = success; |
| | | this.message = message; |
| | | this.appUser = appUser; |
| | | } |
| | | |
| | | public static AuthResult success(TAppUser appUser) { |
| | | return new AuthResult(true, "认证成功", appUser); |
| | | } |
| | | |
| | | public static AuthResult failure(String message) { |
| | | return new AuthResult(false, message, null); |
| | | } |
| | | |
| | | public boolean isSuccess() { |
| | | return success; |
| | | } |
| | | |
| | | public String getMessage() { |
| | | return message; |
| | | } |
| | | |
| | | public TAppUser getAppUser() { |
| | | return appUser; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 从认证消息中提取用户信息 |