springboot中server.main.web-application-type=reactive导致的拦截器不生效

1、问题

今天写了在springboot项目中写了一个拦截器但是不生效,代码中拦截器implements HandlerInterceptor,同时implements WebMvcConfigurer中将该拦截器添加进去了,但是打断点进不去,也就意味着代码没有生效

java 复制代码
@Data
@Component
@ConfigurationProperties(prefix = "myapp")
public class WhiteIpProperties {
	private List<String> allowedIps;
}

IpAddressInterceptor

java 复制代码
/**
 * 定义拦截器以及执行时候的业务代码
 */
@Slf4j
@Component
public class IpAddressInterceptor implements HandlerInterceptor {

    @Autowired
    private WhiteIpProperties whiteIpProperties;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String ipAddress = request.getRemoteAddr();
        List<String> allowedIps = whiteIpProperties.getAllowedIps();
        if (allowedIps.contains(ipAddress)) {
            return true;
        } else {
            log.info("该ip不在访问的白名单中:{}", ipAddress);
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
            return false;
        }
    }
}

WebMvcConfig

java 复制代码
/**
 * 用于注册拦截器
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Autowired
    private IpAddressInterceptor ipAddressInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(ipAddressInterceptor)
                .addPathPatterns("/api/video/**", "/api/img/**")
                .excludePathPatterns("/error");
    }
}

2、定位原因

yml 复制代码
spring:
  main:
    web-application-type: reactive

问了下ai

那就按照ai提示的重新写代码


3、修改后正确代码

将上面的WebMvcConfig、WebMvcConfig删除掉重写,
下面是修改后的正确的代码,因为我是只有一个就直接这样写了,后面有扩展多个的情况

java 复制代码
import com.cao.config.WhiteIpProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

import java.util.List;

@Slf4j
@Component
public class IpAddressFilter implements WebFilter {

    @Autowired
    private WhiteIpProperties whiteIpProperties;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        String reqestIp = exchange.getRequest().getRemoteAddress().getHostName();
        List<String> allowedIps = whiteIpProperties.getAllowedIps();
        // 判断请求ip是否在白名单中
        if (allowedIps.contains(reqestIp)) {
            // 允许访问
            return chain.filter(exchange);
        } else {
            // 拒绝访问
            log.info("该ip不在访问的白名单中:{}", reqestIp);
            exchange.getResponse().setStatusCode(HttpStatus.FORBIDDEN);
            return exchange.getResponse().writeWith(
                    Mono.just(exchange.getResponse().bufferFactory().wrap("该ip不在访问的白名单中".getBytes()))
            );
        }
    }
}

调用后打断点进去入代码中,所以代码是生效的,同时要实现的拦截器的功能也测试是OK了。

4、扩展

因为我是只需要一个,如果有多个的话,就按照这个格式写

方式1:

方式2:

相关推荐
Leinwin7 小时前
OpenClaw 多 Agent 协作框架的并发限制与企业化规避方案痛点直击
java·运维·数据库
薛定谔的悦7 小时前
MQTT通信协议业务层实现的完整开发流程
java·后端·mqtt·struts
enjoy嚣士7 小时前
springboot之Exel工具类
java·spring boot·后端·easyexcel·excel工具类
罗超驿8 小时前
独立实现双向链表_LinkedList
java·数据结构·链表·linkedlist
无限大68 小时前
职场逻辑03:3步搞定高效汇报,让领导看到你的价值
后端
盐水冰8 小时前
【烘焙坊项目】后端搭建(12) - 订单状态定时处理,来单提醒和顾客催单
java·后端·学习
凸头9 小时前
CompletableFuture 与 Future 对比与实战示例
java·开发语言
wuqingshun3141599 小时前
线程安全需要保证几个基本特征
java·开发语言·jvm
紫丁香9 小时前
AutoGen详解一
后端·python·flask
努力也学不会java9 小时前
【缓存算法】一篇文章带你彻底搞懂面试高频题LRU/LFU
java·数据结构·人工智能·算法·缓存·面试