SpringBoot interceptor excludePathPatterns method does not take effect solution

When the interceptor uses the excludePathPatterns() method to exclude the access path, it is found that it does not take effect. The configuration code is as follows

/**
 * @author 程序员大佬超
 * @date 2023-03-01 15:33.
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    
    
    @Autowired
    AuthTokenInterceptor authTokenInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    
    
        List<String> patterns = new ArrayList<>();
        patterns.add("/**/login/login");
        registry.addInterceptor(authTokenInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(patterns);
    }
}

Interface access address: http://127.0.0.1:8088/api-im/login/login

In fact, this problem is nothing more than two reasons in most cases:

1. The access path configuration to be excluded is sloppy and wrongly written.
2. The access path to be excluded does not exist, or the request parameter is incorrectly parsed and an error occurs. At this time, SpringBoot will automatically change the path to /error . You can verify it at the break point in the custom interceptor.


Here is the second reason, you can see that the requestURI in the custom interceptor has changed to /error.

insert image description here

Solution:

Ensure that the interface is correct or check the request parameters. Anyway, to ensure that it can be entered normally, I am missing a parameter in the request header, and then when parsing, the log actually indicates that the request header is missing.

WARN 15628 --- [nio-8088-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : 
Resolved [org.springframework.web.bind.MissingRequestHeaderException: Required request header 'test' for method parameter type String is not present]

Then, just add

insert image description here

Guess you like

Origin blog.csdn.net/xch_yang/article/details/129386989