第3章 ビジネスアプリケーションの構成

3.1. ビジネスアプリケーションの認証と承認

デフォルトでは、全 REST エンドポイント (/rest/ を含む URL) を保護することで、ビジネスアプリケーションのセキュリティーを確保します。さらに、ビジネスアプリケーションには認証情報のログが 2 セットあり、ユーザーが開発モードで Business Central に接続できるようになっています。ID が user、パスワードが user のユーザーと、ID が kieserver、パスワードが kieserver1! のユーザーがあります。

認証も承認も Spring セキュリティーをもとに行われます。実稼働環境で使用するビジネスアプリケーションはすべて、このセキュリティー設定を変更してください。設定は、<business-application>/<business-application>-services/src/main/java/com/company/service/DefaultWebSecurityConfig.java ファイルで変更できます。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;


@Configuration("kieServerSecurity")
@EnableWebSecurity
public class DefaultWebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .csrf().disable()
        .authorizeRequests()
        .antMatchers("/rest/*").authenticated()
        .and()
        .httpBasic();
    }

  @Autowired
  public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
      PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
      auth.inMemoryAuthentication().withUser("kieserver").password(encoder.encode("kieserver1!")).roles("kie-server")
      .and()
      .withUser("john").password(encoder.encode("john@pwd1")).roles("kie-server", "PM", "HR");
  }
}

3.2. application.propertiesファイルの設定

ビジネスアプリケーションを作成したら、application.properties ファイルで、複数のコンポーネントを設定してアプリケーションをカスタマイズできます。

前提条件

  • business application の Web サイトを使用して作成した <business-application>.zip ファイルがある。

手順

  1. <business-application>.zip ファイルを展開して、<business-application>/<business-application>-service/src/main/resources フォルダーに移動します。
  2. テキストエディターで application.properties ファイルを開きます。
  3. たとえば、以下のように REST エンドポイントのホスト、ポート、およびパスを設定します。

    server.address=localhost
    server.port=8090
    
    cxf.path=/rest
  4. 以下のように、簡単に識別できるように KIE Server (kieserver) を設定します。

    kieserver.serverId=<business-application>-service
    kieserver.serverName=<business-application>-service
    kieserver.location=http://localhost:8090/rest/server
    kieserver.controllers=http://localhost:8080/business-central/rest/controller

    以下の表で、ビジネスアプリケーションに設定可能な KIE Server のパラメーターを一覧で紹介します。

    表3.1 kieserver パラメーター

    パラメーター説明

    kieserver.serverId

    string

    Process Automation Manager コントローラーに接続時にビジネスアプリケーションを識別するために使用する ID

    kieserver.serverName

    string

    Process Automation Manager コントローラーに接続時にビジネスアプリケーションを識別するのに使用する名前。kieserver.serverId パラメーターに使用した文字と同じものを使用できます。

    kieserver.location

    URL

    REST API を使用する他のコンポーネントがこのサーバーの場所を識別するために使用します。 server.address および server.port で定義されている場所は、使用しないでください。

    kieserver.controllers

    URL

    コントローラー URL のコンマ区切りリスト

  5. 非同期実行を有効にするには、以下のように、jbpm.executor.enabled パラメーターの値を true に設定し、他の jbpm.executor パラメーターを安コメントし、必要に応じて値を変更します。

    jbpm.executor.enabled=true
    jbpm.executor.retries=5
    jbpm.executor.interval=0
    jbpm.executor.threadPoolSize=1
    jbpm.executor.timeUnit=SECONDS

    以下の表で、ビジネスアプリケーションに設定可能なエグゼキューターのパラメーターを一覧で紹介します。

    表3.2 エグゼキューターのパラメーター

    パラメーター説明

    jbpm.executor.enabled

    true, false

    エグゼキューターコンポーネントを無効または有効にします。

    jbpm.executor.retries

    整数

    ジョブの実行中にエラーが発生した場合の再試行回数を指定します。

    jbpm.executor.interval

    整数

    エグゼキューターがデータベースの同期にかける時間を指定します。時間の単位は jbpm.executor.timeUnit パラメーターで指定します。デフォルトは無効 (値 0) です。

    jbpm.executor.threadPoolSize

    整数

    スレッドプールのサイズを指定します。

    jbpm.executor.timeUnit

    string

    エグゼキューターがデータベースの同期に費やす間隔を計算するときに使用する時間の単位を指定します。値は、java.util.concurrent.TimeUnit の有効な定数を指定する必要があります。デフォルト値は、SECONDS です。

  6. ビジネスアプリケーションの作成時に Business Automation を選択した場合には、以下のコンポーネントの内、ランタイムに起動するコンポーネントを指定します。

    表3.3 kieserver ケーパビリティーパラメーター

    パラメーター説明

    kieserver.drools.enabled

    true, false

    Decision Manager コンポーネントを有効または無効にします。

    kieserver.dmn.enabled

    true, false

    DMN (Decision Model and Notation) コンポーネントを有効または無効にします。

    kieserver.jbpm.enabled

    true, false

    Red Hat Process Automation Manager コンポーネントを有効または無効にします。

    kieserver.jbpmui.enabled

    true, false

    Red Hat Process Automation Manager UI コンポーネントを有効または無効にします。

    kieserver.casemgmt.enabled

    true, false

    ケース管理のコンポーネントを有効または無効にします。

