Java 无法在web.xml:com.xxx.CORSFilter中加载用户定义的筛选器
我有一个SpringBoot应用程序在WebLogic服务器版本12.2.1.3.0上运行 当我定义自定义servlet过滤器时,它在嵌入式Tomcat上运行良好。然而,当我将应用程序作为war文件部署到wlserver时,它会在每个请求后抛出以下错误。我错过了什么Java 无法在web.xml:com.xxx.CORSFilter中加载用户定义的筛选器,java,spring,spring-boot,weblogic12c,Java,Spring,Spring Boot,Weblogic12c,我有一个SpringBoot应用程序在WebLogic服务器版本12.2.1.3.0上运行 当我定义自定义servlet过滤器时,它在嵌入式Tomcat上运行良好。然而,当我将应用程序作为war文件部署到wlserver时,它会在每个请求后抛出以下错误。我错过了什么 <Could not load user defined filter in web.xml: com.thy.bwsadmin.CORSFilter. java.lang.AbstractMethodError
<Could not load user defined filter in web.xml: com.thy.bwsadmin.CORSFilter.
java.lang.AbstractMethodError
at weblogic.servlet.internal.FilterManager$FilterInitAction.run(FilterManager.java:400)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:328)
at weblogic.security.service.SecurityManager.runAsForUserCode(SecurityManager.java:197)
at weblogic.servlet.provider.WlsSecurityProvider.runAsForUserCode(WlsSecurityProvider.java:203)
at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:71)
at weblogic.servlet.internal.FilterManager.initFilter(FilterManager.java:130)
at weblogic.servlet.internal.FilterManager.loadFilter(FilterManager.java:92)
at weblogic.servlet.internal.FilterManager.preloadFilters(FilterManager.java:72)
at weblogic.servlet.internal.WebAppServletContext.preloadResources(WebAppServletContext.java:1928)
at weblogic.servlet.internal.WebAppServletContext.start(WebAppServletContext.java:3106)
at weblogic.servlet.internal.WebAppModule.startContexts(WebAppModule.java:1843)
at weblogic.servlet.internal.WebAppModule.start(WebAppModule.java:884)
at weblogic.application.internal.ExtensibleModuleWrapper$StartStateChange.next(ExtensibleModuleWrapper.java:360)
at weblogic.application.internal.ExtensibleModuleWrapper$StartStateChange.next(ExtensibleModuleWrapper.java:356)
at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:45)
at weblogic.application.internal.ExtensibleModuleWrapper.start(ExtensibleModuleWrapper.java:138)
at weblogic.application.internal.flow.ModuleListenerInvoker.start(ModuleListenerInvoker.java:124)
at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:233)
at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:228)
at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:45)
at weblogic.application.internal.flow.ModuleStateDriver.start(ModuleStateDriver.java:78)
at weblogic.application.internal.flow.StartModulesFlow.activate(StartModulesFlow.java:52)
at weblogic.application.internal.BaseDeployment$2.next(BaseDeployment.java:752)
at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:45)
at weblogic.application.internal.BaseDeployment.activate(BaseDeployment.java:262)
作为解决方案,我尝试删除web.xml文件中的过滤器定义,并将过滤器注册为bean配置,因为这是一个SpringBoot应用程序。我还从过滤器中删除了@Component和@Order注释。
但结果与上述相同。它仍在Tomcat上工作,但在Weblogic中没有。下面是过滤器配置bean的代码
@Configuration
public class Filters {
@Bean
public FilterRegistrationBean<CORSFilter> loggingFilter() {
FilterRegistrationBean<CORSFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new CORSFilter());
registrationBean.addUrlPatterns("/*");
return registrationBean;
}
}
@配置
公共类过滤器{
@豆子
公共过滤器注册Bean loggingFilter(){
FilterRegistrationBean registrationBean=新的FilterRegistrationBean();
setFilter(新的CORSFilter());
registrationBean.addUrlPatterns(“/*”);
返回注册bean;
}
}
weblogic提供的servlet api可能是旧版本,您的筛选器类需要重写init方法
在过滤器类中添加以下代码
@Override
public void init(FilterConfig filterConfig) throws ServletException {};
在新版本的servlet api中,filter类具有默认的空init方法。如何创建war文件?。。。你能分享springboot应用程序类吗?向我的过滤类添加init方法修复了这个问题。谢谢!
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import com.thy.bwsadmin.service.SecurityUserService;
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CORSFilter implements Filter {
@Autowired
SecurityUserService securityUserService;
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req;
boolean isAuthenticated = authenticateUser(request.getHeader("identity_no"), request.getRequestURI());
if (isAuthenticated) {
if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
response.setStatus(HttpServletResponse.SC_OK);
} else {
chain.doFilter(req, res);
}
} else {
response.sendError(HttpServletResponse.SC_OK, "401");
}
}
private boolean authenticateUser(String userId, String requestURI) {
if (Util.isNotEmpty(userId)
&& securityUserService.isAuthorizedForEndpoint(userId.trim(), requestURI)) {
return true;
}else{
return false;
}
}
}
@Configuration
public class Filters {
@Bean
public FilterRegistrationBean<CORSFilter> loggingFilter() {
FilterRegistrationBean<CORSFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new CORSFilter());
registrationBean.addUrlPatterns("/*");
return registrationBean;
}
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {};