我有一個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