user717236
user717236

Reputation: 5039

Tomcat application Web.xml custom filter giving error of resource has moved; filter redirects https to http

FYI: I'm running Windows Server 2008 R2, with IIS 7.5 and Apache Tomcat 5.5. IIS talks to Tomcat, via the AJP Connector, version 1.3. Tomcat is SSL enabled; so, the web site is enabled for both HTTP and HTTPS traffic. I also have a security constraint in the web.xml file to redirect to enforce HTTPS traffic for a particular file, via the url-pattern.

Having said all that, I have created a servlet filter that is supposed to redirect https to http. Unfortunately, when I restart the tomcat server and type in the login page, I get the following error:

The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

I created the class in Eclipse and exported it as a JAR file called RedirectToHTTP.jar, and I placed this file in the WEB-INF/lib directgory of the application, NOT in the Tomcat installation directory.

I don't know what the problem is and I would appreciate any feedback. Thank you.

Update

Upon recommendation in the comments, I checked the log files found an error related to my filter

-- Exception starting filter redirectFilter

java.lang.UnsupportedClassVersionError: myfilterpkg/RedirectToHTTPFilter : Unsupported major.minor version 51.0.

I compiled the filter with JRE 1.7. I think Tomcat might be using a lower version of JRE. Could that be the problem?

Here is the relevant data in the web.xml file:

<filter>
    <filter-name>UTF8Encoder</filter-name>
     <filter-class>com.remedy.arsys.support.UTF8EncodingFilter</filter-class>
     <init-param>
         <param-name>Param1</param-name>
         <param-value>0</param-value>
     </init-param>
</filter>

<filter>
    <filter-name>redirectFilter</filter-name>
    <filter-class>myfilterpkg.RedirectToHTTPFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>UTF8Encoder</filter-name>
    <servlet-name>*.jsp</servlet-name>
</filter-mapping>

<filter-mapping>
    <filter-name>redirectFilter</filter-name>
    <url-pattern>/index.jsp</url-pattern>
</filter-mapping>

Here is the Java class:

package myfilterpkg;

import java.io.IOException;  

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.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.servlet.http.HttpSession; 

@SuppressWarnings("unused")
public class RedirectToHTTPFilter implements Filter {

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        if ((request instanceof HttpServletRequest) && (response instanceof HttpServletResponse)) {
            String redirectTarget = ((HttpServletRequest)request).getRequestURL().toString().replaceFirst("https", "http");

            if(request.isSecure()) {
                ((HttpServletResponse)response).sendRedirect(redirectTarget);
            }
            else {
                chain.doFilter(request, response);
            }
        }
    }

    @Override
    public void destroy() {
    }

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

Upvotes: 0

Views: 2904

Answers (1)

tbl
tbl

Reputation: 771

The filter must be compiled with the source level of the runtime the tomcat server actually runs in. Otherwise you will get above error when classloading.

Upvotes: 1

Related Questions