Search code examples
javaspring-mvctomcatmaven-3applicationcontext

Tomcat/ApplicationContext not able to find out Filter class


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>

Custom filter class:-

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 !");
    }

}

Project hierarchy:-

A - parent maven module

B - RESTful webservices sub module (contains - /WEB-INF/web.xml, /WEB-INF/dispatcher-servlet.xml)

C - persistence layer sub module (contains - /resources/applicationContext.xml, /resources/persistence.xml)

Please help me to resolve this issue to make my both spring context up and running (ui - webservices and persistence) Many thanks in advance !

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

Solution

  • Let's see the exception:

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

    There was a problem with your classpath, the class "com.adobe.ssp.api.filter.AuthenticationFilter" was not found. Please make sure this class is contained in: YOUR_APP\WEB-INF\classes or YOUR_APP\WEB-INF\lib