在不同端口上启动执行器时,NPE

问题描述

将执行器配置为在不同的端口上启动时,应用程序失败并显示以下堆栈跟踪:

java.lang.NullPointerException: null
at org.springframework.web.filter.GenericFilterBean.init(GenericFilterBean.java:241) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:270) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:106) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4528) [tomcat-embed-core-9.0.37.jar:9.0.37]

aop发出以下信息警告:

2020-08-16 10:01:11.240  INFO 83848 --- [           main] o.s.aop.framework.cglibAopProxy          : Unable to proxy interface-implementing method [public final void org.springframework.web.filter.GenericFilterBean.init(javax.servlet.FilterConfig) throws javax.servlet.servletexception] because it is marked as final: Consider using interface-based JDK proxies instead!

这是我的gradle依赖项:

plugins {
  id 'org.springframework.boot' version '2.3.3.RELEASE'
...
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.1.5.RELEASE'
implementation 'org.springframework.security:spring-security-jwt:1.0.10.RELEASE'

我们有一个@Aspect注释类:

...
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.security.web.firewall.RequestRejectedException;
import org.springframework.stereotype.Component;
...
@Aspect
@Component
@Slf4j
public class MyFilter {

    @Around("execution(public void org.springframework.security.web.FilterChainProxy.doFilter(..))")
    public void handleRequestRejectedException (ProceedingJoinPoint pjp) throws Throwable {
        try {
            pjp.proceed();
        } catch (RequestRejectedException exception) {
           ...
        }
    }
}

我发现了一些讨论此问题的问题,与aop有关,但到目前为止,我还没有找到解决方案。

任何/所有帮助表示赞赏。

  • 詹姆斯

解决方法

OP用一些示例代码更新了问题之后,我发现问题不是通用的切入点,就像我在评论中链接的其他问题一样,捕获了太多的类,

>

James,您正在定位方法FilterChainProxy.doFilter(..),即直接定位为Spring框架类。问题是,正如您在日志中看到的错误消息中所看到的那样:

o.s.aop.framework.CglibAopProxy:
  Unable to proxy interface-implementing method [
    public final void org.springframework.web.filter.GenericFilterBean.init(javax.servlet.FilterConfig)
    throws javax.servlet.ServletException
  ]
  because it is marked as final:
  Consider using interface-based JDK proxies instead!

问题在于目标类是从GenericFilterBean派生的,其中init(..)方法被标记为final。 CGLIB通过子类化和覆盖非私有方法来工作,但是最终方法不能被覆盖。因此,Spring AOP抱怨。

该错误消息还提示您寻求该问题的解决方案:您可以使用JDK代理来实现目标,而不是将CGLIB代理用于目标。幸运的是,doFilter是一种接口方法,更准确地说是JavaEE方法Filter.doFilter(..)的一种实现。

现在,下一个问题是您没有使用普通的Spring而是使用Spring Boot,它以预设的组合而闻名,即使在普通的Spring中,它似乎也无法切换到JDK代理。默认。但是Boot想要变得用户友好和智能化,从而使您陷入陷阱。我不记得处理这个问题的Spring Boot票证了,但是上次我检查它尚未解决。

避免出现这种情况的一种方法是对您的方面使用完整的AspectJ而不是仅使用Spring AOP,从而使您摆脱了基于代理的“ AOP lite”方法,并使您能够直接修改目标类或进行编织您的方面不在目标类中,而在所有调用类中,而是通过call()切入点(Spring AOP不支持)。

另一种解决方案是与您的方面挂钩到另一个问题较少的目标类/方法中,而不是直接与带有最终方法的Spring框架类挂钩。

P.S .:我不是Spring用户,我只是在这里回答AOP相关问题时才知道一些细节。实际上,令我惊讶的是,Spring所实现的CGLIB代理对于最终方法并不宽容,只是忽略它们并记录警告,而不是尝试覆盖它们并产生错误。也许有一些配置选项,但是我让Spring的人来回答这个问题,这里我没有头绪。

我无法准确回答,因为我需要查看完整的MCVE,最好是在GitHub上查看Maven或Gradle项目,以便进一步分析您的情况。我只想在这里解释一些基本知识。