用Spring—Security、JWT、Mybatis、Redis实现登录的具体流程和代码演示

1.引入依赖确保项目中包含以下依赖:
Redis 相关依赖:用于缓存用户信息和 Token

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

MyBatis 相关依赖:用于数据库操作

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.2</version>
</dependency>

Spring Security 相关依赖:用于安全认证

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

JWT 相关依赖:用于生成和解析 Token

<dependency>
    <groupId>io.jsonwebtoken</groupId>
    <artifactId>jjwt</artifactId>
    <version>0.9.1</version>
</dependency>

2.编写工具类和配置类
Redis 工具类:用于操作 Redis 缓存

@Component
public class RedisCache {
    @Autowired
    private RedisTemplate redisTemplate;

    // 缓存基本对象
    public <T> void setCacheObject(String key, T value) {
        redisTemplate.opsForValue().set(key, value);
    }

    // 缓存基本对象并设置过期时间
    public <T> void setCacheObject(String key, T value, Integer timeout, TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
    }

    // 设置有效时间
    public boolean expire(String key, long timeout) {
        return expire(key, timeout, TimeUnit.SECONDS);
    }

    // 设置有效时间
    public boolean expire(String key, long timeout, TimeUnit unit) {
        return redisTemplate.expire(key, timeout, unit);
    }

    // 获取缓存的基本对象
    public <T> T getCacheObject(String key) {
        ValueOperations<String, T> operation = redisTemplate.opsForValue();
        return operation.get(key);
    }

    // 删除单个对象
    public boolean deleteObject(String key) {
        return redisTemplate.delete(key);
    }

    // 删除集合对象
    public long deleteObject(Collection collection) {
        return redisTemplate.delete(collection);
    }
}

JWT 工具类:用于生成和解析 JWT Token

public class JwtUtil {
    public static final Long JWT_TTL = 2 * 24 * 60 * 60 * 1000L; // 有效期为两天
    public static final String JWT_KEY = "your_secret_key"; // 设置密钥

    // 生成 JWT Token
    public static String createJWT(String subject) {
        JwtBuilder builder = getJwtBuilder(subject, null, getUUID());
        return builder.compact();
    }

    // 解析 JWT Token
    public static Claims parseJWT(String jwt) throws Exception {
        SecretKey secretKey = generalKey();
        return Jwts.parser().setSigningKey(secretKey).parseClaimsJws(jwt).getBody();
    }

    // 生成加密后的密钥
    public static SecretKey generalKey() {
        byte[] encodedKey = Base64.getDecoder().decode(JWT_KEY);
        return new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES");
    }
}

FastJsonRedisSerializer:用于 Redis 使用 FastJson 序列化

public class FastJsonRedisSerializer<T> implements RedisSerializer<T> {
    public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值