结对作业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);