登录成功
This commit is contained in:
@@ -18,4 +18,35 @@
|
||||
<maven.compiler.target>21</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- Spring Web MVC -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Context -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Common Core dependency -->
|
||||
<dependency>
|
||||
<groupId>org.xyzh</groupId>
|
||||
<artifactId>common-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Common Util dependency -->
|
||||
<dependency>
|
||||
<groupId>org.xyzh</groupId>
|
||||
<artifactId>common-util</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Common Redis dependency -->
|
||||
<dependency>
|
||||
<groupId>org.xyzh</groupId>
|
||||
<artifactId>common-redis</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -1,7 +0,0 @@
|
||||
package org.xyzh;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello world!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.xyzh.common.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* @description HttpLogin.java文件描述 HTTP登录注解
|
||||
* @filename HttpLogin.java
|
||||
* @author yslg
|
||||
* @copyright xyzh
|
||||
* @since 2025-10-06
|
||||
*/
|
||||
@Target(ElementType.PARAMETER)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface HttpLogin {
|
||||
|
||||
/**
|
||||
* @description 是否必需,默认为true
|
||||
* @return boolean
|
||||
* @author yslg
|
||||
* @since 2025-10-06
|
||||
*/
|
||||
boolean required() default true;
|
||||
|
||||
/**
|
||||
* @description 当token无效时的错误消息
|
||||
* @return String
|
||||
* @author yslg
|
||||
* @since 2025-10-06
|
||||
*/
|
||||
String message() default "用户未登录或登录已过期";
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package org.xyzh.common.annotation.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.xyzh.common.annotation.resolver.HttpLoginArgumentResolver;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @description WebMvcConfig.java文件描述 Web MVC配置
|
||||
* @filename WebMvcConfig.java
|
||||
* @author yslg
|
||||
* @copyright xyzh
|
||||
* @since 2025-10-06
|
||||
*/
|
||||
@Configuration
|
||||
public class WebMvcConfig implements WebMvcConfigurer {
|
||||
|
||||
private final HttpLoginArgumentResolver httpLoginArgumentResolver;
|
||||
|
||||
/**
|
||||
* 使用构造器注入
|
||||
* 通过接口抽象解决了循环依赖问题,不再需要@Lazy注解
|
||||
*/
|
||||
public WebMvcConfig(HttpLoginArgumentResolver httpLoginArgumentResolver) {
|
||||
this.httpLoginArgumentResolver = httpLoginArgumentResolver;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
|
||||
resolvers.add(httpLoginArgumentResolver);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
package org.xyzh.common.annotation.resolver;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.support.WebDataBinderFactory;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
import org.xyzh.common.annotation.HttpLogin;
|
||||
import org.xyzh.common.core.domain.LoginDomain;
|
||||
import org.xyzh.common.core.security.TokenParser;
|
||||
import org.xyzh.common.redis.service.RedisService;
|
||||
import org.xyzh.common.utils.NonUtils;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* @description HttpLoginArgumentResolver.java文件描述 HTTP登录参数解析器
|
||||
* @filename HttpLoginArgumentResolver.java
|
||||
* @author yslg
|
||||
* @copyright xyzh
|
||||
* @since 2025-10-06
|
||||
*/
|
||||
@Component
|
||||
public class HttpLoginArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
|
||||
private final TokenParser tokenParser;
|
||||
private final RedisService redisService;
|
||||
|
||||
/**
|
||||
* 使用构造器注入,依赖TokenParser接口而非具体实现
|
||||
* 这样避免了与auth模块的直接依赖,解决循环依赖问题
|
||||
*/
|
||||
public HttpLoginArgumentResolver(TokenParser tokenParser,
|
||||
RedisService redisService) {
|
||||
this.tokenParser = tokenParser;
|
||||
this.redisService = redisService;
|
||||
}
|
||||
|
||||
private static final String TOKEN_PREFIX = "Bearer ";
|
||||
private static final String REDIS_LOGIN_PREFIX = "login:token:";
|
||||
|
||||
@Override
|
||||
public boolean supportsParameter(MethodParameter parameter) {
|
||||
return parameter.hasParameterAnnotation(HttpLogin.class)
|
||||
&& LoginDomain.class.isAssignableFrom(parameter.getParameterType());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
|
||||
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
|
||||
|
||||
HttpLogin httpLogin = parameter.getParameterAnnotation(HttpLogin.class);
|
||||
if (httpLogin == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 从请求头中获取token
|
||||
String token = extractTokenFromRequest(webRequest);
|
||||
|
||||
if (NonUtils.isEmpty(token)) {
|
||||
if (httpLogin.required()) {
|
||||
throw new IllegalArgumentException(httpLogin.message());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
// 验证token格式和有效性
|
||||
if (!tokenParser.validateToken(token, tokenParser.getUserIdFromToken(token))) {
|
||||
if (httpLogin.required()) {
|
||||
throw new IllegalArgumentException(httpLogin.message());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// 从Redis中获取LoginDomain
|
||||
String userId = tokenParser.getUserIdFromToken(token);
|
||||
String redisKey = REDIS_LOGIN_PREFIX + userId;
|
||||
LoginDomain loginDomain = (LoginDomain) redisService.get(redisKey);
|
||||
|
||||
if (loginDomain == null) {
|
||||
if (httpLogin.required()) {
|
||||
throw new IllegalArgumentException(httpLogin.message());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// 更新token信息
|
||||
loginDomain.setToken(token);
|
||||
return loginDomain;
|
||||
|
||||
} catch (Exception e) {
|
||||
if (httpLogin.required()) {
|
||||
throw new IllegalArgumentException(httpLogin.message());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 从请求中提取token
|
||||
* @param webRequest 请求对象
|
||||
* @return String token
|
||||
* @author yslg
|
||||
* @since 2025-10-06
|
||||
*/
|
||||
private String extractTokenFromRequest(NativeWebRequest webRequest) {
|
||||
HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
|
||||
if (request == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 优先从Authorization头获取
|
||||
String authHeader = request.getHeader(HttpHeaders.AUTHORIZATION);
|
||||
if (NonUtils.isNotEmpty(authHeader) && authHeader.startsWith(TOKEN_PREFIX)) {
|
||||
return authHeader.substring(TOKEN_PREFIX.length());
|
||||
}
|
||||
|
||||
// 从请求参数中获取token
|
||||
String token = request.getParameter("token");
|
||||
if (NonUtils.isNotEmpty(token)) {
|
||||
return token;
|
||||
}
|
||||
|
||||
// 从请求头中获取token
|
||||
token = request.getHeader("token");
|
||||
if (NonUtils.isNotEmpty(token)) {
|
||||
return token;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -23,5 +23,12 @@
|
||||
<groupId>org.xyzh</groupId>
|
||||
<artifactId>common-dto</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- JWT API - 仅接口定义,具体实现由auth模块提供 -->
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt-api</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -9,7 +9,7 @@ import org.xyzh.common.dto.role.TbSysRole;
|
||||
import org.xyzh.common.dto.permission.TbSysPermission;
|
||||
import org.xyzh.common.dto.dept.TbSysDeptRole;
|
||||
import org.xyzh.common.dto.menu.TbSysMenu;
|
||||
|
||||
import org.xyzh.common.vo.DeptRoleVO;
|
||||
/**
|
||||
* @description LoginDomain.java文件描述 登录域对象
|
||||
* @filename LoginDomain.java
|
||||
@@ -40,7 +40,7 @@ public class LoginDomain implements Serializable {
|
||||
* @author yslg
|
||||
* @since 2025-09-28
|
||||
*/
|
||||
private List<TbSysDeptRole> roles;
|
||||
private List<DeptRoleVO> roles;
|
||||
|
||||
/**
|
||||
* @description 用户权限列表
|
||||
@@ -132,7 +132,7 @@ public class LoginDomain implements Serializable {
|
||||
* @author yslg
|
||||
* @since 2025-09-28
|
||||
*/
|
||||
public List<TbSysDeptRole> getRoles() {
|
||||
public List<DeptRoleVO> getRoles() {
|
||||
return roles;
|
||||
}
|
||||
|
||||
@@ -141,7 +141,7 @@ public class LoginDomain implements Serializable {
|
||||
* @author yslg
|
||||
* @since 2025-09-28
|
||||
*/
|
||||
public void setRoles(List<TbSysDeptRole> roles) {
|
||||
public void setRoles(List<DeptRoleVO> roles) {
|
||||
this.roles = roles;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package org.xyzh.common.core.security;
|
||||
|
||||
import io.jsonwebtoken.Claims;
|
||||
|
||||
/**
|
||||
* @description TokenParser.java文件描述 令牌解析器接口
|
||||
* @filename TokenParser.java
|
||||
* @author yslg
|
||||
* @copyright xyzh
|
||||
* @since 2025-10-06
|
||||
*/
|
||||
public interface TokenParser {
|
||||
|
||||
/**
|
||||
* @description 从令牌中获取用户ID
|
||||
* @param token 令牌
|
||||
* @return String 用户ID
|
||||
* @author yslg
|
||||
* @since 2025-10-06
|
||||
*/
|
||||
String getUserIdFromToken(String token);
|
||||
|
||||
/**
|
||||
* @description 从令牌中获取所有声明信息
|
||||
* @param token 令牌
|
||||
* @return Claims 所有声明信息
|
||||
* @author yslg
|
||||
* @since 2025-10-06
|
||||
*/
|
||||
Claims getAllClaimsFromToken(String token);
|
||||
|
||||
/**
|
||||
* @description 验证令牌
|
||||
* @param token 令牌
|
||||
* @param userId 用户ID
|
||||
* @return boolean 是否有效
|
||||
* @author yslg
|
||||
* @since 2025-10-06
|
||||
*/
|
||||
boolean validateToken(String token, String userId);
|
||||
|
||||
/**
|
||||
* @description 检查令牌是否过期
|
||||
* @param token 令牌
|
||||
* @return boolean 是否过期
|
||||
* @author yslg
|
||||
* @since 2025-10-06
|
||||
*/
|
||||
boolean isTokenExpired(String token);
|
||||
}
|
||||
|
||||
@@ -3,13 +3,13 @@ package org.xyzh.common.dto.user;
|
||||
import org.xyzh.common.dto.BaseDTO;
|
||||
|
||||
/**
|
||||
* @description TbSysUserRole.java文件描述 用户角色
|
||||
* @filename TbSysUserRole.java
|
||||
* @description TbSysUserDeptRole.java文件描述 用户部门角色
|
||||
* @filename TbSysUserDeptRole.java
|
||||
* @author yslg
|
||||
* @copyright xyzh
|
||||
* @since 2025-09-26
|
||||
*/
|
||||
public class TbSysUserRole extends BaseDTO{
|
||||
public class TbSysUserDeptRole extends BaseDTO{
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@@ -20,6 +20,13 @@ public class TbSysUserRole extends BaseDTO{
|
||||
*/
|
||||
private String userID;
|
||||
|
||||
/**
|
||||
* @description 部门ID
|
||||
* @author yslg
|
||||
* @since 2024-06
|
||||
*/
|
||||
private String deptID;
|
||||
|
||||
/**
|
||||
* @description 角色ID
|
||||
* @author yslg
|
||||
@@ -49,6 +56,14 @@ public class TbSysUserRole extends BaseDTO{
|
||||
this.userID = userID;
|
||||
}
|
||||
|
||||
public String getDeptID() {
|
||||
return deptID;
|
||||
}
|
||||
|
||||
public void setDeptID(String deptID) {
|
||||
this.deptID = deptID;
|
||||
}
|
||||
|
||||
public String getRoleID() {
|
||||
return roleID;
|
||||
}
|
||||
@@ -75,9 +90,10 @@ public class TbSysUserRole extends BaseDTO{
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TbSysUserRole{" +
|
||||
return "TbSysUserDeptRole{" +
|
||||
"id='" + getID() + '\'' +
|
||||
", userID='" + userID + '\'' +
|
||||
", deptID='" + deptID + '\'' +
|
||||
", roleID='" + roleID + '\'' +
|
||||
", creator='" + creator + '\'' +
|
||||
", updater='" + updater + '\'' +
|
||||
@@ -0,0 +1,26 @@
|
||||
package org.xyzh.common.vo;
|
||||
|
||||
import org.xyzh.common.dto.dept.TbSysDept;
|
||||
import org.xyzh.common.dto.role.TbSysRole;
|
||||
|
||||
public class DeptRoleVO {
|
||||
|
||||
private TbSysDept dept;
|
||||
private TbSysRole role;
|
||||
|
||||
public TbSysDept getDept() {
|
||||
return dept;
|
||||
}
|
||||
|
||||
public void setDept(TbSysDept dept) {
|
||||
this.dept = dept;
|
||||
}
|
||||
|
||||
public TbSysRole getRole() {
|
||||
return role;
|
||||
}
|
||||
|
||||
public void setRole(TbSysRole role) {
|
||||
this.role = role;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package org.xyzh.common.vo;
|
||||
|
||||
public class UserVO {
|
||||
|
||||
private String id;
|
||||
private String userID;
|
||||
private String username;
|
||||
private String email;
|
||||
private String phone;
|
||||
private String wechatID;
|
||||
private String avatar;
|
||||
private String gender;
|
||||
private String familyName;
|
||||
private String givenName;
|
||||
private String fullName;
|
||||
private String idCard;
|
||||
private String address;
|
||||
private String createTime;
|
||||
private String updateTime;
|
||||
private String deleteTime;
|
||||
private String deleted;
|
||||
private String status;
|
||||
|
||||
|
||||
}
|
||||
@@ -18,4 +18,49 @@
|
||||
<maven.compiler.target>21</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- Spring Boot Redis Starter -->
|
||||
<!-- Redis 依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
<!-- 排除默认的logback依赖 -->
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-logging</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Data Redis -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- FastJSON2 for JSON serialization -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.fastjson2</groupId>
|
||||
<artifactId>fastjson2</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Common Core dependency -->
|
||||
<dependency>
|
||||
<groupId>org.xyzh</groupId>
|
||||
<artifactId>common-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Context for @Component annotation -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Boot Auto Configuration -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -1,7 +0,0 @@
|
||||
package org.xyzh;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello world!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package org.xyzh.common.redis.config;
|
||||
|
||||
import org.xyzh.common.core.constant.Constants;
|
||||
import java.nio.charset.Charset;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.data.redis.serializer.SerializationException;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONReader;
|
||||
import com.alibaba.fastjson2.JSONWriter;
|
||||
import com.alibaba.fastjson2.filter.Filter;
|
||||
|
||||
/**
|
||||
* @description FastJson2JsonRedisSerializer.java文件描述
|
||||
* @filename FastJson2JsonRedisSerializer.java
|
||||
* @author yslg
|
||||
* @copyright xyzh
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T>
|
||||
{
|
||||
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
|
||||
|
||||
static final Filter AUTO_TYPE_FILTER = JSONReader.autoTypeFilter(Constants.JSON_WHITELIST_STR);
|
||||
|
||||
private Class<T> clazz;
|
||||
|
||||
public FastJson2JsonRedisSerializer(Class<T> clazz)
|
||||
{
|
||||
super();
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] serialize(T t) throws SerializationException
|
||||
{
|
||||
if (t == null)
|
||||
{
|
||||
return new byte[0];
|
||||
}
|
||||
return JSON.toJSONString(t, JSONWriter.Feature.WriteClassName).getBytes(DEFAULT_CHARSET);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T deserialize(byte[] bytes) throws SerializationException
|
||||
{
|
||||
if (bytes == null || bytes.length <= 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
String str = new String(bytes, DEFAULT_CHARSET);
|
||||
|
||||
return JSON.parseObject(str, clazz, AUTO_TYPE_FILTER);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package org.xyzh.common.redis.config;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
|
||||
/**
|
||||
* @description RedisConfig.java文件描述 Redis配置
|
||||
* @filename RedisConfig.java
|
||||
* @author yslg
|
||||
* @copyright xyzh
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
@AutoConfigureBefore(RedisAutoConfiguration.class)
|
||||
public class RedisConfig {
|
||||
|
||||
@Bean
|
||||
@SuppressWarnings(value = { "unchecked", "rawtypes" })
|
||||
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory)
|
||||
{
|
||||
RedisTemplate<String, Object> template = new RedisTemplate<>();
|
||||
template.setConnectionFactory(connectionFactory);
|
||||
|
||||
FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);
|
||||
|
||||
// 使用StringRedisSerializer来序列化和反序列化redis的key值
|
||||
template.setKeySerializer(new StringRedisSerializer());
|
||||
template.setValueSerializer(serializer);
|
||||
|
||||
// Hash的key也采用StringRedisSerializer的序列化方式
|
||||
template.setHashKeySerializer(new StringRedisSerializer());
|
||||
template.setHashValueSerializer(serializer);
|
||||
|
||||
template.afterPropertiesSet();
|
||||
return template;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,384 @@
|
||||
package org.xyzh.common.redis.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.RedisCallback;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @description RedisService.java Redis工具服务类,封装常用Redis操作
|
||||
* @filename RedisService.java
|
||||
* @author yslg
|
||||
* @copyright xyzh
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
@SuppressWarnings(value = { "unchecked", "rawtypes" })
|
||||
@Component
|
||||
public class RedisService {
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
/**
|
||||
* @description 设置key-value
|
||||
* @param key String 键
|
||||
* @param value Object 值
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public void set(String key, Object value) {
|
||||
redisTemplate.opsForValue().set(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 设置key-value并指定过期时间
|
||||
* @param key String 键
|
||||
* @param value Object 值
|
||||
* @param timeout long 过期时间
|
||||
* @param unit TimeUnit 时间单位
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public void set(String key, Object value, long timeout, TimeUnit unit) {
|
||||
redisTemplate.opsForValue().set(key, value, timeout, unit);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取key对应的value
|
||||
* @param key String 键
|
||||
* @return Object 值
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public Object get(String key) {
|
||||
return redisTemplate.opsForValue().get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 删除key
|
||||
* @param key String 键
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public void delete(String key) {
|
||||
redisTemplate.delete(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 批量删除key
|
||||
* @param keys Collection<String> 键集合
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public void delete(Collection<String> keys) {
|
||||
redisTemplate.delete(keys);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 判断key是否存在
|
||||
* @param key String 键
|
||||
* @return boolean 是否存在
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public boolean hasKey(String key) {
|
||||
return redisTemplate.hasKey(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 设置key过期时间(秒)
|
||||
* @param key String 键
|
||||
* @param timeout long 过期秒数
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public void setExpire(String key, long timeout) {
|
||||
redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 设置key过期时间(自定义单位)
|
||||
* @param key String 键
|
||||
* @param timeout long 过期时间
|
||||
* @param unit TimeUnit 时间单位
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public void setExpire(String key, long timeout, TimeUnit unit) {
|
||||
redisTemplate.expire(key, timeout, unit);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取key剩余过期时间(秒)
|
||||
* @param key String 键
|
||||
* @return long 剩余秒数
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public long getExpire(String key) {
|
||||
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 原子递增
|
||||
* @param key String 键
|
||||
* @param delta long 增量
|
||||
* @return long 递增后值
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public long incr(String key, long delta) {
|
||||
Long result = redisTemplate.opsForValue().increment(key, delta);
|
||||
return result != null ? result : 0L;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 原子递减
|
||||
* @param key String 键
|
||||
* @param delta long 减量
|
||||
* @return long 递减后值
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public long decr(String key, long delta) {
|
||||
Long result = redisTemplate.opsForValue().increment(key, -delta);
|
||||
return result != null ? result : 0L;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Hash操作-put
|
||||
* @param key String 键
|
||||
* @param hashKey String 哈希键
|
||||
* @param value Object 值
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public void hSet(String key, String hashKey, Object value) {
|
||||
redisTemplate.opsForHash().put(key, hashKey, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Hash操作-get
|
||||
* @param key String 键
|
||||
* @param hashKey String 哈希键
|
||||
* @return Object 值
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public Object hGet(String key, String hashKey) {
|
||||
return redisTemplate.opsForHash().get(key, hashKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Hash操作-获取所有
|
||||
* @param key String 键
|
||||
* @return Map<Object, Object> 哈希所有键值对
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public Map<Object, Object> hGetAll(String key) {
|
||||
return redisTemplate.opsForHash().entries(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Hash操作-删除
|
||||
* @param key String 键
|
||||
* @param hashKeys String[] 哈希键数组
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public void hDelete(String key, String... hashKeys) {
|
||||
redisTemplate.opsForHash().delete(key, (Object[]) hashKeys);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description List操作-左入队
|
||||
* @param key String 键
|
||||
* @param value Object 值
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public void lPush(String key, Object value) {
|
||||
redisTemplate.opsForList().leftPush(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description List操作-右入队
|
||||
* @param key String 键
|
||||
* @param value Object 值
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public void rPush(String key, Object value) {
|
||||
redisTemplate.opsForList().rightPush(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description List操作-左出队
|
||||
* @param key String 键
|
||||
* @return Object 出队值
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public Object lPop(String key) {
|
||||
return redisTemplate.opsForList().leftPop(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description List操作-右出队
|
||||
* @param key String 键
|
||||
* @return Object 出队值
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public Object rPop(String key) {
|
||||
return redisTemplate.opsForList().rightPop(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description List操作-获取区间元素
|
||||
* @param key String 键
|
||||
* @param start long 起始索引
|
||||
* @param end long 结束索引
|
||||
* @return List<Object> 元素列表
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public List<Object> lRange(String key, long start, long end) {
|
||||
return redisTemplate.opsForList().range(key, start, end);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Set操作-添加元素
|
||||
* @param key String 键
|
||||
* @param values Object[] 元素数组
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public void sAdd(String key, Object... values) {
|
||||
redisTemplate.opsForSet().add(key, values);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Set操作-移除元素
|
||||
* @param key String 键
|
||||
* @param values Object[] 元素数组
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public void sRemove(String key, Object... values) {
|
||||
redisTemplate.opsForSet().remove(key, values);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Set操作-获取所有元素
|
||||
* @param key String 键
|
||||
* @return Set<Object> 元素集合
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public Set<Object> sMembers(String key) {
|
||||
return redisTemplate.opsForSet().members(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description ZSet操作-添加元素
|
||||
* @param key String 键
|
||||
* @param value Object 元素
|
||||
* @param score double 分数
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public void zAdd(String key, Object value, double score) {
|
||||
redisTemplate.opsForZSet().add(key, value, score);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description ZSet操作-移除元素
|
||||
* @param key String 键
|
||||
* @param values Object[] 元素数组
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public void zRemove(String key, Object... values) {
|
||||
redisTemplate.opsForZSet().remove(key, values);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description ZSet操作-按分数区间获取元素
|
||||
* @param key String 键
|
||||
* @param min double 最小分数
|
||||
* @param max double 最大分数
|
||||
* @return Set<Object> 元素集合
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public Set<Object> zRangeByScore(String key, double min, double max) {
|
||||
return redisTemplate.opsForZSet().rangeByScore(key, min, max);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description ZSet操作-获取全部元素
|
||||
* @param key String 键
|
||||
* @param start long 起始索引
|
||||
* @param end long 结束索引
|
||||
* @return Set<Object> 元素集合
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public Set<Object> zRange(String key, long start, long end) {
|
||||
return redisTemplate.opsForZSet().range(key, start, end);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 发布消息(Pub/Sub)
|
||||
* @param channel String 通道
|
||||
* @param message Object 消息内容
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public void publish(String channel, Object message) {
|
||||
redisTemplate.convertAndSend(channel, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 执行Redis原生命令
|
||||
* @param action RedisCallback<?> 回调命令
|
||||
* @return Object 执行结果
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public Object execute(RedisCallback<?> action) {
|
||||
return redisTemplate.execute(action);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取所有key(慎用)
|
||||
* @param pattern String 匹配模式
|
||||
* @return Set<String> key集合
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public Set<String> keys(String pattern) {
|
||||
Set<String> keys = redisTemplate.keys(pattern);
|
||||
Set<String> stringKeys = new HashSet<>();
|
||||
if (keys != null) {
|
||||
for (String key : keys) {
|
||||
stringKeys.add(key);
|
||||
}
|
||||
}
|
||||
return stringKeys;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 清空当前数据库(慎用)
|
||||
* @author yslg
|
||||
* @since 2025-09-07
|
||||
*/
|
||||
public void flushDb() {
|
||||
redisTemplate.execute((RedisCallback<Void>) connection -> { connection.flushDb(); return null; });
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user