You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
4.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.service.impl;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.dao.TokenDao;
import com.entity.TokenEntity;
import com.service.TokenService;
import com.utils.CommonUtil;
import com.utils.PageUtils;
import com.utils.Query;
/**
* Token服务实现类
* 核心职责:管理用户认证令牌的生成、验证和过期处理
*
* 主要功能:
* 1. 生成用户登录令牌
* 2. 验证令牌有效性
* 3. 管理令牌生命周期
* 4. 支持令牌分页查询
*/
@Service("tokenService") // Spring服务注解指定bean名称为"tokenService"
public class TokenServiceImpl extends ServiceImpl<TokenDao, TokenEntity> implements TokenService {
/**
* 分页查询Token列表基础版本
* 用于管理系统查看所有令牌信息
* @param params 请求参数,包含分页参数
* @return 分页工具对象
*/
@Override
public PageUtils queryPage(Map<String, Object> params) {
Page<TokenEntity> page = this.selectPage(
new Query<TokenEntity>(params).getPage(), // 解析分页参数
new EntityWrapper<TokenEntity>() // 创建空的查询条件
);
return new PageUtils(page);
}
/**
* 查询Token列表
* 支持自定义查询条件
* @param wrapper 查询条件包装器
* @return Token实体列表
*/
@Override
public List<TokenEntity> selectListView(Wrapper<TokenEntity> wrapper) {
return baseMapper.selectListView(wrapper);
}
/**
* 分页查询Token列表增强版本
* 支持自定义查询条件和分页
* @param params 分页参数
* @param wrapper 查询条件包装器
* @return 分页工具对象
*/
@Override
public PageUtils queryPage(Map<String, Object> params, Wrapper<TokenEntity> wrapper) {
Page<TokenEntity> page = new Query<TokenEntity>(params).getPage();
page.setRecords(baseMapper.selectListView(page, wrapper));
PageUtils pageUtil = new PageUtils(page);
return pageUtil;
}
/**
* 生成用户认证令牌
* 核心业务方法:为用户生成唯一的登录令牌,支持令牌复用和过期管理
*
* @param userid 用户ID
* @param username 用户名
* @param tableName 用户表名yonghu、kuaidiyuan
* @param role 用户角色
* @return 生成的令牌字符串
*/
@Override
public String generateToken(Long userid, String username, String tableName, String role) {
// 查询该用户是否已有有效令牌基于用户ID和角色
TokenEntity tokenEntity = this.selectOne(
new EntityWrapper<TokenEntity>()
.eq("userid", userid)
.eq("role", role)
);
// 生成32位随机字符串作为令牌
String token = CommonUtil.getRandomString(32);
// 计算令牌过期时间(当前时间 + 1小时
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.HOUR_OF_DAY, 1); // 令牌有效期为1小时
if(tokenEntity != null) {
// 如果已存在令牌记录,则更新令牌和过期时间(令牌复用)
tokenEntity.setToken(token);
tokenEntity.setExpiratedtime(cal.getTime());
this.updateById(tokenEntity);
} else {
// 如果不存在令牌记录,则创建新的令牌记录
this.insert(new TokenEntity(userid, username, tableName, role, token, cal.getTime()));
}
return token;
}
/**
* 根据令牌字符串获取令牌实体
* 核心业务方法:验证令牌有效性并返回用户信息
*
* @param token 令牌字符串
* @return 有效的令牌实体如果令牌无效或过期则返回null
*/
@Override
public TokenEntity getTokenEntity(String token) {
// 根据令牌字符串查询令牌记录
TokenEntity tokenEntity = this.selectOne(
new EntityWrapper<TokenEntity>().eq("token", token)
);
// 验证令牌是否存在且未过期
if(tokenEntity == null || tokenEntity.getExpiratedtime().getTime() < new Date().getTime()) {
return null; // 令牌不存在或已过期
}
return tokenEntity;
}
}