1. 序章

この記事では、SpringセッションとSpringWebFluxを組み合わせる方法を学習します。 具体的には、SpringSessionとSpring Boot2のWebFluxを統合するSpringWebSessionの使用方法を学習します。

Spring Session は、「名前と値のペアの簡略化されたMap」として定義されます。 セッション次のようなHTTPセッションにとって重要な値を追跡するユーザープリンシパル。 したがって、私たちは使用することができますセッション新しいリアクティブWebFluxと一緒に管理単核症フラックスオブジェクト 。 Spring Sessionは、(Tomcatだけでなく)さまざまなアプリケーションコンテナの使用もサポートしています。

Springセッションの詳細については、Baeldungに関する別のすばらしい記事をご覧ください。

2. Mavenのセットアップ

それでは、アプリをセットアップして構成しましょう。 ありがたいことに、pom.xmlの構成は非常に簡単です。 まず、関連するSpringSessionの依存関係とともにSpringBoot2.xxを使用する必要があります。 Mavenリポジトリから最新バージョンを追加します。

次に、それらをpom.xmlに追加します。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
    <version>2.5.1</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
    <version>2.5.1</version>
</dependency>
<dependency> 
    <groupId>org.springframework.session</groupId> 
    <artifactId>spring-session-core</artifactId> 
    <version>2.5.1</version> 
</dependency>

これらの3つの依存関係は、メモリ内セッション管理の最小要件です。  Redisの場合は、次を使用します。

次に、pom.xmlに以下を追加します。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.5.1</version>
</dependency>
<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
    <version>2.5.1</version>
 </dependency>

それでは、クラスを構成しましょう。

3. インメモリ構成

インメモリ構成を使用するには、構成クラスを追加します。

@Configuration
@EnableSpringWebSession
public class SessionConfig {
 
    @Bean
    public ReactiveSessionRepository reactiveSessionRepository() {
        return new ReactiveMapSessionRepository(new ConcurrentHashMap<>());
    }
}

これにより、(リアクティブな)リポジトリがセッションマネージャに関連付けられます。 それらの値をHashMapに保存します。

重要なのは、構成クラスに@EnableSpringWebSessionアノテーションが含まれている必要があるということです。

4. Redis構成

それでは、Redisを接続しましょう。 Redisを使用してWebSessionsを管理するには、構成クラスを追加します。

@Configuration
@EnableRedisWebSession
public class RedisConfig {
 
    @Bean
    public LettuceConnectionFactory redisConnectionFactory() {
        return new LettuceConnectionFactory();
    }
}

構成クラスには、 @EnableRedisWebSession 注釈 。 覚えておいてください、私たちは使用することはできません @EnableRedisWebSession EnableSpringWebSession 例外を発生させることなく一緒に注釈。

Docker は、Redisを操作する最も簡単な方法の1つです。 Dockerをインストールした後は、3つのコマンドを入力するだけで済みます。 コマンドを実行して、Redisインスタンスを起動します。

$ docker stop redis
$ docker rm redis
$ docker run -d --name redis -p 6379:6379 redis:4.0.5-alpine

次に、アプリをテストしてみましょう。

5. 実際には

それでは、リアクティブRESTコントローラーをアプリに追加しましょう。

@GetMapping("/websession")
public Mono<String> getSession(WebSession session) {
    session.getAttributes().putIfAbsent("note", "Howdy Cosmic Spheroid!");
    return Mono.just((String) session.getAttributes().get("note"));
}

次に、RESTハンドラーにパラメーターを追加することで、WebSessionを使用できます。  マップを返す.getAttributes()メソッドを使用して、値を取得または設定できます。

Springアプリを起動してみましょう。

これで、アプリは localhost:8080 で表示でき、Springログインページが表示されます。 デフォルトのログイン資格情報( -u admin -p password )を試してください。

認証後、デフォルトの WebSession 値( 0および「HowdyCosmicSpheroid!」)を変更できます。 curlコマンドを実行します

$ curl -i -H "Accept: application/json" -H "Content-Type:application/json" -X GET http://localhost:8080/websession/test?id=222¬e=helloworld

または、URL http:// localhost:8080 / websession / test?id=222¬e=helloworldにアクセスしてください。 その後、 localhost:8080 / websession から返されたJSONは、更新されたSession値を表示します。

そのエンドポイントlocalhost:8080 / websession、は、現在のWebSession属性idおよびnote。を返します。

6. 結論

SpringWebSessionをWebFluxアプリケーションに追加する方法を学びました。 詳細については、すばらしい公式ドキュメントをご覧ください。

いつものように、この記事で使用されているコードサンプルはGitHubで入手できます。