springboot(六):拦截器的设置

上一章节我们讲到token可以在拦截器进行验证和解析。所以这一章节我们就讲讲如何设置拦截器。

:这边我先说明一些,springboot这个系列我不会特别去讲某个知识点,我只会根据目前项目需要什么功能,然后就讲什么功能。有针对性的去实现,可能比漫无目的的了解会更有效果一点,不是吗。比如拦截器这里会涉及配置类
WebMvcConfigurer的实现,这个类还有其他配置的实现,这里我也不具体去说,这里只涉及对它的拦截器配置。后面有设计其他配置功能再做其他处理。

WebMvcConfigurer

WebMvcConfigurer配置类其实是Spring内部的一种配置方式,采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制,可以自定义一些Handler,Interceptor,ViewResolver,MessageConverter。基于java-based方式的spring mvc配置,需要创建一个配置类并实现WebMvcConfigurer 接口。

拦截器实现

1.创建WebMvcConfigurer的配置实现类。

package com.admin.zhou.config;

import com.admin.zhou.interceptor.AdminIntercepter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {


    @Autowired
    private AdminIntercepter adminIntercepter;
    


    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(adminIntercepter).addPathPatterns("/api/**").excludePathPatterns("/api/userLogin");

    }


}

WebMvcConfigurer还有其他配置接口方法,这里只讲addInterceptors的拦截器添加方法。
这里主要介绍三个方法:
addInterceptor:添加实现HandlerInterceptor接口的拦截器实例,下面会讲。
addPathPatterns:用于设置拦截器的过滤路径规则,addPathPatterns("/api/**")意思就是对/api/下的所以请求进行拦截。
excludePathPatterns:用于设置不需要拦截的过滤规则,这个拦截器主要是用来拦截除登录以外的所有请求,所有把/api/userLogin忽略。

拦截器
package com.admin.zhou.interceptor;

import com.admin.zhou.common.Const;
import com.admin.zhou.common.Tool;
import com.admin.zhou.redis.RedisUtil;
import com.admin.zhou.utils.RSAUtils;
import com.alibaba.druid.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Component
public class AdminIntercepter implements HandlerInterceptor {

    private Logger logger = LoggerFactory.getLogger(AdminIntercepter.class);

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {

        HttpSession session = request.getSession();
        String token = (String) session.getAttribute(Const.Admin_Token);


        if (StringUtils.isEmpty(token)){
            Tool.responeTo(response,"请先登录");
            return false;
        }

        String res = null;
        try {
            res = RSAUtils.decryptByPriKey(token, RSAUtils.PRIVATE_KEY);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (StringUtils.isEmpty(res)){
            Tool.responeTo(response,"请先登录");
            return false;
        }

        String[] result = res.split("-");

        String redisRes = (String) redisUtil.get(Const.REDIS_ADMIN+result[0]);

        if (redisRes != null && redisRes.equals(token)){
            request.setAttribute(Const.CURRENT_USER,result[0]);
            return true;
        }else{
            Tool.responeTo(response,"token失效");
            return false;
        }
    }

}

preHandler叫前置拦截,返回true 表示允许直接放过,不进行任何拦截,反之就拦截不再往下请求。

描述一下上面的过程,用户端将将服务端返回的token存放在HttpSession,所以上述方法在HttpSession获取token,通过RSAUtils.decryptByPriKey方法对token进行私钥解密获取用户id。然后在从redis中获取存放的token进行比对。redis里面的token是在用户登录的时候进行存储的。以上就是拦截器对token的验证和解析的过程。

项目git地址:https://gitee.com/stonezry/Springboot-Admin-Demo

欢迎关注本人公众号和小程序,谢谢
在这里插入图片描述

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值