我尝试使用spring配置自定义过滤器AccountVerificationFilter并获得NPE,这些是我的配置
我在web.xml中配置了cpFilterCHain,如下所示
<filter>
<filter-name>cpFilterChain
</filter-name>
<filter-class>org.springframework.security.web.FilterChainProxy
</filter-class>
</filter>
<filter>
<filter-name>cpFilterChain
</filter-name>
<filter-class>org.springframework.security.web.FilterChainProxy
</filter-class>
</filter>
这是我的applicationCOntext.xml
<bean id="accountVerificationFilter" class="com.dc.apps.cpportal.security.filter.AccountVerificationFilter" />
<bean id="cpFilterChain" class="org.springframework.security.web.FilterChainProxy">
<sec:filter-chain-map path-type="ant">
<sec:filter-chain pattern="/**" filters="
accountVerificationFilter" />
</sec:filter-chain-map>
</bean>
这是我的AccountVerificationFilter组件
@Component
public class AccountVerificationFilter implements Filter {
private static final String UPDATE_PASSWORD_URL = "/updatePassword.do";
private static final String DO_ACTION_URL = ".do";
private static final String IS_URL_VERIFIED = "isUrlVerified";
private Long maxSessionTimeToLive = null;
@Autowired
protected DCAuthenticationSuccessHandler successHandler;
@Autowired
protected ApplicationUrlConfig applicationUrlConfig;
@Autowired
protected AuthenticationFilterConfiguration authenticationConfiguration;
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException,
ServletException {
doFilterHttp((HttpServletRequest) request, (HttpServletResponse) response, filterChain);
}
}
使用弹簧安全3.1.4。 Tomcat开始很好但是当我转到我的应用程序时看到这个异常
SEVERE: Servlet.service() for servlet [cpportal] in context with path [/cpportal] threw exception
java.lang.NullPointerException
at org.springframework.security.web.FilterChainProxy.getFilters(FilterChainProxy.java:202)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:176)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at com.dc.core.common.SetXFrameOptionsFilter.doFilter(SetXFrameOptionsFilter.java:21)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)
at com.dc.core.common.FlashRecyclingFilter.doFilterInternal(FlashRecyclingFilter.java:21)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at com.dc.core.common.StripJSessionIdFilter.doFilter(StripJSessionIdFilter.java:101)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
有人可以帮我解决这个问题吗?
答案 0 :(得分:1)
我认为问题在于web.xml中的cpFilterChain是由应用程序服务器创建的重复对象,而不是弹簧容器实例化的原始对象。
尝试使用delegatingfilterproxy将过滤器任务委托给Spring bean
<filter>
<filter-name>cpFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
<filter-mapping>
<filter-name>cpFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>