Tomcat / ApplicationContext无法找到Filter类

时间:2016-08-04 18:32:01

标签: java spring-mvc tomcat maven-3 applicationcontext

  

的web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/j2ee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      <display-name>Self Service Portal</display-name>
      <welcome-file-list>
        <welcome-file>home.html</welcome-file>
        <welcome-file>default.html</welcome-file>
      </welcome-file-list>

      <filter>
        <filter-name>CorsFilter</filter-name>
        <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
        <init-param>
          <param-name>cors.allowed.methods</param-name>
          <param-value>GET, POST, PUT, DELETE, OPTIONS, HEAD</param-value>
        </init-param>
      </filter>
      <filter-mapping>
        <filter-name>CorsFilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>

      <filter>
        <filter-name>login</filter-name>
        <filter-class>com.app.api.filter.AuthenticationFilter</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>login</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>

      <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
      </servlet-mapping>

       <!-- Spring Context Listener -->
      <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>

      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext*.xml</param-value>
      </context-param>

    </web-app>
  

自定义过滤器类: -

package com.app.api.filter;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;

import com.fasterxml.jackson.databind.ObjectMapper;

@WebFilter("/*")
public class AuthenticationFilter implements Filter {
    private static final Logger LOGGER = LoggerFactory.getLogger(AuthenticationFilter.class);

    public AuthenticationFilter() {
        System.out.println("Authentication - default filter !");
    }

    public void destroy() {
        System.out.println("Authentication - destroy !");
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        LOGGER.debug("Request authentication!");
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String uri = httpRequest.getRequestURI();
        if(uri.equals("/app-api/login") || uri.equals("/app-api/logout")) {
            chain.doFilter(request, response);
            return;
        }

        HttpSession session = httpRequest.getSession(false);
        if(session == null || session.getAttribute("user") == null) {
            writeInvalidCredentialResponse((HttpServletResponse) response);
        } else {
            chain.doFilter(request, response);
        }
    }

    private void writeInvalidCredentialResponse(HttpServletResponse response) throws IOException {
        Map<String, String> errorResponse = new HashMap<String, String>();
        errorResponse.put("message", "Please login with right credentials!");
        ObjectMapper mapper = new ObjectMapper();
        String responseMessage = mapper.writeValueAsString(errorResponse);
        LOGGER.debug("Invalid request authentication!");
        LOGGER.debug(responseMessage);
        response.getWriter().write(responseMessage);
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    }

    public void init(FilterConfig fConfig) throws ServletException {
        System.out.println("Authentication - init !");
    }

}
  

项目层次结构: -

     
    

A - 父maven模块

         

B - RESTful webservices子模块(包含 - /WEB-INF/web.xml,/WEB-INF/dispatcher-servlet.xml)

         

C - 持久层子模块(包含 - /resources/applicationContext.xml,/resources/persistence.xml)

  

请帮我解决这个问题,使我的弹簧上下文运行起来(ui - webservices和persistence) 非常感谢提前!

  

应用程序的API   /app-api/src/main/webapp/WEB-INF/dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">     
    <import resource="classpath:/applicationContext-data.xml" />
    <context:annotation-config />
    <context:component-scan base-package="com.app" />
    <context:property-placeholder location="classpath:*.properties" />
    <mvc:annotation-driven/>    
</beans>
  

应用的数据   /app-data/src/main/resources/applicationContext-data.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd 
    http://www.springframework.org/schema/jdbc 
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd 
    http://www.springframework.org/schema/data/jpa 
    http://www.springframework.org/schema/data/jpa/spring-jpa-2.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- ************* JPA/Hibernate configuration ************* -->
    <bean 
        name="entityManagerFactory" 
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.app.data.*" />
        <property name="persistenceXmlLocation" value="classpath:persistence.xml" />
        <property name="persistenceUnitName" value="persistence-unit" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
    </bean>
    <bean 
        id="transactionManager" 
        class="org.springframework.orm.jpa.JpaTransactionManager" 
        p:entityManagerFactory-ref="entityManagerFactory" />

    <bean 
        id="dataSource" 
        class="org.apache.commons.dbcp.BasicDataSource"
        p:driverClassName="com.mysql.jdbc.Driver" 
        p:url="jdbc:mysql://localhost:3306/app"
        p:username="root" 
        p:password="root123" 
        p:initialSize="5"
        p:maxActive="10">
    </bean>
<context:component-scan base-package="com.app.data.*" />
    <tx:annotation-driven />
</beans>    

2 个答案:

答案 0 :(得分:0)

尝试删除@WebFilter并确保您的init方法覆盖,因为我们无法在提供的代码中看到它。

答案 1 :(得分:0)

让我们看看例外:

java.lang.ClassNotFoundException: com.adobe.ssp.api.filter.AuthenticationFilter

您的类路径出现问题,找不到类“ com.adobe.ssp.api.filter.AuthenticationFilter ”。请确保此课程包含在: YOUR_APP \ WEB-INF \ classes YOUR_APP \ WEB-INF \ lib