Spring Data Geode 1.0.0.INCUBATING-RELEASE 釋出

工程 | John Blum | 2016 年 11 月 10 日 | ...

我非常高興地代表 SpringApache Geode 社群宣佈,面向 Apache Geode 1.0.0-incubatingSpring Data 已釋出。

您可以透過在應用的 Maven POM 或 Gradle 構建檔案中包含以下依賴項來從 Maven Central 獲取相關檔案...

Maven

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-geode</artifactId>
  <version>1.0.0.INCUBATING-RELEASE</version>
</dependency>

Gradle

compile 'org.springframework.data:spring-data-geode:1.0.0.INCUBATING-RELEASE'

包含 spring-data-geode 依賴項將傳遞地引入所有必需的 Apache Geode 工件,因此您現在可以開始構建使用 Apache GeodeSpring 應用了。

注意

我再次更改了版本限定符,移除了 APACHE-GEODE 限定,並簡化為 INCUBATING-RELEASE。一旦 Apache Geode 從孵化器畢業,INCUBATING 限定也將取消,版本號將簡單地變成 major.minor.maint.[M#|RC#|RELEASE]

新特性

Spring Data Geode 1.0.0.INCUBATING-RELEASEApache Geode 1.0.0-incubating 的釋出都意義重大,原因如下:

首先,也是最重要的是,這標誌著 Apache GeodeApache Software Foundation (ASF) 內的第一個 官方 GA 版本釋出。這不僅標誌著 Geode 的成熟(其根基在於十多年的生產經驗,即 Pivotal GemFire),也加速了其在 ASF 內畢業成為頂級專案 (TLP) 的程序。

但這還不是全部!

安全!

此版本還透過引入一個新的整合安全框架(一些技術細節可在此處檢視)對 Apache Geode 的安全模型進行了重大更改,該框架不僅包括安全傳輸(即 SSL),還包括身份驗證和授權。

這一點意義重大,因為 Apache Geode 是少數提供安全功能而無需企業許可證的開源 IMDG 選項之一!

這項新功能最出色的方面之一是它是一個框架,允許插入不同的安全提供者。開箱即用,Geode 構建在 Apache Shiro 之上,它為 Geode 和您的應用提供了熟悉且強大的安全配置方式。

如何保護 Apache Geode 的安全

如果不使用 Spring (Data Geode)Apache Geode 提供自己的安全配置選項。

一種選擇是實現 Apache GeodeSecurityManager 介面,並將其完全限定類名設定到相應的 Geode security-manager (系統)屬性中。示例可在此處檢視。

但是,使用屬性引用 FQCN 嚴重限制了您在託管環境或測試上下文中配置 SecurityManager 的方式。根據我的反饋,這將在稍後的 Geode 版本中得到解決

另一種選擇是使用 Apache Geodesecurity-shiro-init (系統)屬性來指定位於 Apache Shiro 支援的指定資源路徑中的 INI 配置檔案。然而,這有兩個限制。

首先,目前 Apache Geode 僅支援 classpath: 資源指定符(Geode 工程團隊也正在解決此問題)。其次,IMO,學習另一種配置檔案格式,無論它多麼標準,都不比 XML 好多少。

當然,Apache Shiro 試圖透過提供此功能來減輕在 Spring 上下文中執行時遇到的麻煩。但是,仍然有太多樣板配置邏輯需要改進。

如何使用 Spring (Data Geode) 保護 Apache Geode 的安全

為了使 Apache Geode 儘可能地快速易於使用(請參閱我的上一篇部落格文章),我一直與 Geode 工程團隊緊密合作,改進初始設計,並透過採用 Spring FrameworkSpring Boot 推廣的許多基礎 API 和框架設計概念,真正將整合安全Spring Data Geode 中打造成為一級公民。

因此,我向您介紹了 SDG 新的基於註解的配置模型中的 @EnableSecurity 註解。您可以使用該註解以多種方式配置 Apache Geode 的安全功能。

SecurityManager 類名引用

您仍然可以透過其完全限定類名引用 Geode SecurityManager 實現,使用...

package example;

class ExampleSecurityManager 
    implements org.apache.geode.security.SecurityManager {
  ...
}

@CacheServerApplication(name = "ClassNameExample")
@EnableSecurity(securityManagerClassName = "example.ExampleSecurityManager")
class ExampleApplication {
  ...
}

更詳細的示例可以在 SDG Contacts Application RI 中此處看到。

但是,您必須提供一個預設的無參建構函式,並且您的 Geode SecurityManager 實現將負責在構建時載入所有安全身份驗證/授權詳細資訊;這不是很理想。

SecurityManager 代理實現

另一種選擇是建立一個實現 Geode SecurityManager 介面的 Proxy,它將委託給一個實際的、底層 Geode SecurityManager,該 SecurityManagerSpring 容器或其他託管環境(如 Pivotal CloudFoundry)中配置並由其注入。

一個這樣的 Proxy 實現在 RI 中可以此處看到,配置如下...

@CacheServerApplication(name = "ProxyExample")
@EnableSecurity(securityManagerClassName = 
  "example.app.geode.security.SecurityManagerProxy", 
  useBeanFactoryLocator = true)
class ExampleApplication {

    ...

    @Bean
    JdbcSecurityRepository securityRepository(JdbcTemplate template) {
      return new JdbcSecurityRepository(template);
    }

    @Bean
    SimpleSecurityManager securityManager(
        SecurityRepository<User> securityRepository) {

      return new SimpleSecurityManager(securityRepository);
    }
}

SecurityMangerProxy 在快取初始化期間由 Apache Geode 構建。Spring 容器將找到 SimpleSecurityManager Bean 定義,並將其注入到 SecurityManagerProxy 中。

SecurityManagerProxy 利用了 Spring 的另一個特性,即 BeanFactoryLocator,如參考指南(和此處)所述,SDG 使用它來配置和自動裝配在 Spring 容器外部構建和初始化的物件,例如由 Apache Geode 構建和初始化的物件。

這在某些情況下很有用,例如應用程式物件(如 CacheLoader)可能在 Geode 的原生 cache.xml 配置中定義,並且需要與 Spring 容器中定義的 Bean(如 DataSource)自動裝配。這對於在 Geode (系統) 屬性中引用的物件(如 SecurityManager)也適用。

SecurityManagerProxy 必須繼承 LazyWiringDeclarableSupport 類,這使得一旦 Geode 構建了該物件,Spring 容器就可以使用 BeanFactoryLocator 自動裝配 Proxy。這實際上非常巧妙。

您可以在 RI 此處看到完整的示例配置。這還需要在 Geode Server 的 Spring Boot 應用程式類上將 useBeanFactoryLocator 屬性設定為 true,這在上面的示例中也有展示。

Apache Shiro INI 配置檔案

也許您不想讓應用程式程式碼不必要地與 Geode 的專有類和介面(例如 SecurityManager)耦合。也許您只是想充分利用 Apache Shiro 的安全框架。

一種方法是建立一個 Apache Shiro INI 配置檔案,並在 @EnableSecurity 註解中引用它,如下所示...

@CacheServerApplication(name = "ProxyExample")
@EnableSecurity(shiroIniResourcePath = "my-shiro.ini")
class ExampleApplication {
    ...
}

同樣,Apache Shiro INI 檔案必須在類路徑上。由於目前 Apache Geode限制,無法使用其他資源指定符(例如 file:url:)。

這個完整的示例配置可以在此處看到。

Apache Shiro Realms

然而,作為應用程式開發人員,您真正想做的是僅在 Spring 容器中將 Apache Shiro Realms 定義為 Spring Bean,以便訪問應用程式保護 Apache Geode 所需的安全元資料,然後讓 Spring 完成所有工作。

嗯,SDG 也可以為您做到這一點。例如...

@CacheServerApplication(name = "RealmExample")
@EnableSecurity
class ExampleApplication {

    @Bean
    PropertiesRealm shiroRealm() {
      PropertiesRealm propertiesRealm = new PropertiesRealm();
      propertiesRealm.setResourcePath("classpath:shiro.properties");
      propertiesRealm.setPermissionResolver(new GeodePermissionResolver());
      return propertiesRealm;
    }
  }

就是這樣;這就是您所需要的全部。

注意 Shiro 的 PropertiesRealm 使用 GeodePermissionResolver 來解析 Geode 許可權。此外,您可以選擇指定任何資源路徑;您不受限於只能使用 classpath:

您還可以自由定義應用程式用於訪問其安全元資料的 Shiro 提供的任何 Realms(例如 JDBC、JNDI、LDAP 等)。

如果您定義了多個 Shiro Realm,您甚至可以使用 Spring@Order 註解在 Realm Bean 定義上對它們進行排序,如下所示...

@CacheServerApplication(name = "OrderedMultiRealmExample")
@EnableSecurity
class ExampleApplication {

    @Bean
    @Order(1)
    IniRealm iniRealm() {
      IniRealm iniRealm = new IniRealm("classpath:partial-shiro.ini");
      iniRealm.setPermissionResolver(new GeodePermissionResolver());
      return iniRealm;
    }

    @Bean
    @Order(2)
    PropertiesRealm propertiesRealm() {
      PropertiesRealm propertiesRealm = new PropertiesRealm();
      propertiesRealm.setResourcePath("classpath:partial-shiro.properties");
      propertiesRealm.setPermissionResolver(new GeodePermissionResolver());
      return propertiesRealm;
    }
}

Realm 排序是 Apache Shiro 身份驗證序列中使用的身份驗證策略中的一個重要因素。

您可以在 RI 此處看到使用 Shiro Realms 的多個示例配置。

下一步是什麼

我們已經講了很多內容,但仍有很多工作要做。具體來說,我打算做以下工作...

  • Apache Geode整合安全框架與 Spring Security 整合。
  • 改進 Spring Boot 對 SDG Repositories 的自動配置支援,並使用 Spring快取抽象Apache Geode 自動配置為快取提供者
  • 擴充套件 SDG 的註解配置支援,根據實體和/或 Repository Bean 定義動態建立 Geode 快取區域 (Region)

還有更多工作正在進行中,敬請關注。

其他釋出亮點

  • 將基礎 Java 版本設定為 Java 8
  • 升級到 Spring Framework 4.3.4.RELEASE。
  • 升級到 Spring Data Commons 1.12.5.RELEASE。

更多詳細資訊請參閱變更日誌

結論

一如既往,歡迎您的反饋,您可以透過 JIRAStackOverflow 與我們聯絡。

感謝大家!編碼愉快。

獲取 Spring 資訊

訂閱 Spring 資訊,保持聯絡

訂閱

領先一步

VMware 提供培訓和認證,助您快速提升。

瞭解更多

獲取支援

Tanzu Spring 透過一個簡單的訂閱提供 OpenJDK™、Spring 和 Apache Tomcat® 的支援和二進位制檔案。

瞭解更多

近期活動

檢視 Spring 社群的所有近期活動。

檢視全部