explain : When using interceptors “ Request forwarding ” Infinite loop or “ Redirect ” Too many times when this kind of problem , It's usually Interceptor Wrong setting

Situation 1 : The exclusion interception path is not configured when the request is forwarded , That is to say, all the paths you visit are blocked , Cause to keep forwarding , To produce java.lang.StackOverflowError error

Situation two : The exclusion interception path is not configured during redirection , Will cause redirection all the time / Too many redirects

terms of settlement : Add interception path correctly , Correct exclusion of intercept paths

Spring Boot I made mistakes due to negligence ( Reference resources ):

Wrong writing :
 @Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor())
// addPathPatterns Used to add interception rules
// excludePathPatterns Used to exclude interception
// notes : Because of the neglect of index.html There is no /, Forward multiple times -->StackOverflowError
.addPathPatterns("/**").excludePathPatterns("index.html","/","/login","/user/login",
"/assets/**","/assets/brand/**","/assets/dist/css/**","/assets/dist/js/**");
}
Write it correctly :

Custom implementation WebMvcConfigurer Interface configuration class , And rewrite addInterceptors Method

 @Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor())
// addPathPatterns Used to add interception rules
// excludePathPatterns Used to exclude interception
.addPathPatterns("/**").excludePathPatterns("/index.html","/","/login","/user/login",
"/assets/**","/assets/brand/**","/assets/dist/css/**","/assets/dist/js/**");
}
Interceptor :
public class LoginHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// After landing successfully , Get the user's session
Object loginUser = request.getSession().getAttribute("loginUser");
if (loginUser==null){// Not logged in
System.out.println(request.getRequestURI());
request.getRequestDispatcher("index.html").forward(request,response);
// response.sendRedirect("index.html");
return false;
}else {
return true;
}
}
}
lesson : Write code carefully

Java Infinite forwarding with interceptors / Redirecting infinite loops / Too many redirections, error report (StackOverflowError) More articles on solutions

  1. java The interceptor Interceptor

    1, The concept of interceptor     java The interceptor in is dynamic interception Action Called object , It provides a mechanism for developers to work in a Action Execute a piece of code before and after execution , You can do it in one Action Prevent its execution before execution , It also provides ...

  2. java The end interceptor determines whether the client's request is ajax request

    java The end interceptor determines whether the client's request is ajax request Published in 2014-08-22 23:38:08 Ordinary requests and ajax The request header is different , Through the following String requestType = reque ...

  3. Java filter Use of interceptors

    1.web.xml To configure <!-- Verify that you are logged in Intercept function --> <filter> <filter-name>isLogin</filter-name> ...

  4. [Java] Use interceptors and custom annotations to log in and verify permissions

    1. Custom annotation Need to verify login comments package com.etaofinance.wap.common; import java.lang.annotation.Documented; import ...

  5. Java Two ways to use interceptors

    Interceptors are a good thing , I used it before , Now take a note of , For future reference ! firstly , Use org.aspectj.lang.annotation.Aspect On the first code : package com.test.interc ...

  6. Java Landing interceptors

    package com.beidou.warehouseerp.interceptor; import com.alibaba.fastjson.JSON; import com.beidou.war ...

  7. java web Interceptor 、 I understand

    1. First of all, we need to know what an interceptor is . What is a filter 1.1 What is an interceptor : Interceptor , stay AOP(Aspect-Oriented Programming) Before a method or field is accessed , Intercept and then add... Before or after ...

  8. java Middle interceptor filter What's the difference between monitors

    filter , Is in java web in , You introduced request,response Filter out some information in advance , Or set some parameters in advance , And then pass in servlet perhaps struts2 Of action Conduct business logic , Like filtering out illegal u ...

  9. java The difference between interceptors and filters in

    filter , Is in java web in , You introduced request,response Filter out some information in advance , Or set some parameters in advance , And then pass in servlet perhaps struts Of action Conduct business logic , Like filtering out illegal u ...

  10. Java Web The difference between interceptors and filters

    One .AOP: Section oriented programming ,Java Web There are two commonly used techniques in : Interceptor . filter Two . Interceptor 1. Definition : Before a method or field is accessed , Intercept and then add some actions before or after 2. principle : Most of the time , Interceptor methods are ...

Random recommendation

  1. SQL Server An old problem that cannot be saved after modifying the table structure

    Cannot save after modifying the table structure , This is every time you reload SQL Server The problems we will encounter in the future , Better a good memory than a bad pen , Write it down here . When you save the modified table structure, you will be prompted “ Saving changes is not allowed . Your changes require the following tables to be deleted and recreated . You can't ...

  2. ajax Principles and cross domain solutions

    ajax It's asynchronous JavaScript and XML. Through a small amount of data exchange with the server in the background ,AJAX Asynchronous update of web pages . This means that you can load the entire page without reloading it , Update a part of the web page . 1-- start-up ...

  3. nfs Service deployment records

    One . The concept is introduced NFS yes Network File System Abbreviation , Network file system . A protocol for distributed file systems , from Sun Companies to develop , On 1984 This year, it was announced to the public . The function is to make different machines . Different operating systems can ...

  4. c In the language, the value is assigned to the specified memory address by pointer

    1. An intuitive way Let's say we need to go to memory now 0x12ff7c An integer number is stored in the address 0x100. How can we do that ? We know that we can write data to the memory address it points to through a pointer , So the memory address here 0x12ff7c Its essence ...

  5. Zabbix Installation and wechat SMS reminder

    Zabbix brief introduction Zabbix In recent years, it has been recognized by major Internet companies , Of course, the first is due to its powerful monitoring function , Second, free and open source has also been favored by the majority of users .Zabbix It can monitor most of the indicators in the operating system , such as (CP ...

  6. saiku The fixed dimension of ( Required dimension )

    Problems at work , Write it down so you can find it later . stay saiku How to set fixed dimensions in ? find WorkspaceDropZone.js file , stay synchronize_query: function(){} Methods “ ...

  7. Install well .net framework After running slow

    performance Sometimes the system is slow , In especial .net The program runs quite slowly mscorsvw.exe And mscorsvw.exe *32 Two processes hang in the task manager and occupy... From time to time CPU solve Run the following two commands , Speed up the operation of these two processes , ...

  8. 【POJ3207】Ikki&#39;s Story IV - Panda&#39;s Trick

    POJ 3207 Ikki's Story IV - Panda's Trick liympanda, one of Ikki's friend, likes playing games with I ...

  9. Angular4 Component lifecycle

  10. Mysql 5.7--ubuntu18.04 Installation process and problems encountered

    Mysql 5.7 setup script 1. download mysql Of apt-config file a. https://dev.mysql.com/downloads/file/?id=477124 b. Click on downlo ...