Redis の構成

アプリケーションの設定ができたところで、カスタマイズを始めましょう。

JSON を使用したセッションの直列化

デフォルトでは、Spring Session は Java 直列化を使用してセッション属性を直列化します。特に、同じ Redis インスタンスを使用しているが、同じクラスの異なるバージョンを持つ複数のアプリケーションがある場合、問題が発生することがあります。RedisSerializer Bean を提供して、セッションを Redis に直列化する方法をカスタマイズできます。Spring Data Redis は、Jackson の ObjectMapper を使用してオブジェクトを直列化および逆直列化する GenericJackson2JsonRedisSerializer を提供します。

RedisSerializer の構成
@Configuration
public class SessionConfig implements BeanClassLoaderAware {

	private ClassLoader loader;

	/**
	 * Note that the bean name for this bean is intentionally
	 * {@code springSessionDefaultRedisSerializer}. It must be named this way to override
	 * the default {@link RedisSerializer} used by Spring Session.
	 */
	@Bean
	public RedisSerializer<Object> springSessionDefaultRedisSerializer() {
		return new GenericJackson2JsonRedisSerializer(objectMapper());
	}

	/**
	 * Customized {@link ObjectMapper} to add mix-in for class that doesn't have default
	 * constructors
	 * @return the {@link ObjectMapper} to use
	 */
	private ObjectMapper objectMapper() {
		ObjectMapper mapper = new ObjectMapper();
		mapper.registerModules(SecurityJackson2Modules.getModules(this.loader));
		return mapper;
	}

	/*
	 * @see
	 * org.springframework.beans.factory.BeanClassLoaderAware#setBeanClassLoader(java.lang
	 * .ClassLoader)
	 */
	@Override
	public void setBeanClassLoader(ClassLoader classLoader) {
		this.loader = classLoader;
	}

}

上記のコードスニペットは Spring Security を使用しているため、Spring Security の Jackson モジュールを使用するカスタム ObjectMapper を作成しています。Spring Security Jackson モジュールが必要ない場合は、アプリケーションの ObjectMapper Bean を挿入して次のように使用できます。

@Bean
public RedisSerializer<Object> springSessionDefaultRedisSerializer(ObjectMapper objectMapper) {
    return new GenericJackson2JsonRedisSerializer(objectMapper);
}

別の名前空間の指定

複数のアプリケーションが同じ Redis インスタンスを使用することは珍しいことではありません。そのため、Spring Session は、必要に応じて namespace (デフォルトは spring:session) を使用してセッションデータを分離します。

Spring Boot プロパティの使用

spring.session.redis.namespace プロパティを設定することで指定できます。

application.properties
spring.session.redis.namespace=spring:session:myapplication
application.yml
spring:
  session:
    redis:
      namespace: "spring:session:myapplication"

アノテーションの属性の使用

namespace を指定するには、@EnableRedisHttpSession@EnableRedisIndexedHttpSession、または @EnableRedisWebSession アノテーションで redisNamespace プロパティを設定します。

@EnableRedisHttpSession
@Configuration
@EnableRedisHttpSession(redisNamespace = "spring:session:myapplication")
public class SessionConfig {
    // ...
}
@EnableRedisIndexedHttpSession
@Configuration
@EnableRedisIndexedHttpSession(redisNamespace = "spring:session:myapplication")
public class SessionConfig {
    // ...
}
@EnableRedisWebSession
@Configuration
@EnableRedisWebSession(redisNamespace = "spring:session:myapplication")
public class SessionConfig {
    // ...
}

RedisSessionRepository と RedisIndexedSessionRepository のどちらかを選択する

Spring Session Redis を使用する場合は、おそらく RedisSessionRepository と RedisIndexedSessionRepository のどちらかを選択する必要があります。どちらも、セッションデータを Redis に保存する SessionRepository インターフェースの実装です。ただし、セッションのインデックス作成とクエリの処理方法が異なります。

  • RedisSessionRepositoryRedisSessionRepository は、追加のインデックス作成を行わずにセッションデータを Redis に保存する基本的な実装です。シンプルなキーと値の構造を使用してセッション属性を保存します。各セッションには一意のセッション ID が割り当てられ、セッションデータはその ID に関連付けられた Redis キーに保存されます。セッションを取得する必要がある場合、リポジトリはセッション ID を使用して Redis にクエリを実行し、関連するセッションデータを取得します。インデックス作成がないため、セッション ID 以外の属性や条件に基づいてセッションをクエリするのは非効率的になる可能性があります。

  • RedisIndexedSessionRepositoryRedisIndexedSessionRepository は、Redis に保存されたセッションにインデックス作成機能を提供する拡張実装です。属性または条件に基づいてセッションを効率的にクエリするために、Redis に追加のデータ構造が導入されています。RedisSessionRepository で使用されるキーと値の構造に加えて、高速検索を可能にする追加のインデックスも維持されます。例: ユーザー ID や最終アクセス時間などのセッション属性に基づいてインデックスを作成する場合があります。これらのインデックスにより、特定の条件に基づいてセッションの効率的なクエリが可能になり、パフォーマンスが向上し、高度なセッション管理機能が有効になります。それに加えて、RedisIndexedSessionRepository はセッションの有効期限と削除もサポートしています。

