Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report