java.lang.StackOverflowError
错误 @Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor())
// addPathPatterns 用于添加拦截规则
// excludePathPatterns 用于排除拦截
//注:因为疏忽下方的index.html前面未加/,转发多次-->StackOverflowError
.addPathPatterns("/**").excludePathPatterns("index.html","/","/login","/user/login",
"/assets/**","/assets/brand/**","/assets/dist/css/**","/assets/dist/js/**");
}
自定义的实现了WebMvcConfigurer接口的配置类,并重写addInterceptors方法
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor())
// addPathPatterns 用于添加拦截规则
// excludePathPatterns 用于排除拦截
.addPathPatterns("/**").excludePathPatterns("/index.html","/","/login","/user/login",
"/assets/**","/assets/brand/**","/assets/dist/css/**","/assets/dist/js/**");
}
public class LoginHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//登陆成功之后,获取用户的session
Object loginUser = request.getSession().getAttribute("loginUser");
if (loginUser==null){//未登录
System.out.println(request.getRequestURI());
request.getRequestDispatcher("index.html").forward(request,response);
// response.sendRedirect("index.html");
return false;
}else {
return true;
}
}
}
Java 使用拦截器无限转发/重定向无限循环/重定向次数过多报错(StackOverflowError) 解决方案
原文:https://www.cnblogs.com/knny/p/14276869.html