我有一个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容器上运行的战争)时,我的配置有些不正确。
在此先感谢您的任何建议/指针!
我相信使用@EnableWebMvc将避免使用Spring的自动配置,我猜。是否没有解决方法来添加拦截器而不覆盖自动配置? – lbvirgo
另外,尝试了以上建议;没有帮助。首先调用控制器,然后调用拦截器! – lbvirgo
您是否正确替换了您的动作网址“addPathPatterns” – kuhajeyan