LoginSignup
1
1

Spring Security5.8で非推奨になったantMatchersがrequestMatchersで置き換えられない件

Last updated at Posted at 2023-10-12

最近結構書き方の変わったSpring Security

Spring Security5.4~あたりで、いろんなものが非推奨になったり、6.0以降では削除されたり、いろいろ変わりましたね。
WebSecurityConfigurerAdapterが使えなくなって、configureメソッド各種を@Beanアノテーションを付与し、Beanとして公開するように変更するのが一番大きな変更かな…と思ってます。
それで5.8~非推奨になったメソッドで、antMatchersmvcMatcherがあります。
Spring公式から出ているドキュメントでも、requestMatchersに置き換えられると記載してあります。

でも、確かにコンパイルエラーにはならないけど、起動するとエラーで画面表示できないんですよね…
いやもしかしたらconfigure(HttpSecurity)メソッドから置き換えたsecurityFilterChain(HttpSecurity)メソッドなら、requestMatchersで置き換えてもうまく動作するのかもしれない。サンプルコードもこのメソッドのが多いし。
でもうちでantMatchers使ってたのは、configure(WebSecurity)メソッドから置き換えたwebSecurityCustomizer()メソッドなんですよねー。
困った困った。
でもどこ探してもみんなrequestMatchersに置き換えられるって書いてあるばかり。少なくともサンプルコードは大体そう。あと大体securityFilterChainの話で、WebSecurityCustomizer はあんまり出てこない。
もう少し時間経つまで待つしかないかなーと思ってたんですけど、やっと見つけました。


5.3以前
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/ignore1", "/ignore2");
    }
5.4~
@Configuration
public class SecurityConfiguration {

    @Bean
    public WebSecurityCustomizer webSecurityCustomizer() {
        return (web) -> web.ignoring().antMatchers("/ignore1", "/ignore2");
    }
}
5.8~
@Configuration
public class SecurityConfiguration {

    @Bean
    public WebSecurityCustomizer webSecurityCustomizer() {
        return (web) -> web.ignoring().requestMatchers(
                new AntPathRequestMatcher("/ignore1"),
                new AntPathRequestMatcher("/ignore2"));
    }
}

requestMatchersに置き換えるときは、そのままStringでパターンを渡すんじゃなくて、AntPathRequestMatcherでラップしてオブジェクトを渡すと、元のように動きます。
コンストラクタ以外にもAntPathRequestMatcher.antMatcher(String)も使えます。引数が可変長Stringのものはなかったですが。
RegexRequestMatcher.regexMatcher(String)も使えるっぽい。
mvcMatcherからの置き換えは、MvcRequestMatcherのコンストラクタだと思う。

とりあえず動いてよかったよ…

1
1
1

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