整合圖形控制器

如果您的應用程式是基於 Web 的(或建立在具有嵌入式 Web 容器的 Spring Boot 之上),並且 Spring Integration HTTP 或 WebFlux 模組(請參閱 HTTP 支援WebFlux 支援,分別)存在於類別路徑中,您可以使用 IntegrationGraphControllerIntegrationGraphServer 功能公開為 REST 服務。為此,HTTP 模組中提供了 @EnableIntegrationGraphController@Configuration 類別註解以及 <int-http:graph-controller/> XML 元素。與 @EnableWebMvc 註解(或 XML 定義的 <mvc:annotation-driven/>)一起,此配置會註冊 IntegrationGraphController @RestController,其 @RequestMapping.path 可以在 @EnableIntegrationGraphController 註解或 <int-http:graph-controller/> 元素上配置。預設路徑為 /integration

IntegrationGraphController @RestController 提供以下服務

  • @GetMapping(name = "getGraph"):用於檢索自上次 IntegrationGraphServer 重新整理以來 Spring Integration 元件的狀態。o.s.i.support.management.graph.Graph 作為 REST 服務的 @ResponseBody 回傳。

  • @GetMapping(path = "/refresh", name = "refreshGraph"):用於重新整理目前 Graph 以取得實際執行時期狀態,並將其作為 REST 回應回傳。對於指標而言,不需要重新整理圖形。指標會在檢索圖形時即時提供。如果應用程式上下文自上次檢索圖形後已修改,則可以呼叫重新整理。在這種情況下,圖形會完全重建。

您可以使用 Spring Security 和 Spring MVC 專案提供的標準配置選項和元件,為 IntegrationGraphController 設定安全性及跨來源限制。以下範例實現了這些目標

<mvc:annotation-driven />

<mvc:cors>
	<mvc:mapping path="/myIntegration/**"
				 allowed-origins="https://127.0.0.1:9090"
				 allowed-methods="GET" />
</mvc:cors>

<security:http>
    <security:intercept-url pattern="/myIntegration/**" access="ROLE_ADMIN" />
</security:http>


<int-http:graph-controller path="/myIntegration" />

以下範例示範如何使用 Java 配置執行相同的操作

@Configuration
@EnableWebMvc // or @EnableWebFlux
@EnableWebSecurity // or @EnableWebFluxSecurity
@EnableIntegration
@EnableIntegrationGraphController(path = "/testIntegration", allowedOrigins="https://127.0.0.1:9090")
public class IntegrationConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
	    http
            .authorizeRequests()
               .antMatchers("/testIntegration/**").hasRole("ADMIN")
            // ...
            .formLogin();
    }

    //...

}

請注意,為了方便起見,@EnableIntegrationGraphController 註解提供了 allowedOrigins 屬性。這提供了對 pathGET 存取權。為了更精細的設定,您可以使用標準 Spring MVC 機制來配置 CORS 對應。