3.3. Red Hat Single Sign-On を使用したビジネスアプリケーションの設定

Red Hat Single Sign-On (RH SSO) を使用して、サービス間のシングルサインオンを有効化し、一元的にユーザーとロールの設定や管理ができます。

前提条件

  • business applications の Web サイトを使用して作成した <business-application>.zip ファイルがある。

手順

  1. Red Hat シングルサインオン (SSO) をダウンロードし、インストールしている。『Red Hat Single Sign-On Getting Started Guide』を参照してください。
  2. RH SSO を設定します。

    1. デフォルトのマスターレルムを使用するか、新しいレルムを作成します。
    2. springboot-app クライアントを作成して、パブリックに AccessType を追加します。
    3. 以下のように、 ローカルの設定に合わせて有効なリダイレクト URI と Web オリジンを設定します。

      • 有効なリダイレクト URI: http://localhost:8090/*
      • Web オリジン: http://localhost:8090
    4. アプリケーションで使用するレルムロールを作成します。
    5. アプリケーションで使用するユーザーを作成してロールを割り当てます。
  3. サービスプロジェクトの pom.xml ファイルに以下の依存関係を追加します。

    <dependencyManagement>
      <dependencies>
        <dependency>
          <groupId>org.keycloak.bom</groupId>
          <artifactId>keycloak-adapter-bom</artifactId>
          <version>${version.org.keycloak}</version>
          <type>pom</type>
          <scope>import</scope>
        </dependency>
      </dependencies>
    </dependencyManagement>
    
      ....
    
    <dependency>
      <groupId>org.keycloak</groupId>
      <artifactId>keycloak-spring-boot-starter</artifactId>
    </dependency>
  4. application.properties ファイルを更新します。

    # keycloak security setup
    keycloak.auth-server-url=http://localhost:8100/auth
    keycloak.realm=master
    keycloak.resource=springboot-app
    keycloak.public-client=true
    keycloak.principal-attribute=preferred_username
    keycloak.enable-basic-auth=true
  5. DefaultWebSecurityConfig.java ファイルを変更して、Spring Security が RH SSO で正しく動作することを確認します。

    import org.keycloak.adapters.KeycloakConfigResolver;
    import org.keycloak.adapters.springboot.KeycloakSpringBootConfigResolver;
    import org.keycloak.adapters.springsecurity.authentication.KeycloakAuthenticationProvider;
    import org.keycloak.adapters.springsecurity.config.KeycloakWebSecurityConfigurerAdapter;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
    import org.springframework.security.core.authority.mapping.SimpleAuthorityMapper;
    import org.springframework.security.core.session.SessionRegistryImpl;
    import org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy;
    import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy;
    
    @Configuration("kieServerSecurity")
    @EnableWebSecurity
    public class DefaultWebSecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            super.configure(http);
            http
            .csrf().disable()
            .authorizeRequests()
                .anyRequest().authenticated()
                .and()
            .httpBasic();
        }
    
        @Autowired
        public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
            KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();
            SimpleAuthorityMapper mapper = new SimpleAuthorityMapper();
            mapper.setPrefix("");
            keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(mapper);
            auth.authenticationProvider(keycloakAuthenticationProvider);
        }
    
        @Bean
        public KeycloakConfigResolver KeycloakConfigResolver() {
           return new KeycloakSpringBootConfigResolver();
        }
    
        @Override
        protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
            return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
        }
    }

