Weblogic上的Spring Boot Servlet过滤器

时间:2019-07-16 13:42:46

标签: java spring spring-boot

我有一个在WebLogic Server版本上运行的SpringBoot应用程序: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
    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文件的内容

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

我正在设置servlet依赖关系,以防止jar冲突。

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

这是我的Filter类。

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

        }

    }

1 个答案:

答案 0 :(得分:1)

您的weblogic提供的servlet api可能是旧版本,您的过滤器类需要重写init方法。

在您的过滤器类中添加以下代码;

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

在新版本的servlet api中,过滤器类具有默认的空init方法。