Java use interceptor infinite forwarding / redirection infinite loop / redirection times too many error (stack overflow error) solution

Jiulishi 2021-01-14 14:31:02
java use interceptor infinite forwarding


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
版权声明
本文为[Jiulishi]所创,转载请带上原文链接,感谢
https://javamana.com/2021/01/20210114135733619D.html

  1. Redis basic command
  2. Summary of MySQL articles
  3. 2、 Create k8s cluster in 5 seconds
  4. data自定义属性在jQuery中的用法
  5. Linux常见解压缩
  6. Detailed explanation of HBase basic principle
  7. 1、 Why and how to learn k8s
  8. Java advanced (29) -- HashMap set
  9. java中大文件上传
  10. Weblogic 2017-3248 analysis of Java Security
  11. Kubernetes official java client 8: fluent style
  12. Explain the function of thread pool and how to use it in Java
  13. Programming software tutorial video Encyclopedia: C + + / Java / Python / assembly / easy language (with tutorial)
  14. Description of dependency problem after javacv is updated to 1.5. X and how to reduce the size of dependency package
  15. Java reflection & dynamic agent
  16. Building Apache 2.4 + php7 + mysql8 in centos7 environment
  17. Summary of Java multithreading (1)
  18. Oracle AWR report generation
  19. Four magic functions of mybatis, don't step on the pit!
  20. A 16-year-old high school student successfully transplanted Linux to iPhone and posted a detailed guide
  21. Centos7 one click installation of JDK1.8 shell script
  22. Mounting of file system in Linux (centos7)
  23. How does serverless deal with the resource supply demand of k8s in the offline scenario
  24. Detailed explanation of HBase basic principle
  25. Spring security oauth2.0 authentication and authorization 4: distributed system authentication and authorization
  26. Redis performance Part 5 redis buffer
  27. JavaScript this keyword
  28. Summary of Java multithreading (3)
  29. Sentry(v20.12.1) K8S 云原生架构探索, SENTRY FOR JAVASCRIPT 手动捕获事件基本用法
  30. Sentry(v20.12.1) K8S 云原生架构探索, SENTRY FOR JAVASCRIPT 手动捕获事件基本用法
  31. (10) Spring from the beginning to the end
  32. Summary of Java multithreading (2)
  33. Spring source notes! From the introduction to the source code, let you really understand the source code
  34. A stormy sunny day
  35. Zookeeper (curator), the implementation of distributed lock
  36. Show the sky! Tencent T4's core Java Dictionary (framework + principle + Notes + map)
  37. Spring boot project, how to gracefully replace the blank value in the interface parameter with null value?
  38. Spring boot project, how to gracefully replace the blank value in the interface parameter with null value?
  39. docker+mysql集群+读写分离+mycat管理+垂直分库+负载均衡
  40. docker+mysql集群+读写分离+mycat管理+垂直分库+负载均衡
  41. To what extent can I go out to work?
  42. Java 使用拦截器无限转发/重定向无限循环/重定向次数过多报错(StackOverflowError) 解决方案
  43. Implementation of rocketmq message sending based on JMeter
  44. How to choose the ticket grabbing app in the Spring Festival? We have measured
  45. Implementation of rocketmq message sending based on JMeter
  46. My programmer's Road: self study java
  47. My programmer's Road: self study java
  48. All in one, one article talks about the use of virtual machine VirtualBox and Linux
  49. All in one, one article talks about the use of virtual machine VirtualBox and Linux
  50. Java 使用拦截器无限转发/重定向无限循环/重定向次数过多报错(StackOverflowError) 解决方案
  51. [Java training project] Java ID number recognition system
  52. How does serverless deal with the resource supply demand of k8s in the offline scenario
  53. Detailed explanation of HBase basic principle
  54. Explain the function of thread pool and how to use it in Java
  55. Kubernetes official java client 8: fluent style
  56. 010_MySQL
  57. Vibrant special purchases for the Spring Festival tiktok section, hundreds of good things to make the year more rich flavor.
  58. 010_MySQL
  59. Of the 4 million docker images, 51% have high-risk vulnerabilities
  60. Rocketmq CPP client visual studio 2019 compilation