2016-10-25 87 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. 
}