3.4. Quartz を使用したクラスターのビジネスアプリケーションの設定

クラスターでアプリケーションを実行する予定の場合には、Quartz タイマーサービスを設定する必要があります。

前提条件

  • business application の Web サイトを使用して作成した <business-application>.zip ファイルがあり、クラスターで使用する予定である。

手順

  1. quartz.properties ファイルを作成して、以下のコンテンツを追加します。

    #============================================================================
    # Configure Main Scheduler Properties
    #============================================================================
    org.quartz.scheduler.instanceName = SpringBootScheduler
    org.quartz.scheduler.instanceId = AUTO
    org.quartz.scheduler.skipUpdateCheck=true
    org.quartz.scheduler.idleWaitTime=1000
    #============================================================================
    # Configure ThreadPool
    #============================================================================
    org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
    org.quartz.threadPool.threadCount = 5
    org.quartz.threadPool.threadPriority = 5
    #============================================================================
    # Configure JobStore
    #============================================================================
    org.quartz.jobStore.misfireThreshold = 60000
    org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreCMT
    org.quartz.jobStore.driverDelegateClass=org.jbpm.process.core.timer.impl.quartz.DeploymentsAwareStdJDBCDelegate
    org.quartz.jobStore.useProperties=false
    org.quartz.jobStore.dataSource=myDS
    org.quartz.jobStore.nonManagedTXDataSource=notManagedDS
    org.quartz.jobStore.tablePrefix=QRTZ_
    org.quartz.jobStore.isClustered=true
    org.quartz.jobStore.clusterCheckinInterval = 5000
    #============================================================================
    # Configure Datasources
    #============================================================================
    org.quartz.dataSource.myDS.connectionProvider.class=org.jbpm.springboot.quartz.SpringConnectionProvider
    org.quartz.dataSource.myDS.dataSourceName=quartzDataSource
    org.quartz.dataSource.notManagedDS.connectionProvider.class=org.jbpm.springboot.quartz.SpringConnectionProvider
    org.quartz.dataSource.notManagedDS.dataSourceName=quartzNotManagedDataSource
    注記

    Quartz 設定ファイルのデータソース名は、Spring bean を参照します。接続プロバイダーは、org.jbpm.springboot.quartz.SpringConnectionProvider に設定して、Spring ベースのソースとの統合を有効化する必要があります。

  2. <business-application>/<business-application>-service/src/main/resourcesapplication.properties ファイルに以下のプロパティーを追加して、 クラスター化された Quartz タイマーを有効にして、1 つ前の手順で作成した quartz.properties ファイルのパスを設定します。

    jbpm.quartz.enabled=true
    jbpm.quartz.configuration=quartz.properties
  3. 以下の内容を <business-application>/<business-application>-service/src/main/resources/application.properties ファイルに追加して、管理対象のデータソースと、管理対象外のデータソースを作成します。

    # enable to use database as storage
    jbpm.quartz.db=true
    
    quartz.datasource.name=quartz
    quartz.datasource.username=sa
    quartz.datasource.password=sa
    quartz.datasource.url=jdbc:h2:./target/spring-boot-jbpm;MVCC=true
    quartz.datasource.driver-class-name=org.h2.Driver
    
    # used to configure connection pool
    quartz.datasource.dbcp2.maxTotal=15
    
    # used to initialize quartz schema
    quartz.datasource.initialization=true
    spring.datasource.schema=classpath*:<QUARTZ_TABLES_H2>.sql
    spring.datasource.initialization-mode=always

    上記の例では、<QUARTZ_TABLES_H2> は Quartz H2 データベースのスキーマスクリプトの名前に置き換えます。上記の設定の最後の 3 行で、データベーススキーマを初期化します。

