Could not load user defined filter in web.xml: com.xxx.CORSFilter

醉酒当歌 提交于 2019-12-11 15:53:35

问题


I have a SpringBoot application running on WebLogic Server Version: 12.2.1.3.0 When I define a custom servlet Filter its working fine on Embedded Tomcat. However, when i deploy my application as a war file to wlserver it throws following error after each request. What am i missing here?

    <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)

This is the content of my web.xml file

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <display-name>MWSAdminService</display-name>

    <filter>
        <filter-name>CORSFilter</filter-name>
        <filter-class>com.sample.CORSFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>CORSFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

I am setting servlet dependency as provided to prevent jar conflicts.

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>

This is my Filter class.

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;
            }

        }

    }

As a solution i tried to remove the filter definitions in web.xml file and registered my filter as a bean configuration since this is a SpringBoot application. I also removed @Component and @Order annotations from my filter. But the result was the same as above. It is still working on Tomcat but not in Weblogic. Here is the code for filter config bean.

@Configuration
public class Filters {

    @Bean
    public FilterRegistrationBean<CORSFilter> loggingFilter() {
        FilterRegistrationBean<CORSFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new CORSFilter());
        registrationBean.addUrlPatterns("/*");
        return registrationBean;
    }
}

回答1:


Your servlet api provided by weblogic is probably old version and your filter class needs to override init method.

Add below code at your filter class;

@Override
public void init(FilterConfig filterConfig) throws ServletException {}; 

At new versions of servlet api, filter class has default empty init method.



来源:https://stackoverflow.com/questions/57058669/could-not-load-user-defined-filter-in-web-xml-com-xxx-corsfilter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!