7
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Spring Security】WebSecurityConfigurerAdapterが使えないのを解決する

Posted at

はじめに

SpringSecurityを久々に触り、ん?となった箇所があったため備忘録の意も踏まえて記事にします。
SpringSecurityの設定ファイルであるSecurityConfigを記述していたときのことです。

問題点

SecurityConfigを以下のように書いたところ、「WebSecurityConfigurerAdapterなんて無い!」と怒られてしまいました。

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.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{
    @Override
    public void configure(HttpSecurity http) throws Exception {}
}

エラーのメッセージは以下の通りです。

The import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter cannot be resolved

こちらの記事によると、バージョンアップによって使えなくなったみたいです。

解決方法

以下のようにSecurityFilterChainをBeanで定義することで同じ振る舞いを実現できます。

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.web.SecurityFilterChain;

@Configuration
public class SecurityConfig {
	@Bean
	public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
		return http.build();
	}
}

メソッドの中身は基本的にこれまでと同じように書くことができます。
バージョン上がったことで動かなくなると、もともとあった記事が廃れてしまいますね…。

7
4
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
7
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?