菜鸟的博客

纵有疾风起,人生不言弃。

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

结对作业2

PSP0(Personal Software Process Stages ) 所需时间(TIME)
Planning(计划) 今天把后端学穿了,但是还是有点不会的
   estimate[估计这个任务需要多少时间 ] 1h
Development (开发 )  
   · Design [具体设计 ] 1
   · Coding [具体编码 ] 2
   · Test [测试(自我测试,修改代码,提交修改)] 1
Reporting(报告 )  
  · Postmortem & Process Improvement Plan [事后总结, 并提出过程改进计划 ] 1h
合计 6h

首先引入依赖

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

在yml中引入redis数据库

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/big_event
    username: root
    password: 1234
  data:
    redis:
      host: localhost
      port: 6379


mybatis:
  configuration:
    map-underscore-to-camel-case: true #开启下划线驼峰命名之间的转换

测试代码

package com.di.bigevent;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import java.util.concurrent.TimeUnit;

@SpringBootTest//如果在这个测试类上添加了这个注解,那么将来单元测试方法执行前,会先初始化spring容器
public class RedisTest {


    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Test
    public void testSet(){

        //往redis中存储一个键值对 StringRedisTemplate
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        operations.set("username","zhangsan");
        operations.set("id","1",15, TimeUnit.SECONDS);
    }

    @Test
    public void testGet(){
        //从redis中获取键值对
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        System.out.println(operations.get("username"));
    }
}

令牌主动失效

  • 把令牌存到redis中
  • 拦截器Intercepter中,同时验证自带的token和redis中的taken

首先在usercontroller中

 @Autowired
    private StringRedisTemplate stringRedisTemplate;

//把token存到redis中
            ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
            operations.set(token,token,1, TimeUnit.HOURS);

从拦截器中添加验证redis中token的步骤

@Autowired
    private StringRedisTemplate stringRedisTemplate;


//从redis中获取相同的token
            ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
            String redisToken = operations.get(token);
            if(redisToken==null)
            {
                //token已经失效了
                throw new RuntimeException();
            }

在修改密码的controller中,添加参数

@RequestHeader("Authorization") String token

修改密码后,删除redis中的token

//删除redis中对应的token
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        operations.getOperations().delete(token);

posted on   hhmzd233  阅读(11)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2023-04-12 2023.4.12
点击右上角即可分享
微信分享提示