デフォルトでは、Quartz には 2 つのデータソースが必要です。

  • デシジョンエンジンまたはプロセスエンジンのトランザクションに参加する管理対象データソース。
  • タイマーを検索してトランザクション処理を行わずにトリガーする管理対象外のデータソース。

Red Hat Process Automation Manager ビジネスアプリケーションでは、Quartz データベース (スキーマ) が Red Hat Process Automation Manager テーブルと共存することを想定しているので、Quartz のトランザクション操作に使用するデータソースを生成します。

他の (トランザクション以外) データソースを設定する必要がありますが、主なデータソースと同じデータベースを参照する必要があります。

3.5. ビジネスアプリケーションのユーザーグループプロバイダーの設定

Red Hat Process Automation Manager を使用すると、人間中心のアクティビティーを管理できます。2つの KIE API エントリーポイントを使用して、ユーザーおよびグループリポジトリーを統合できます。

  • UserGroupCallback: ユーザーまたはグループが存在するかどうかを確認して、特定のユーザーのグループの情報を集めます。
  • UserInfo: メールアドレスや設定言語など、ユーザーおよびグループの追加情報を収集します。

すぐに使用できるコードまたはカスタムの開発コードなど、代わりのコードを指定すると、これらの両コンポーネントを設定できます。

UserGroupCallback コンポーネントについては、デフォルトの実装はアプリケーションのセキュリティーコンテキストをもとにしているので、この実装が保持されます。このように、どのバックエンドストアを認証や承認 (例: RH-SSO) に使用するかは重要ではありません。ユーザーやグループの情報収集用の情報源として、この実装が自動的に使用されます。

UserInfo コンポーネントは、より詳細にわたる情報を収集するので、別のコンポーネントとなっています。

前提条件

  • business applications の Web サイトを使用して作成し、ビジネス自動化プロジェクトが含まれる <business-application>.zip ファイルがある。

手順

  1. UserGroupCallback の別の実装を提供するには、以下のコードを、アプリケーションクラスに追加するか、@Configuration のアノテーションが付いた別のクラスに追加します。

    @Bean(name = "userGroupCallback")
    public UserGroupCallback userGroupCallback(IdentityProvider identityProvider) throws IOException {
        return new MyCustomUserGroupCallback(identityProvider);
    }
  2. UserInfo の別の実装を提供するには、以下のコードを、アプリケーションクラスに追加するか、@Configuration のアノテーションが付いた別のクラスに追加します。

    @Bean(name = "userInfo")
    public UserInfo userInfo() throws IOException {
        return new MyCustomUserInfo();
    }

3.6. MySQL または PostgreSQL データベースを使用したビジネスアプリケーションの設定

Red Hat Process Automation Manager ビジネスアプリケーションは、デフォルトの H2 データベースで生成されます。データベースのタイプは、MySQL または PostgreSQL に変更できます。

前提条件

  • business applications の Web サイトを使用して作成した <business-application>.zip ファイルがある。

手順

  1. <business-application>.zip ファイルを展開して、<business-application>/business-application-service/src/main/resources フォルダーに移動します。
  2. テキストエディターで application.properties ファイルを開きます。
  3. ビジネスアプリケーションで MySQL データベースを使用するように設定するには、application.properties ファイルで以下のパラメーターを検索して、以下の値に変更します。

    spring.datasource.username=jbpm
    spring.datasource.password=jbpm
    spring.datasource.url=jdbc:mysql://localhost:3306/jbpm
    spring.datasource.driver-class-name=com.mysql.jdbc.jdbc2.optional.MysqlXADataSource
    
    spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
  4. ビジネスアプリケーションで PostgreSQL データベースを使用するように設定するには、application.properties ファイルで以下のパラメーターを検索して、以下の値に変更します。

    spring.datasource.username=jbpm
    spring.datasource.password=jbpm
    spring.datasource.url=jdbc:postgresql://localhost:5432/jbpm
    spring.datasource.driver-class-name=org.postgresql.xa.PGXADataSource
    
    spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
  5. application.properties ファイルを保存します。

