Spring @WebFilter在prod上导致404,在本地运行正常

时间:2017-12-31 17:28:02

标签: spring-mvc http-status-code-404 servlet-filters url-pattern

我正在使用带有urlMapping =“/ *”选项的@WebFilter。 (也尝试使用servlet)。我用它来管理我的oAuth单点登录。它在本地与tomcat上的日食运行良好。

在prod上抛出404.禁用过滤器(因此,通过过滤器)工作正常。

有人指点吗?

AppConfig.java:

@Configuration
@EnableWebMvc
@EnableAsync
@ComponentScan(basePackages = "com.my.package")
public class AppConfig extends WebMvcConfigurerAdapter implements AsyncConfigurer {

    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/");
        viewResolver.setSuffix(".html");
        viewResolver.setExposeContextBeansAsAttributes(true);
        return viewResolver;
    }

    @Bean
    public ViewResolver contentNegotiatingViewResolver() {
      ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
      List<ViewResolver> viewResolvers = new ArrayList<ViewResolver>();
      viewResolvers.add(viewResolver());
      resolver.setViewResolvers(viewResolvers);
      return resolver;
}

AppInitializer.java:

public class AppInitializer implements WebApplicationInitializer {

    public void onStartup(ServletContext container) throws ServletException {
        //container.addFilter("name", AccessFilter.class);//Have tried toggling this

        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        //configuration mapping
        ctx.register(AppConfig.class);
        ctx.setServletContext(container);


        ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx));
        servlet.setLoadOnStartup(1);
        servlet.addMapping("/");

    }

}

MyFilter.java:

//@WebFilter(urlPatterns = "/*") //Commenting this out works fine. Problem is surely with the filter.
public class AccessFilter implements Filter {
    SSOHelper ssoHelper;

    public void init(FilterConfig filterConfig) throws ServletException {
        //init the helper and internal property objects
        System.out.println("Filter init for dispatcher servlet");
        ssoHelper = new SSOHelper();
    }
public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;

//This is never hit when applied on prod. Local runs as expected. 
}

0 个答案:

没有答案