第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ファイルがある。
手順
-
<business-application>.zipファイルを展開して、<business-application>/<business-application>-service/src/main/resourcesフォルダーに移動します。 -
テキストエディターで
application.propertiesファイルを開きます。 たとえば、以下のように REST エンドポイントのホスト、ポート、およびパスを設定します。
server.address=localhost server.port=8090 cxf.path=/rest
以下のように、簡単に識別できるように Process Server (
kieserver) を設定します。kieserver.serverId=<business-application>-service kieserver.serverName=<business-application>-service kieserver.location=http://localhost:8090/rest/server kieserver.controllers=http://localhost:8080/jbpm-console/rest/controller
以下の表で、ビジネスアプリケーションに設定可能な Process Server のパラメーターを一覧で紹介します。
表3.1 kieserver パラメーター
パラメーター 値 説明 kieserver.serverIdstring
Process Automation Manager コントローラーに接続時にビジネスアプリケーションを識別するために使用する ID
kieserver.serverNamestring
Process Automation Manager コントローラーに接続時にビジネスアプリケーションを識別するのに使用する名前。
kieserver.serverIdパラメーターに使用した文字と同じものを使用できます。kieserver.locationURL
REST API を使用する他のコンポーネントがこのサーバーの場所を識別するために使用します。
server.addressおよびserver.portで定義されている場所は、使用しないでください。kieserver.controllersURL
コントローラー URL のコンマ区切りリスト
非同期実行を有効にするには、以下のように、
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.enabledtrue, falseエグゼキューターコンポーネントを無効または有効にします。
jbpm.executor.retries整数
ジョブの実行中にエラーが発生した場合の再試行回数を指定します。
jbpm.executor.interval整数
エグゼキューターがデータベースの同期にかける時間を指定します。時間の単位は
jbpm.executor.timeUnitパラメーターで指定します。デフォルトは無効 (値0) です。jbpm.executor.threadPoolSize整数
スレッドプールのサイズを指定します。
jbpm.executor.timeUnitstring
エグゼキューターがデータベースの同期に費やす間隔を計算するときに使用する時間の単位を指定します。値は、
java.util.concurrent.TimeUnitの有効な定数を指定する必要があります。デフォルト値は、SECONDSです。ビジネスアプリケーションの作成時に Business Automation を選択した場合には、以下のコンポーネントの内、ランタイムに起動するコンポーネントを指定します。
表3.3
kieserverケーパビリティーパラメーターパラメーター 値 説明 kieserver.drools.enabledtrue, falseDecision Manager コンポーネントを有効または無効にします。
kieserver.dmn.enabledtrue, falseDMN (Decision Model and Notation) コンポーネントを有効または無効にします。
kieserver.jbpm.enabledtrue, falseRed Hat Process Automation Manager コンポーネントを有効または無効にします。
kieserver.jbpmui.enabledtrue, falseRed Hat Process Automation Manager UI コンポーネントを有効または無効にします。
kieserver.casemgmt.enabledtrue, falseケース管理のコンポーネントを有効または無効にします。
3.3. Red Hat Single Sign-On を使用したビジネスアプリケーションの設定
Red Hat Single Sign-On (RH SSO) を使用して、サービス間のシングルサインオンを有効化し、一元的にユーザーとロールの設定や管理ができます。
前提条件
-
business applications の Web サイトを使用して作成した
<business-application>.zipファイルがある。
手順
- Red Hat シングルサインオン (SSO) をダウンロードし、インストールしている。『Red Hat Single Sign-On Getting Started Guide』を参照してください。
RH SSO を設定します。
- デフォルトのマスターレルムを使用するか、新しいレルムを作成します。
-
springboot-appクライアントを作成して、パブリックにAccessTypeを追加します。 以下のように、 ローカルの設定に合わせて有効なリダイレクト URI と Web オリジンを設定します。
-
有効なリダイレクト URI:
http://localhost:8090/* -
Web オリジン:
http://localhost:8090
-
有効なリダイレクト URI:
- アプリケーションで使用するレルムロールを作成します。
- アプリケーションで使用するユーザーを作成してロールを割り当てます。
サービスプロジェクトの
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>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
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ファイルがあり、クラスターで使用する予定である。
手順
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 ベースのソースとの統合を有効化する必要があります。<business-application>/<business-application>-service/src/main/resourcesapplication.propertiesファイルに以下のプロパティーを追加して、 クラスター化された Quartz タイマーを有効にして、1 つ前の手順で作成したquartz.propertiesファイルのパスを設定します。jbpm.quartz.enabled=true jbpm.quartz.configuration=quartz.properties
以下の内容を
<business-application>/<business-application>-service/src/main/resources/application.propertiesファイルに追加して、管理対象のデータソースと、管理対象外のデータソースを作成します。# enable to use data base 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ファイルがある。
手順
UserGroupCallbackの別の実装を提供するには、以下のコードを、アプリケーションクラスに追加するか、@Configurationのアノテーションが付いた別のクラスに追加します。@Bean(name = "userGroupCallback") public UserGroupCallback userGroupCallback(IdentityProvider identityProvider) throws IOException { return new MyCustomUserGroupCallback(identityProvider); }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ファイルがある。
手順
-
<business-application>.zipファイルを展開して、<business-application>/business-application-service/src/main/resourcesフォルダーに移動します。 -
テキストエディターで
application.propertiesファイルを開きます。 ビジネスアプリケーションで 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
ビジネスアプリケーションで 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
-
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ファイルがある。
手順
-
<business-application>.zipファイルを展開して、<business-application>/<business-application>-service/src/main/resourcesフォルダーに移動します。 -
テキストエディターで
application.propertiesファイルを開きます。 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
ビジネスアプリケーションにビジネス自動化機能がある場合は、コンマ区切りの一覧として、パッケージを追加し、エンティティーマネージャーファクトリーにエンティティーを追加してください。
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ファイルがある。
手順
-
<business-application>.zipファイルを展開して、<business-application>/<business-application>-serviceフォルダーに移動します。 -
テキストエディターでサービスプロジェクト
pom.xmlファイルを開きます。 サービスプロジェクトの
pom.xmlファイルに以下の依存関係を追加して、このファイルを保存します。<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-rs-service-description-swagger</artifactId> <version>3.1.11</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>Swagger UI を有効にするには (オプション)、以下の依存関係を
pom.xmlファイルに追加して、このファイルを保存します。<dependency> <groupId>org.webjars</groupId> <artifactId>swagger-ui</artifactId> <version>2.2.10</version> </dependency>
-
テキストエディターで
<business-application>/<business-application>-service/src/main/resources/application.propertiesファイルを開きます。 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 で入手できます。