2016-10-25 88 views
2

我有一个Spring-Boot应用程序打包为tomcat依赖提供的战争(所以我有两个选项 - 启动后使用打包的.war在嵌入式容器中运行它通过java -jar命令并且还可以在独立的servlet容器上运行)。拦截器没有得到初始化,并用SpringBoot调用

下面是我的应用程序的主类

package com.mycompany.edsa.dgv.proxysvc; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.boot.builder.SpringApplicationBuilder; 
import org.springframework.boot.web.support.SpringBootServletInitializer; 
import org.springframework.context.annotation.Bean; 
//import org.springframework.context.annotation.Import; 
import org.springframework.context.annotation.ImportResource; 
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 

import com.mycompany.edsa.dgv.proxysvc.interceptor.DGVProxySvcRequestInterceptor; 

//import com.mycompany.edsa.dgv.proxysvc.config.ConfigExtension; 

@SpringBootApplication 
@ImportResource("classpath:dgv-proxy-svc-spring-ctx.xml") 
//@Import(ConfigExtension.class) 
public class DGVProxySvcAppMain extends SpringBootServletInitializer { 

public static void main(String[] args) { 
    SpringApplication.run(DGVProxySvcAppMain.class, args); 
} 

@Override 
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
    return application.sources(DGVProxySvcAppMain.class); 
} 

@Bean 
public DGVProxySvcRequestInterceptor dgvProxySvcRequestInterceptor() { 
    DGVProxySvcRequestInterceptor dgvProxySvcReqInterceptor = new DGVProxySvcRequestInterceptor(); 
    return dgvProxySvcReqInterceptor; 
} 


@Bean 
public WebMvcConfigurerAdapter adapter() { 
    return new WebMvcConfigurerAdapter() { 
     @Override 
     public void addInterceptors(InterceptorRegistry registry) { 
      System.out.println("Adding interceptors"); 
      registry.addInterceptor(dgvProxySvcRequestInterceptor()).addPathPatterns("/*"); 
      super.addInterceptors(registry); 
     } 
    }; 
} 

}

我下面的拦截器类:

package com.mycompany.edsa.dgv.proxysvc.interceptor; 

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import org.springframework.stereotype.Component; 
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; 

@Component 
public class DGVProxySvcRequestInterceptor extends HandlerInterceptorAdapter { 

    @Override 
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 

     /* Put in the code here to validate user principal before passing control to the controller */ 

     //BXPPrincipal principal = (BXPPrincipal)SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 
     //Map<String, ?> result = principal.getAttributes(); 
     System.out.println("Inside DGVProxySvcRequestInterceptor..."); 
     return super.preHandle(request, response, handler); 
    } 

} 

然而,在启动应用程序(我启动通过spring-boot:run Maven的目标,并使用Eclipse IDE),我没有看到我的拦截器在控制台日志中注册。 我试图在public void addInterceptors(InterceptorRegistry registry)方法断点启动在调试模式下,我看到的是控制需求,在这一点上和系统输出消息"Adding interceptors"获得登录控制台,也registry包含了我在其封装的ArrayList DGVProxySvcRequestInterceptor但不知道为什么有控制台上没有提及任何有关此拦截器bean初始化的消息。另外,在调用我的服务时,我没有看到sysout消息"Inside DGVProxySvcRequestInterceptor..."已被放入我的拦截器类'preHandle方法(确认拦截器未被调用)。

有人可以帮我找出我可能在做什么配置错误吗?

请注意 - 我试着用WebMvcConfigurerAdapter而不是SpringBootServletInitializer扩展我的主类,并重写addInterceptors方法来添加我的拦截器。在这种情况下,我的拦截器得到了初始化(我可以在控制台日志中看到),并且在调用我的服务uri时也会被调用。 所以这告诉我,当我尝试使用SpringBootServletInitializer(我必须使用这个初始值设定项,因为我需要将我的代码打包为可以在独立的servlet容器上运行的战争)时,我的配置有些不正确。

在此先感谢您的任何建议/指针!

回答

0

找到了修复程序。只好用ant像URL模式相匹配的请求:

registry.addInterceptor(dgvProxySvcRequestInterceptor()).addPathPatterns("/**"); 

我原来的配置是所有好;除了上面的url模式外,不需要任何改变。

0
@Configuration 
@EnableWebMvc 
public class AppConfig extends WebMvcConfigurerAdapter { 
    // @Bean resolvers , etc 

    @Override 
    public void addInterceptors(InterceptorRegistry registry) { 

     registry.addInterceptor(new DGVProxySvcRequestInterceptor()).addPathPatterns("/controller/action/*"); 
    } 
} 
+0

我相信使用@EnableWebMvc将避免使用Spring的自动配置,我猜。是否没有解决方法来添加拦截器而不覆盖自动配置? – lbvirgo

+0

另外,尝试了以上建议;没有帮助。首先调用控制器,然后调用拦截器! – lbvirgo

+0

您是否正确替换了您的动作网址“addPathPatterns” – kuhajeyan

0

我有同样的问题。 @SpringBootApplication只扫描当前包中的所有组件。为了扫描其他包装,我们需要在@ComponentScan中指定包装。 对于实施例

package com.main; 
@SpringBootApplication 
public class Application { 
    //Code goes here. 
} 

现在我想拦截是寄存器,这既不在com.maincom.main.**,其在com.test封装。

package com.test.interceptor 
@Configuration 
public class InterceptorConfig extends WebMvcConfigurerAdapter { 
    @Autowired 
    HeaderInterceptor headerInterceptor; 

    @Override 
    public void addInterceptors(InterceptorRegistry registry) { 
     registry.addInterceptor(headerInterceptor); 
    } 
} 

在上述情况下,Spring Boot将不会在上下文中注册HeaderInterceptor。为了注册,我们必须明确扫描该软件包。

package com.main; 
@SpringBootApplication 
@ComponentScan("com.*") //Which takes care all the package which starts with com. 
@ComponentScan({"com.main.*","com.test.*"}) //For specific packages 
public class Application { 
    //Code goes here. 
}