kagamihogeの日記

kagamihogeの日記です。

Spring BootでSpring Securityをredisでうごかす

Spring Securityのセッション保存先をメモリからredisに変更する。

やること

redisのインストール

他サイトを見て適当なマシンにredisをインストールしておく。

依存性の追加

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <!-- spring-boot-starter-redisはdeprecated -->
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.session/spring-session-data-redis -->
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>

上記pom.xmlのコメントにも書いたけどspring-boot-starter-redisはdeprecatedになっているので注意。

プロパティ設定

src/main/resources/application.yamlなどで設定変更する。設定内容はAppendix A. Common application propertiesを参照。必要に応じて、spring.security, spring.redis.*, spring.session.*あたりを修正する。

spring:
  security:
    user:
      password: hoge
  redis:
    host: 192.168.10.23

ここでは、spring-bootでspring-securityをデフォルトで動かすとuserというユーザが使えるのでそれのパスワードをテスト目的のために変更して、redisサーバのhostがデフォルトは127.0.0.1なのでこれを変更している。

エントリーポイント

起動用のmainをつくる。

package kagamihoge.springseurityredis;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

うごかす

spring-boot + spring-securityは特になんも設定しない状態だと http://localhost:8080 にアクセスするとデフォルトで以下のようなログイン画面になる。user/hogeでログインする。

f:id:kagamihoge:20180519162911j:plain

redis-cliで見るとなんかデータが入っている。

$ redis-cli 
127.0.0.1:6379> keys *
1) "spring:session:expirations:1526715420000"
2) "spring:session:sessions:f6fbfc66-2e0d-41ef-97c2-5ef66235eccf"
3) "spring:session:sessions:expires:f6fbfc66-2e0d-41ef-97c2-5ef66235eccf"
4) "spring:session:expirations:1526715480000"
5) "spring:session:index:org.springframework.session.FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME:user"
6) "spring:session:sessions:expires:657e59f9-caf9-4774-ae47-ff4c8eaa2752"
127.0.0.1:6379>