RedisIndexedSessionRepository を Redis クラスターで使用する場合、クラスター内の 1 つのランダムな Redis ノードからのイベントのみをサブスクライブする [GitHub] (英語) ため、別のノードでイベントが発生した場合に一部のセッションインデックスがクリーンアップされない可能性があることに注意する必要があります。

RedisSessionRepository の構成

Spring Boot プロパティの使用

Spring Boot を使用している場合、RedisSessionRepository がデフォルトの実装です。ただし、それを明示的にしたい場合は、アプリケーションで次のプロパティを設定できます。

application.properties
spring.session.redis.repository-type=default
application.yml
spring:
  session:
    redis:
      repository-type: default

アノテーションの使用

@EnableRedisHttpSession アノテーションを使用して RedisSessionRepository を構成できます。

@Configuration
@EnableRedisHttpSession
public class SessionConfig {
    // ...
}

RedisIndexedSessionRepository の構成

Spring Boot プロパティの使用

アプリケーションで次のプロパティを設定することで、RedisIndexedSessionRepository を構成できます。

application.properties
spring.session.redis.repository-type=indexed
application.yml
spring:
  session:
    redis:
      repository-type: indexed

アノテーションの使用

@EnableRedisIndexedHttpSession アノテーションを使用して RedisIndexedSessionRepository を構成できます。

@Configuration
@EnableRedisIndexedHttpSession
public class SessionConfig {
    // ...
}

セッションイベントのリスニング

多くの場合、セッションイベントに反応することが重要です。たとえば、セッションのライフサイクルに応じて何らかの処理を実行する必要がある場合があります。これを行うには、インデックス付きリポジトリを使用する必要があります。インデックス付きリポジトリとデフォルトリポジトリの違いがわからない場合は、このセクションに進んでください。

インデックス付きリポジトリが構成されたら、SessionCreatedEventSessionDeletedEventSessionDestroyedEventSessionExpiredEvent イベントのリッスンを開始できるようになります。Spring でアプリケーションイベントをリッスンする方法はいくつかありますが、ここでは @EventListener アノテーションを使用します。

@Component
public class SessionEventListener {

    @EventListener
    public void processSessionCreatedEvent(SessionCreatedEvent event) {
        // do the necessary work
    }

    @EventListener
    public void processSessionDeletedEvent(SessionDeletedEvent event) {
        // do the necessary work
    }

    @EventListener
    public void processSessionDestroyedEvent(SessionDestroyedEvent event) {
        // do the necessary work
    }

    @EventListener
    public void processSessionExpiredEvent(SessionExpiredEvent event) {
        // do the necessary work
    }

}

特定のユーザーのすべてのセッションを検索する

特定のユーザーのすべてのセッションを取得することで、デバイスまたはブラウザー全体でユーザーのアクティブなセッションを追跡できます。例: この情報セッションは、ユーザーが特定のセッションを無効にしたりログアウトしたり、ユーザーのセッションアクティビティに基づいてアクションを実行したりできるようにするなど、管理目的に使用できます。

これを行うには、まずインデックス付きリポジトリを使用する必要があります。次に、次のように FindByIndexNameSessionRepository インターフェースを挿入します。

@Autowired
public FindByIndexNameSessionRepository<? extends Session> sessions;

public Collection<? extends Session> getSessions(Principal principal) {
    Collection<? extends Session> usersSessions = this.sessions.findByPrincipalName(principal.getName()).values();
    return usersSessions;
}

public void removeSession(Principal principal, String sessionIdToDelete) {
    Set<String> usersSessionIds = this.sessions.findByPrincipalName(principal.getName()).keySet();
    if (usersSessionIds.contains(sessionIdToDelete)) {
        this.sessions.deleteById(sessionIdToDelete);
    }
}

上の例では、getSessions メソッドを使用して特定のユーザーのすべてのセッションを検索し、removeSession メソッドを使用してユーザーの特定のセッションを削除できます。

