0

So basically I have put up my backend on Render web Service (on tomcat) and my frontend(Nextjs) is on Vercel. I have put up a Filter for Cors Policy to intercept all policies and as well as handled OPTIONS request. In my local setup, when I send requests from port 3000 to port 8080(tomcat), my requests does hit my backend, but when i deploy and try to send requests from vercel to render, I keep getting "Response to preflight request didnt attach allow origin header", I have attached it in my filter and im using annotations. I tried with both Annotations and web.xml but none seem to work.I binded tomcat to Port 10000 on Render.

This is my DOCKERFILE:

# ===== Stage 1: Build with Maven =====
FROM maven:3.8.5-openjdk-8 AS builder
WORKDIR /app

# Copy pom.xml and download dependencies first (better cache)
COPY pom.xml .
RUN mvn dependency:go-offline -B

# Copy source and build WAR
COPY src ./src
RUN mvn -B clean package -DskipTests


# ===== Stage 2: Tomcat runtime =====
FROM openjdk:8-jdk-alpine

# Install wget and unzip
RUN apk add --no-cache wget bash tar

# Set Tomcat environment
ENV TOMCAT_VER=9.0.80
ENV CATALINA_HOME=/usr/local/tomcat
ENV PATH=$CATALINA_HOME/bin:$PATH

# Download Tomcat from archive (since 9.0.80 is old)
RUN wget https://archive.apache.org/dist/tomcat/tomcat-9/v$TOMCAT_VER/bin/apache-tomcat-$TOMCAT_VER.tar.gz \
    && mkdir -p $CATALINA_HOME \
    && tar xzf apache-tomcat-$TOMCAT_VER.tar.gz -C $CATALINA_HOME --strip-components=1 \
    && rm apache-tomcat-$TOMCAT_VER.tar.gz

# Fix Tomcat port to use Render's PORT environment variable
RUN sed -i "s/port=\"8080\"/port=\"${PORT:-10000}\"/" $CATALINA_HOME/conf/server.xml

# Copy WAR from builder stage
COPY --from=builder /app/target/IssuesandDeploymentTracker-0.0.1-SNAPSHOT.war $CATALINA_HOME/webapps/ROOT.war

# Expose Tomcat port
#EXPOSE 8080

# Run Tomcat
CMD ["catalina.sh", "run"]


and This is my filter:

@WebFilter("/*")
public class CorsFilter implements Filter {

    public CorsFilter() {
        super();
        System.out.println(">>> CorsFilter constructor loaded, class = " + this.getClass().getName());

    }
    public void destroy() {
    }

    public void doFilter(ServletRequest request, ServletResponse resp, FilterChain chain)
            throws IOException, ServletException {

        HttpServletResponse response = (HttpServletResponse) resp;
        HttpServletRequest req = (HttpServletRequest) request;
        System.out.println("CorsFilter hit for: " + req.getRequestURI());

        response.setHeader("Access-Control-Allow-Origin","https://issues-deployment-tracker.vercel.app");
        response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");

        // Handle preflight immediately
        if ("OPTIONS".equalsIgnoreCase(req.getMethod())) {
            response.setStatus(HttpServletResponse.SC_OK);
            return;
        }

        // pass the request along the filter chain
        chain.doFilter(request, response);
    }

    /**
     * @see Filter#init(FilterConfig)
     */
    public void init(FilterConfig fConfig) throws ServletException {
        // TODO Auto-generated method stub
    }

}

I have used Hibernate, Postgres for prod, maven build. **This is my pom.xml for reference: ** yes the urls used in frontend to send requests are correct as well. My render service goes live but doesnt receive anything. no logs except healthchecks. Github repo for further reference

I have no idea whats going wrong at this point.

I tried with both Annotations and web.xml but none seem to work. I binded tomcat to Port 10000 on Render. requests dont even hit the filter forget servlets. Using annotations currently. No spring, just plain Hibernate, JPA Daos and Servlets. I have not tried with proxy in next config since I heard its only for local dev.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.