없이 스프링 MVC 응용 프로그램에서 서블릿 필터를 등록하는 방법은 내 응용 프로그램 아래의 응답을 느리게하는 간단한 서블릿 필터를 만든 : 나는 같은 앱을 등록하는 기사의 무리를 읽을 는 이니셜
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class DelayFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {}
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
Integer seconds = 10;
try {
Thread.sleep(seconds * 1000);
} catch (InterruptedException e) {
throw new ServletException("Interrupted!");
}
HttpServletResponse response = (HttpServletResponse) resp;
response.setHeader("Cache-Control", "no-cache, must-revalidate");
chain.doFilter(req, resp);
}
@Override
public void destroy() {}
}
this :
enter link description here 일반적으로 두 가지 등록 방법이 있습니다. 하나는 web.xml을 사용하고 다른 하나는 프로그래밍 방식 구성입니다. 내가 사용해야하는 응용 프로그램은 XML을 사용하지 않지만 어느 이니셜 라이저 클래스도 가지고 있지 않습니다. 구성 클래스는 다음과 같이 시작하는 일 구성 표준시 : 그래서 내가 초기화를 만들어
@Configuration
@EnableWebMvc
@EnableAsync
@EnableTransactionManagement
@EnableSpringConfigured
@PropertySource("classpath:config/application.properties")
public class ApplicationConfiguration extends WebMvcConfigurerAdapter {
수입 javax.servlet.Filter;
public class arachneInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{ApplicationConfiguration.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
@Override
protected Filter[] getServletFilters() {
return new Filter[] {
new DelayFilter()
};
}
}
이것이 맞거나 내 응용 프로그램의 작동 방식이 바뀌면 확실하지 않습니다. 모든 것이 정상적인 것으로 보입니다. 하지만 필터가 작동하지 않습니다! 내가 잘못했거나 필터를 어떻게 추가 할 수 있는지에 대한 아이디어는 Initializer가 없을 수도 있습니다.
편집 : Spring MVC 4.3.4를 사용합니다.
스프링 부트 또는 다른 것을 사용하고 있습니까? 초기화 도구를 만들기 전에 구성 클래스가 어떻게 선택 되었습니까? – zero01alpha
필터 클래스에 @WebFilter와 같은 anotation이 필요하지 않습니까? – Novy