Redis セッションマッパーの構成

Spring Session Redis は、Redis からセッション情報を取得し、それを Map<String, Object> に保存します。このマップは、マッピングプロセスを経て MapSession オブジェクトに変換される必要があり、その後 RedisSession 内で使用されます。

この目的で使用されるデフォルトのマッパーは RedisSessionMapper と呼ばれます。creationTime など、セッションを構築するために必要な最小限のキーがセッションマップに含まれていない場合、このマッパーは例外をスローします。必要なキーがない場合に考えられるシナリオの 1 つは、保存プロセスの進行中に、通常は期限切れによりセッションキーが同時に削除される場合です。これは、HSET コマンド (英語) がキー内のフィールドの設定に使用されており、キーが存在しない場合はこのコマンドによって作成されるために発生します。

マッピングプロセスをカスタマイズする場合は、BiFunction<String, Map<String, Object>, MapSession> の実装を作成し、それをセッションリポジトリに設定できます。次の例は、マッピングプロセスをデフォルトマッパーに委譲する方法を示していますが、例外がスローされた場合、セッションは Redis から削除されます。

  • RedisSessionRepository

  • RedisIndexedSessionRepository

  • ReactiveRedisSessionRepository

@Configuration
@EnableRedisHttpSession
public class SessionConfig {

    @Bean
    SessionRepositoryCustomizer<RedisSessionRepository> redisSessionRepositoryCustomizer() {
        return (redisSessionRepository) -> redisSessionRepository
                .setRedisSessionMapper(new SafeRedisSessionMapper(redisSessionRepository));
    }

    static class SafeRedisSessionMapper implements BiFunction<String, Map<String, Object>, MapSession> {

        private final RedisSessionMapper delegate = new RedisSessionMapper();

        private final RedisSessionRepository sessionRepository;

        SafeRedisSessionMapper(RedisSessionRepository sessionRepository) {
            this.sessionRepository = sessionRepository;
        }

        @Override
        public MapSession apply(String sessionId, Map<String, Object> map) {
            try {
                return this.delegate.apply(sessionId, map);
            }
            catch (IllegalStateException ex) {
                this.sessionRepository.deleteById(sessionId);
                return null;
            }
        }

    }

}
@Configuration
@EnableRedisIndexedHttpSession
public class SessionConfig {

    @Bean
    SessionRepositoryCustomizer<RedisIndexedSessionRepository> redisSessionRepositoryCustomizer() {
        return (redisSessionRepository) -> redisSessionRepository.setRedisSessionMapper(
                new SafeRedisSessionMapper(redisSessionRepository.getSessionRedisOperations()));
    }

    static class SafeRedisSessionMapper implements BiFunction<String, Map<String, Object>, MapSession> {

        private final RedisSessionMapper delegate = new RedisSessionMapper();

        private final RedisOperations<String, Object> redisOperations;

        SafeRedisSessionMapper(RedisOperations<String, Object> redisOperations) {
            this.redisOperations = redisOperations;
        }

        @Override
        public MapSession apply(String sessionId, Map<String, Object> map) {
            try {
                return this.delegate.apply(sessionId, map);
            }
            catch (IllegalStateException ex) {
                // if you use a different redis namespace, change the key accordingly
                this.redisOperations.delete("spring:session:sessions:" + sessionId); // we do not invoke RedisIndexedSessionRepository#deleteById to avoid an infinite loop because the method also invokes this mapper
                return null;
            }
        }

    }

}
@Configuration
@EnableRedisWebSession
public class SessionConfig {

    @Bean
    ReactiveSessionRepositoryCustomizer<ReactiveRedisSessionRepository> redisSessionRepositoryCustomizer() {
        return (redisSessionRepository) -> redisSessionRepository
                .setRedisSessionMapper(new SafeRedisSessionMapper(redisSessionRepository));
    }

    static class SafeRedisSessionMapper implements BiFunction<String, Map<String, Object>, Mono<MapSession>> {

        private final RedisSessionMapper delegate = new RedisSessionMapper();

        private final ReactiveRedisSessionRepository sessionRepository;

        SafeRedisSessionMapper(ReactiveRedisSessionRepository sessionRepository) {
            this.sessionRepository = sessionRepository;
        }

        @Override
        public Mono<MapSession> apply(String sessionId, Map<String, Object> map) {
            return Mono.fromSupplier(() -> this.delegate.apply(sessionId, map))
                .onErrorResume(IllegalStateException.class,
                    (ex) -> this.sessionRepository.deleteById(sessionId).then(Mono.empty()));
        }

    }

}