LoginSignup
1
1

More than 5 years have passed since last update.

Spring Boot(1系) + spring-security-oauth2 + Redis Session + Heroku で SerializationException

Last updated at Posted at 2018-06-24

Spring Boot(1系) + spring-security-oauth2 + Redis Session の組み合わせで Heroku上で動かすと SerializationException が発生した。
(1インスタンスなら起きない。複数インスタンスの場合のみ)

原因と解決策

ちなみにSpring Boot 2系(Spring5系) を使っている場合は Spring Security5系 を使用すれば良いらしい。(試してない)
Spring Boot 2系(Spring5系)に上げられるならその方がいい。

問題はどうやら spring-security-oauth2 にあるっぽい。
ググって出てきた解決策は 2つ

  1. SerializationException が起きたらRedis該当キーを消す。(id:katsu68 さんのブログ)
  2. スティッキーセッションにする(Heroku スティッキーセッションの設定)

1だけの場合、Heroku上だと頻繁にSerializationExceptionが起きるため、ログイン状態をほとんど維持できないことになる。
2だけの場合だと複数インスタンスにしている意味が薄いし、再起動が走るとやはりSerializationExceptionが起きる。

そのため両方の対応を行い対応した。
デプロイ時とHerokuのインスタンス再起動時だけセッション切れするがしばらくはこれで運用することに。

しかし、Herokuの再起動はいつされるかわからない(任意のタイミングで再起動することによりある程度はコントロールできる)
頻繁にログアウトされるのはユーザにとってもストレス。

ライブラリを作った

それを回避するため一部処理を自分で作ることにした。
また、それをライブラリとして公開することにもした。 -> 作ったライブラリ

        <dependency>
            <groupId>jp.co.ap-com</groupId>
            <artifactId>spring-oauth2-serializable</artifactId>
            <version>0.0.2</version>
        </dependency>

spring-security-oauth2 の設定が済んでいるならばソースコードは@EnableOAuth2Sso@EnableOAuth2Serializable に変えるだけ
ただし、一箇所でも @EnableOAuth2Sso@EnableOAuth2Client が残ってるとダメ。

デモ用アプリケーションはこちら
https://github.com/apc-hattori/spring-oauth2-demo

spring-security-oauth2だけのときから修正する際の差分はこちら
https://github.com/apc-hattori/spring-oauth2-demo/compare/use-spring-security-oauth2...master

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1