请你谈谈:spring拦截器的应用(token校验)

java 复制代码
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class MyInterceptor implements HandlerInterceptor {
    private String TOKEN = "123456789.";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("MyInterceptor preHandle: ");
        String token = request.getHeader("token");
        if (StrUtil.isBlank(token)) {
            response.getWriter().write("token is null");
            return false;
        }
        if (!TOKEN.equals(token)) {
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("MyInterceptor postHandle: ");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("MyInterceptor afterCompletion: ");
    }
}
java 复制代码
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 MyInterceptor myInterceptor;


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor).addPathPatterns("/**");
    }
}
java 复制代码
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello() {
        System.out.println("controller:");
        return "hello, zhaoshuai-lc";
    }
}

如下为:基于注解的token放行校验:

java 复制代码
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface NoNeedToken {
}
java 复制代码
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class MyInterceptor implements HandlerInterceptor {
    private String TOKEN = "123456789.";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("MyInterceptor preHandle: ");
        String token = request.getHeader("token");

        HandlerMethod handlerMethod = (HandlerMethod) handler;
        if (handlerMethod.getMethodAnnotation(NoNeedToken.class) != null ||
                handlerMethod.getBeanType().isAnnotationPresent(NoNeedToken.class)) {
            System.out.println("无需Token校验,放行...");
            return true;
        }

        if (StrUtil.isBlank(token)) {
            response.getWriter().write("token is null");
            return false;
        }
        if (!TOKEN.equals(token)) {
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("MyInterceptor postHandle: ");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("MyInterceptor afterCompletion: ");
    }
}
java 复制代码
@RestController
public class HelloController {
    @GetMapping("/hello")
    @NoNeedToken
    public String hello() {
        System.out.println("controller:");
        return "hello, zhaoshuai-lc";
    }
}
相关推荐
xiao--xin7 分钟前
Java定时任务实现方案(一)——Timer
java·面试题·八股·定时任务·timer
DevOpsDojo8 分钟前
HTML语言的数据结构
开发语言·后端·golang
MrZhangBaby21 分钟前
SQL-leetcode—1158. 市场分析 I
java·sql·leetcode
一只淡水鱼6635 分钟前
【spring原理】Bean的作用域与生命周期
java·spring boot·spring原理
五味香41 分钟前
Java学习,查找List最大最小值
android·java·开发语言·python·学习·golang·kotlin
时韵瑶1 小时前
Scala语言的云计算
开发语言·后端·golang
jerry-891 小时前
Centos类型服务器等保测评整/etc/pam.d/system-auth
java·前端·github
Jerry Lau1 小时前
大模型-本地化部署调用--基于ollama+openWebUI+springBoot
java·spring boot·后端·llama
工业甲酰苯胺1 小时前
深入解析 Spring AI 系列:解析返回参数处理
javascript·windows·spring
小白的一叶扁舟1 小时前
Kafka 入门与应用实战:吞吐量优化与与 RabbitMQ、RocketMQ 的对比
java·spring boot·kafka·rabbitmq·rocketmq