【Spring MVC + Spring Security】セキュリティ機能の導入方法

概要

Spring Security機能を利用するための必要資材や導入方法についてまとめた。

 

前提

以下の記事の続きとなる。

あわせて読みたい

概要 Spring Securityフレームワークのアーキテクチャについてまとめた。   Spring Securityとは アプリケーションに、セキュリティ対策機能を実装するためのフレームワークとなる。 様々なセキ[…]

プロジェクトのセットアップについては以下を参照。

あわせて読みたい

概要 Spring MVCフレームワークを使用して開発を進める際、Mavenプロジェクトの基本的なセットアップ方法についてまとめた。   前提 プロジェクトの具体的な作成方法については以下に記載。 [siteca[…]

 

導入方法

Spring Securityフレームワークを利用するための必要な資材について紹介する。
基本的には必要な依存資材を導入し、SecurityFilterChain(複数のセキュリティフィルターで構成された処理パッケージ)をBean定義して
必要なフィルター機能の設定を行う。

 

依存資材

Spring Securityの機能を利用する場合、以下のライブラリを導入する。
※機能追加に合わせて適宜その他ライブラリを追加していく

ライブラリ 役割
spring-security-web HTTPリクエストをフィルターで処理し、セキュリティロジックをWeb層に適用するライブラリ
spring-security-core 認証・認可のロジックや暗号化など、セキュリティの基盤機能を提供する中核ライブラリ
spring-security-config JavaやXMLによるセキュリティ設定を簡潔に記述するための補助ライブラリ

 

これらのライブラリをpom.xmlに記述する。
※今回はバージョン5.8.5を使用

pom.xml


<!-- Spring Security -->
<dependency>
	<groupId>org.springframework.security</groupId>
	<artifactId>spring-security-web</artifactId>
	<version>5.8.5</version>
</dependency>
<dependency>
	<groupId>org.springframework.security</groupId>
	<artifactId>spring-security-core</artifactId>
	<version>5.8.5</version>
</dependency>
<dependency>
	<groupId>org.springframework.security</groupId>
	<artifactId>spring-security-config</artifactId>
	<version>5.8.5</version>
</dependency>

 

SecurityFilterChainをBean定義

SecurityFilterChainのBean定義を行う。
構成方法はXMLとJavaConfigの両方に対応しているが、可読性と保守性の観点からJavaConfigベースの定義が推奨されている。

 

SecurityConfig.java


package com.example.prototype.biz.security.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class SecurityConfig {
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        // ここにHTTPリクエストに対する認証・認可のルールを定義する
        
        return http.build();
    }
}

 

@EnableWebSecurity

Spring Securityの機能を利用するためのアノテーション。

Spring Securityを利用するために必要なBean(AuthenticationManagerなど)がDIコンテナに自動登録される。
※ただし、Spring Securityの機能を有効にするには別途web.xmlへの登録が必要(後述)

 

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

SecurityFilterChainのBean定義の中で、HTTPリクエストに対する認証・認可のルールを実装していく。

 

FilterChainProxyをフィルター登録

FilterChainProxy(Bean名:springSecurityFilterChain)をServletフィルターとしてweb.xmlに登録することで、
Spring Security のフィルター処理が有効になる。

これにより、受け取ったHTTPリクエストに対してSecurityFilterChainに定義した認証・認可設定が適用されるようになる。

 

web.xml


<!-- Spring Security Filter -->
<filter>
	<filter-name>springSecurityFilterChain</filter-name>
	<filter-class>org.springframework.web.filter.DelegatingFilterProxy
	</filter-class>
</filter>

<filter-mapping>
	<filter-name>springSecurityFilterChain</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

 

まとめ

 

☑ Spring Security機能を利用するためには、「web」「core」「config」の3つのライブラリが必要

☑ 認証・認可のルールは、SecurityFilterChainのBean定義内で構成する

☑ web.xmlにspringSecurityFilterChainを登録することで、Spring Securityの仕組みが有効になる

 

スポンサーリンク