본문 바로가기
Spring

[Spring] 조건에 따라 필터를 따로 분기해야 할경우. DEXT + SPRING 이미지 업로드시 오류 발생

by Real Iron 2019. 2. 13.

 WebApplicationInitializer


   @Override

    public void onStartup(ServletContext container) {


    // for dexteditor

    AnnotationConfigWebApplicationContext dextContext = new AnnotationConfigWebApplicationContext();

    //dextContext.setConfigLocation(CONFIG_LOCATION);

        //container.addListener(new ContextLoaderListener(dextContext));

        DispatcherServlet dextServlet =  new DispatcherServlet(dextContext);

        dextServlet.setThrowExceptionIfNoHandlerFound(true);

        ServletRegistration.Dynamic dextDispatcher = container.addServlet("DextDispatcher", dextServlet);

        dextDispatcher.setLoadOnStartup(1);

        dextDispatcher.addMapping(MAPPING_URL_DEXT);


    // for main dispatcher

        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();

        context.setConfigLocation(CONFIG_LOCATION);

        container.addListener(new ContextLoaderListener(context));

        DispatcherServlet dispatcherServlet =  new DispatcherServlet(context);

        dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);

        ServletRegistration.Dynamic dispatcher = container.addServlet("SpringDispatcher", dispatcherServlet);

        dispatcher.setLoadOnStartup(1);

        dispatcher.addMapping(MAPPING_URL);

        dispatcher.setMultipartConfig(new MultipartConfigElement(getEnv("file.temp.dir"), Long.parseLong(getEnv("file.max.size")), Long.parseLong(getEnv("file.max.requestsize")), Integer.parseInt(getEnv("file.mem.size"))));

        FilterRegistration.Dynamic multipartFilter = container.addFilter("springMultipartFilter", CustomMultipartFilter.class);

        multipartFilter.addMappingForUrlPatterns(null, true, "/*");


        initLogging(container);

        //System.setProperty("logback.configurationFile", "classpath:/config/logback.xml");

        //initCipherKey();

    }






filter


package com.gsc.filter;


import java.io.IOException;

import java.lang.invoke.MethodHandles;


import javax.servlet.FilterChain;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Component;

import org.springframework.web.multipart.support.MultipartFilter;


@Component

public class CustomMultipartFilter extends MultipartFilter {


final protected static Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass().getSimpleName());


@Override

    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

logger.debug("Call CustomMultipartFilter doFilterInternal...");

        request.setCharacterEncoding("UTF-8");


        if(getRequestURI(request).endsWith("dext"))

        {

        logger.debug("doFilterInternal destroy...");

        request.getRequestDispatcher(((HttpServletRequest) request).getServletPath()).forward(request, response);

}

        else

        {

        super.doFilterInternal(request, response, filterChain);

        }

    }


public String getRequestURI(HttpServletRequest request){

logger.debug("call CustomMultipartFilter getRequestURI...");

        return (request.getRequestURI());

    }

}