Get the App
SLTechnology News&Howtos  ›  Development  › 

SpringBoot interceptor returns false to show how to solve the cross-domain problem.

Shulou Source: shulou.com Published: 2022-05-31 20:11:52 10月03日 Update

In this article, the editor introduces in detail "SpringBoot interceptor returns false to show how to solve cross-domain problems". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "SpringBoot interceptor returns false to show how to solve cross-domain problems" can help you solve your doubts.

The project recently added a function of ip blacklist and whitelist. It is found that if the interceptor filtered by ip returns to false, the front end will show cross-domain. After trying to modify the MVC configuration class, it still won't work. Finally, add a judgment to the interceptor.

@ Override public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {/ /-just add this-if (! (handler instanceof HandlerMethod)) {return true;}}

WebMvcConfigurer configuration class

@ Configuration@RestControllerAdvicepublic class WebMvcConfig implements WebMvcConfigurer {@ Resource private IpFilterInterceptor ipFilterInterceptor; @ Override public void addInterceptors (InterceptorRegistry registry) {/ / IP interceptor registry.addInterceptor (ipFilterInterceptor) .addPathPatterns ("/ *") .order (5) @ Override public void addCorsMappings (CorsRegistry registry) {registry.addMapping ("/ * *") .allowCredentials (true) .allowedOriginPatterns ("*") .allowedMethods ("*") .allowedHeaders ("*") .max Age (3600);}}

IP interceptor before modification

@ Componentpublic class IpFilterInterceptor implements HandlerInterceptor {/ * whether to enable * / private boolean enable; / * is whitelist * / private boolean isWhiteList; / * filter * / private List filters @ Override public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {/ / disable if (! enable) {return true;} String ip = Servlets.getRemoteAddr (request); / / Native does not filter if (Const.LOCALHOST_IP_V4.equals (ip)) {return true } / / filter boolean contains = false; for (String filter: filters) {if (Strings.isBlank (filter)) {continue;} / / detect contains = Utils.checkIpIn (ip, filter); if (contains) {break }} / / result boolean pass; if (isWhiteList) {pass = contains;} else {pass =! contains;} / / returns if (! pass) {response.setContentType (StandardContentType.APPLICATION_JSON) Servlets.transfer (response, HttpWrapper.of (ResultCode.IP_BAN). ToJsonString (). GetBytes ());} return pass;}}

Modified IP interceptor

@ Componentpublic class IpFilterInterceptor implements HandlerInterceptor {/ * whether to enable * / private boolean enable; / * is whitelist * / private boolean isWhiteList; / * filter * / private List filters @ Override public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {/ / disable if (! enable) {return true;} / /-just add this-if (! (handler instanceof HandlerMethod)) {return true } String ip = Servlets.getRemoteAddr (request); / / Native does not filter if (Const.LOCALHOST_IP_V4.equals (ip)) {return true;} / / filter boolean contains = false; for (String filter: filters) {if (Strings.isBlank (filter)) {continue } / / detect contains = Utils.checkIpIn (ip, filter); if (contains) {break;}} / / result boolean pass; if (isWhiteList) {pass = contains;} else {pass =! contains } / / return if (! pass) {response.setContentType (StandardContentType.APPLICATION_JSON); Servlets.transfer (response, HttpWrapper.of (ResultCode.IP_BAN). ToJsonString (). GetBytes ();} return pass;}}

Finally, the reason for the problem is that the interceptor needs to determine in preHandle that the handler type must be HandlerMethod in order to pass.

After reading this, the article "SpringBoot interceptor returns to false to show how to solve cross-domain problems" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, please follow the industry information channel.

Tags: Interceptor question list article content result filter native test configuration no proper front-end function reason idea new knowledge more step knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information macOS Shulou Tech Info MySQL Redmi