springboot + vue 后台token生成 拦截器 redis实现 前台封装axios xueX 接口实现

这篇博客详细介绍了如何在SpringBoot后台结合Vue前端实现Token的生成与管理,利用Redis存储Token,并通过拦截器进行权限验证。同时,文章还演示了Vuex和Axios的使用,包括Vuex的store配置、Axios的接口封装以及跨域问题的处理,确保在登录后能正确保存和使用Token。
摘要由CSDN通过智能技术生成

后台
后台程序图片
在这里插入图片描述

  1. 新建token的基础类
public class Constants {

	public final static String TOKEN = "token";

}
  1. 配置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
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值