实现拦截器
定义FilterService类,配置拦截器(urlPatterns = "/*"拦截所有请求,urlPatterns = "/getAllUser"拦截单个请求)
package com.example.demo.service;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter(urlPatterns = "/getAllUser")
public class FilterService implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("======您已进入过滤器======");
filterChain.doFilter(servletRequest,servletResponse);
}
}
在启动类中通过ServletComponentScan注释配置拦截的路径
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan(basePackages = "com.example.demo.service")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
第二种方式,拦截登录token是否失效
LoginTokenInterceptor
package com.example.demo.utils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
public class LoginTokenInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
System.out.println("请求拦截");
// 获取请求中的token
String token = request.getHeader("Authorization");
String username = request.getHeader("Username");
// 验证token是否失效,这里的逻辑需要根据实际情况实现,例如调用认证服务
boolean isTokenValid = validateToken(token, username);
if (!isTokenValid) {
// token失效,返回错误信息或者重定向到登录页面
// response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); // 401
CodeSataus.setValue(401);
return true;
}
CodeSataus.setValue(200);
// token有效,继续执行请求
return true;
}
private boolean validateToken(String token, String username) {
// 实现token验证逻辑
// 返回true表示token有效,返回false表示token失效
return JwtTokenUtil.validateToken(token, username); // 示例:假设总是返回true
}
}
WebConfig
package com.example.demo.utils;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginTokenInterceptor())
.addPathPatterns("/**").
excludePathPatterns("/user/login/**");
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
//将 allowedOrigins 替换为 allowedOriginPatterns
//.allowedOrigins("*")
.allowedOriginPatterns("*")
.allowedMethods("*")
.allowedHeaders("*")
.allowCredentials(true)
.maxAge(3600);
}
}
CodeStatus
package com.example.demo.utils;
import java.util.Arrays;
public class CodeSataus {
public static int value = 200;
public static int[] white = { 401 };
public static void setValue (int data) {
value = data;
}
public static boolean whiteValue () {
return Arrays.binarySearch(white, value) >= 0;
}
}