java 过滤器拦截异常_springboot tomcat Filter过滤器异常

spring boot jar包可以正常启动,打包成war包放在tomcat就报错

问题报错:

08-Apr-2020 16:52:36.873 信息 [mysql-cj-abandoned-connection-cleanup] org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading Illegal access: this web application instance has been stopped already. Could not load []. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.

java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load []. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.

at org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading(WebappClassLoaderBase.java:1384)

at org.apache.catalina.loader.WebappClassLoaderBase.getResource(WebappClassLoaderBase.java:1034)

at com.mysql.cj.jdbc.AbandonedConnectionCleanupThread.checkThreadContextClassLoader(AbandonedConnectionCleanupThread.java:117)

at com.mysql.cj.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:84)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)

at java.lang.Thread.run(Thread.java:748)

后来发现问题的根本原因根本不在这,而是下面的问题报错

f2b443245307838fdecf808b8c834030.png

代码

@Slf4j

@Component

@WebFilter(filterName = "loginFilter", urlPatterns = "/*")

public class LoginFilter implements Filter {

@Autowired

private NoAuthConfig noAuthConfig;

private String[] noAuthUrls;

@Autowired

private RedisCache redisCache;

@Override

public void init(FilterConfig filterConfig) throws ServletException {

//初始化,无需鉴权链接

initNoAuthUrl(noAuthConfig.getNotAuthUrls()); // 这行代码报错

}

}

发现在使用@Webfilter注解Filter的情况下,不上外部tomcat时是没有问题的。但是在tomcat下运行时,filter中注入的bean就都是null

处理方法

@Configuration

@Slf4j

public class WebConfig {

@Bean

public Filter loginFilter(){

return new LoginFilter();

}

@Bean

public FilterRegistrationBean webTokenFilterRegistration(){

FilterRegistrationBean registrationBean = new FilterRegistrationBean();

registrationBean.setFilter(loginFilter());

registrationBean.addUrlPatterns("/*");

registrationBean.setName("loginFilter");

// 注意这个order顺序

registrationBean.setOrder(1800);

log.info("***************************loginFilter初始化***************************");

return registrationBean;

}

}

去掉 @Component 、@WebFilter注解

@Slf4j

public class LoginFilter implements Filter {

@Autowired

private NoAuthConfig noAuthConfig;

private String[] noAuthUrls;

@Override

public void init(FilterConfig filterConfig) throws ServletException {

//初始化,无需鉴权链接

initNoAuthUrl(noAuthConfig.getNotAuthUrls());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值