3.7. JPA 用のビジネスアプリケーションの設定

Java Persistence API (JPA) は、オブジェクトをリレーショナルデータベースにマッピング可能にする標準技術です。Red Hat Process Automation Manager のビジネスアプリケーションには、JPA を設定する必要があります。

前提条件

  • business applications ファイルを使用して作成した Red Hat Process Automation Manager <business-application>.zip ファイルがある。

手順

  1. <business-application>.zip ファイルを展開して、<business-application>/<business-application>-service/src/main/resources フォルダーに移動します。
  2. テキストエディターで application.properties ファイルを開きます。
  3. application.properties ファイルで以下のパラメーターを検索して、以下の値が含まれていることを確認します。

    spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
    spring.jpa.properties.hibernate.show_sql=false
    spring.jpa.properties.hibernate.hbm2ddl.auto=update
    spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
  4. ビジネスアプリケーションにビジネス自動化機能がある場合は、コンマ区切りの一覧として、パッケージを追加し、エンティティーマネージャーファクトリーにエンティティーを追加してください。

    spring.jpa.properties.entity-scan-packages=org.jbpm.springboot.samples.entities

    ビジネス自動化機能の付いたビジネスアプリケーションは、Red Hat Process Automation Manager に同梱の persistence.xml ファイルをもとに、エンティティーマネージャーファクトリーを作成します。org.jbpm.springboot.samples.entities パッケージにあるエンティティーはすべて、エンティティーマネージャーファクトリーに自動的に追加され、アプリケーションの他の JPA エンティティーと同じものが使用されます。

関連資料

JPA の設定に関する情報は、Spring Boot Reference Guide を参照してください。

3.8. Swagger ドキュメントの有効化

Red Hat Process Automation Manager ビジネスアプリケーションのサービスプロジェクトで利用可能なすべてのエンドポイントに関する Swaggerベースのドキュメントを有効にできます。

前提条件

  • business applications の Web サイトを使用して作成した <business-application>.zip ファイルがある。

手順

  1. <business-application>.zip ファイルを展開して、<business-application>/<business-application>-service フォルダーに移動します。
  2. テキストエディターでサービスプロジェクト pom.xml ファイルを開きます。
  3. サービスプロジェクトの pom.xml ファイルに以下の依存関係を追加して、このファイルを保存します。

    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-rs-service-description-swagger</artifactId>
      <version>3.2.6</version>
    </dependency>
    <dependency>
      <groupId>io.swagger</groupId>
      <artifactId>swagger-jaxrs</artifactId>
      <version>1.5.15</version>
      <exclusions>
        <exclusion>
          <groupId>javax.ws.rs</groupId>
          <artifactId>jsr311-api</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  4. Swagger UI を有効にするには (オプション)、以下の依存関係を pom.xml ファイルに追加して、このファイルを保存します。

    <dependency>
      <groupId>org.webjars</groupId>
      <artifactId>swagger-ui</artifactId>
      <version>2.2.10</version>
    </dependency>
  5. テキストエディターで <business-application>/<business-application>-service/src/main/resources/application.properties ファイルを開きます。
  6. Swaggerサポートを有効にするには、以下の行を application.properties ファイルに追加します。

    kieserver.swagger.enabled=true

ビジネスアプリケーションの起動後に、http://localhost:8090/rest/swagger.json で Swaggerドキュメントを表示できます。全エンドポイントについては、http://localhost:8090/rest/api-docs?url=http://localhost:8090/rest/swagger.json で入手できます。