Spring Data Geode 1.0.0.INCUBATING-RELEASE 已發布

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

我非常高興且興奮地代表SpringApache Geode 社群宣布發布 Spring Data for Apache Geode 1.0.0-incubating

您可以從 Maven Central 取得程式碼,方法是在您的應用程式 Maven POM 或 Gradle 建置檔案中包含以下相依性...

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) 的畢業。

但是,這還不是全部!

安全性!

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

這意義重大,因為 Apache Geode 是少數幾個提供安全性的 OSS IMDG 選項之一,而無需企業授權!

此新功能的最佳之處之一是它是一個框架,允許插入不同的安全提供者。Geode 開箱即用,基於 Apache Shiro,它提供了一種熟悉且強大的方法來配置安全性,不僅適用於 Geode,也適用於您的應用程式。

如何保護 Apache Geode 的安全

如果沒有 Spring (Data Geode)Apache Geode 會提供自己的選項來配置安全性。

一種選擇是實作 Apache Geode SecurityManager 介面並 設定 對應的 Geode security-manager (系統) 屬性為完全限定的類別名稱。可以在 此處 看到一個範例。

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

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

首先,Apache Geode 目前僅支援 classpath: 資源指定符(Geode 工程團隊也在 解決)。其次,必須學習另一種配置檔格式,無論它有多 標準,都比 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 代理實作

另一種選項是建立一個 Proxy 來實作 Geode SecurityManager 介面,這個 Proxy 會委派給一個實際的、底層的 Geode SecurityManager,該 SecurityManager 已在 *Spring* 容器或其他受管理環境 (例如 Pivotal *CloudFoundry*) 中設定並注入。

在 RI 這裡 可以看到一個這樣的 Proxy 實作範例,並且配置如下...

@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 使用 BeanFactoryLocator (如 參考指南 (和 這裡) 中所述) 來配置和自動裝配在 *Spring* 容器外部建構和初始化的物件,例如由 *Apache Geode* 建構的物件。

這在以下情況非常有用:應用程式物件 (例如 CacheLoader) 可能已在 Geode 的原生 cache.xml 配置中定義,並且需要使用在 *Spring* 容器中定義的 bean (例如 DataSource) 進行自動裝配。 這也適用於 Geode (系統) 屬性中引用的物件,例如 SecurityManager

SecurityManagerProxy 必須繼承 LazyWiringDeclarableSupport 類別,這使得 Proxy 能夠在使用 Geode 建構物件後,透過 *Spring* 容器使用 BeanFactoryLocator 進行自動裝配。 實際上非常巧妙。

您可以在 RI 這裡 看到完整的範例配置。 這也需要在 Geode Server、*Spring Boot* 應用程式類別上將 useBeanFactoryLocator 屬性設定為 true 這裡,如上面的範例所示。

Apache Shiro INI 配置文件

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

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

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

同樣,*Apache Shiro* INI 文件必須位於 classpath 中。 由於目前的 *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 等),您的應用程式使用這些 Realms 來存取其安全元資料。

如果您定義了多個 Shiro Realm,您甚至可以使用 *Spring's* @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's* 驗證序列 中使用的 *驗證策略* 的一個重要因素。

您可以在 RI 這裡 看到使用 Shiro Realms 的多個範例配置。

下一步是什麼

我們涵蓋了很多內容,但仍然有更多工作要做。 具體來說,我打算做以下事情...

  • 將 *Apache Geode's* *整合安全性* 框架與 Spring Security 整合。
  • 改進 *Spring Boot's* 對 SDG Repositories 的自動配置支持,並將 *Apache Geode* 自動配置為使用 *Spring's* 快取抽象快取提供者
  • 擴展 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 社群中所有即將舉行的活動。

查看全部