后台
后台程序图片

- 新建token的基础类
public class Constants {
public final static String TOKEN = "token";
}
- 配置redis
pom.xml中添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
application.yml中配置(注意在spring下)
server:
port: 8088
spring:
redis:
database: 0
host: localhost
port: 6379
timeout: 10000ms
password:
jedis:
pool:
max-active: 200
max-wait: -1ms
max-idle: 8
min-idle: 0
token:
#登录生成的token在redis中保存的有效期,单位:秒
expires: 1000
timeout: true
aes:
#AES登录密码加密的私钥,要求长度必须为16位。
key: hj7x89HTyuBI0452
添加redis工具类
/**
* redis 数据操作工具类
*/
@Component
public class RsUtil {
@Resource
public RedisTemplate<String, String> redisTemplate;
@Autowired
public PropertiesConfig properties;
/**
* 将数据插入redis
*
* @param key 索引
* @param value 值
*/
public void set(String key, String value) {
ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
if (properties.isTimeout()) {
valueOperations.set(key, value, properties.getTokenExpires().longValue(), TimeUnit.SECONDS);
} else {
valueOperations.set(key, value);
}
}
/**
* 删除redis中数据
*
* @param key 索引
*/
public void del(String key) {
ValueOperations<String, String> vo = redisTemplate.opsForValue();
RedisOperations<String, String> operations = vo.getOperations();
operations.delete(key);
}
/**
* 查询redis中数据
*
* @param key 索引
* @return 查询结果
*/
public String get(String key) {
ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
String value = valueOperations.get(key);
if (value != null && !value.equals("")) {
set(key, value);
}
return value;
}
}
至此redis已配置完,接下来开始配置拦截器
首先配置下依赖 fastjson 依赖 (json-object数据转换)
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.69</version>
</dependency>
先写一个请求信息获取类ServletUtils
/**
* 请求信息获取工具类
*/
public class ServletUtils {
/**
* 获取配置信息
* @return 配置信息
*/
public static ServletRequestAttributes getRequestAttributes() {
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
return (ServletRequestAttributes) attributes;
}
/**
* 获取请求信息
* @return 请求信息
*/
public static HttpServletRequest getRequest() {
re
这篇博客详细介绍了如何在SpringBoot后台结合Vue前端实现Token的生成与管理,利用Redis存储Token,并通过拦截器进行权限验证。同时,文章还演示了Vuex和Axios的使用,包括Vuex的store配置、Axios的接口封装以及跨域问题的处理,确保在登录后能正确保存和使用Token。
最低0.47元/天 解锁文章


被折叠的 条评论
为什么被折叠?



