Springセッションのリアクティブサポートガイド:WebSession
1前書き
この記事では、Spring SessionとSpring WebFluxを組み合わせる方法を学びます。具体的には、Spring SessionとSpring Boot 2のWebFluxを統合するSpring WebSessionの使い方を学びます。
Springの
Session
は、「名前と値のペアの簡略化された
Map
」として定義されます。
セッション
ユーザー
やプリンシパルのようなHTTPセッションにとって重要な値を追跡します。これで、
Session
管理を新しいリアクティブWebFlux
__Mono
と
Flux
objects
と共に使用できます。
__Spring Sessionは(Tomcatだけではなく)異なるアプリケーションコンテナの使用もサポートします。
Spring Sessionの詳細については、https://www.baeldung.com/spring-session[Baeldung]にあるもう1つの素晴らしい記事を参照してください。
2 Mavenのセットアップ
それでは、我々のアプリをセットアップして設定しましょう。ありがたいことに、__pom.xmlを設定するのはとても簡単です。まず、Spring Boot 2.x.xと関連するSpring Sessionの依存関係を使用する必要があります。 Mavenリポジトリから最新バージョンを追加してください。
ブートスターターWebFlux]**
https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security
[The
春のブーツ
セキュリティ]
はじめに
**
https://mvnrepository.com/artifact/org.springframework.session/spring-session-core
[Spring
セッションコア]
次に、それらを
pom.xml
に追加します。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
これら3つの依存関係は、インメモリセッション管理の最小要件です。 Redisの場合は、次のように使用します。
ブートスターターデータ
**
https://mvnrepository.com/artifact/org.springframework.session/spring-session-data-redis
[Spring
セッションデータの再発行]
次に、
pom.xml
に次の行を追加します。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
それでは、クラスを設定しましょう。
3インメモリ構成
インメモリ設定を使用するには、設定クラスを追加します。
@Configuration
@EnableSpringWebSession
public class SessionConfig {
@Bean
public ReactiveSessionRepository reactiveSessionRepository() {
return new ReactiveMapSessionRepository(new ConcurrentHashMap<>());
}
}
それはあなたのセッションマネージャと(リアクティブ)リポジトリを関連付けます。それらの値を
HashMap
に格納します。
重要なことに、設定クラスは
@ EnableSpringWebSession
アノテーション
__.
__を含まなければなりません。
4 Redisの設定
それでは、Redisを接続しましょう。 Redisを使用して
__WebSessionsを管理するには、設定クラスを
__addします。
@Configuration
@EnableRedisWebSession
public class RedisConfig {
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
return new LettuceConnectionFactory();
}
}
設定クラスには、
__ @ EnableRedisWebSession
annotation
が含まれている必要があります。
@ 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
を使うことができます。
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/server/WebSession.html#getAttributes–
[
.getAttributes()
を使用して値を取得または設定できます。 Mapを返すメソッド。
Springアプリをスピンアップしましょう。
/uploads/websession
one-100×28.png%20100w
これで、私たちのアプリはlocalhost:8080
に表示され、Springのログインページが表示されます。デフォルトのログイン認証情報(
-u admin -p password__)を試してください。
リンク:/uploads/websession__two.png%201573w[]
認証後、デフォルトの
WebSession
値(
0
および
“ Howdy Cosmic Spheroid!”
)を変更できます。 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をご覧ください。
その後、
localhost:8080/websession
から返されたJSONは、更新された
Session
値を表示します。
リンク:/uploads/websession__three-1-100×20.png%20100w[]
そのエンドポイント、
localhost:8080/websession、
は現在の
WebSession
属性
id
および
note.
を返します。
6. 結論
Spring WebSessionをWebFluxアプリケーションに追加する方法を学びました。
詳細については、すばらしい公式文書を調べてください。
いつものように、この記事で使用されているコード・サンプル
are
GitHubで入手できます。