springboot内置的tomcat

在看尚硅谷的教程时,跟着写了一遍springboot的拦截器,主要代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

public class LoginHandlerInterceptor implements HandlerInterceptor {
//目标方法执行之前
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Object user = request.getSession().getAttribute("loginUser");
if(user == null){
//未登陆,返回登陆页面
/* HttpSession session=(HttpSession)request.getSession();
session.setAttribute("msg","没有权限请先登陆");*/
//request.getRequestDispatcher("/login.html").forward(request,response);
response.sendRedirect("/crud/index.html");
return false;
}else{
//已登陆,放行请求
return true;
}

}

结果发现它会拦截静态资源,这和老师讲的不一样呀,于是就网上查,发现是spingboot版本的问题,我用的是2.2.0,静态资源也全部拦截了,而且还抛了个异常(瞧我这暴脾气):

1
2
3
4
5
java.lang.AbstractMethodError: org.apache.tomcat.websocket.server.WsSessionListener.sessionCreated(Ljavax/servlet/http/HttpSessionEvent;)V
at org.apache.catalina.session.StandardSession.tellNew(StandardSession.java:388) [tomcat-embed-core-9.0.27.jar:9.0.27]
at org.apache.catalina.session.StandardSession.setId(StandardSession.java:360) [tomcat-embed-core-9.0.27.jar:9.0.27]
at org.apache.catalina.session.StandardSession.setId(StandardSession.java:341) [tomcat-embed-core-9.0.27.jar:9.0.27]
at org.apache.catalina.session.ManagerBase.createSession(ManagerBase.java:686)

springboot2.2.0默认Tomcat是9.0.27版本的,使用servlet4.0,但是SpringBoot 2.x还包含着 servlet3.1,版本不兼容,网上说的解决办法是在pom里更换Tomcat为低版本,可是之后发现springboot在调用Tomcat9.0.27中新添加的部分接口,强行更换会报异常:

1
2
3
An attempt was made to call a method that does not exist. The attempt was made from the following location: org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:175)
The following method did not exist:
org.apache.tomcat.util.modeler.Registry.disableRegistry()V

未找到解决办法

待续。。。

Donate comment here