LoginSignup
16
12

More than 5 years have passed since last update.

spring boot で 特定の url だけcsrf チェック除外する(修正済)

Last updated at Posted at 2016-03-23

spring-boot で WebSecurity つかうと /hoge/... への POST でも csrf トークンのチェックが入るので、特定 url だけ除外してもらう。

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    ...
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        ...
        http.antMatcher("/hoge/**").csrf().disable(); // これ
        ...
    }
    ...

は間違ってた!!!!

http.antMatcher("/hoge/**").csrf().disable();

じゃなくて

http.csrf().ignoringAntMatchers("/hoge/**");

だった。

メソッドチェインの結果に関してよくわからんのでもっと調べます。

ちょっと調べました。

前者の http.antMatcher("/hoge/**").csrf(); みると、

org.springframework.security.config.annotation.web.configurers.CsrfConfigurer@ac461fc[csrfTokenRepository=org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository@3a75ad21,
requireCsrfProtectionMatcher=org.springframework.security.web.csrf.CsrfFilter$DefaultRequiresCsrfMatcher@1f5ca70,
ignoredCsrfProtectionMatchers=[],
securityBuilder=org.springframework.security.config.annotation.web.builders.HttpSecurity@8c6f8a4,
objectPostProcessor=org.springframework.security.config.annotation.SecurityConfigurerAdapter$CompositeObjectPostProcessor@274f1586]

となってて、後者の http.csrf().ignoringAntMatchers("/hoge/**") は

!!!org.springframework.security.config.annotation.web.configurers.CsrfConfigurer@ac461fc[csrfTokenRepository=org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository@3a75ad21,
requireCsrfProtectionMatcher=org.springframework.security.web.csrf.CsrfFilter$DefaultRequiresCsrfMatcher@1f5ca70,
ignoredCsrfProtectionMatchers=[Ant [pattern='/hoge/**']],
securityBuilder=org.springframework.security.config.annotation.web.builders.HttpSecurity@8c6f8a4,
objectPostProcessor=org.springframework.security.config.annotation.SecurityConfigurerAdapter$CompositeObjectPostProcessor@274f1586]

こうなってた。antMatcher さきにやっても csrf() はそれに対して適用されるわけじゃないんですね。

直感的に書いてもダメなもんはダメという例だったのかな。

16
12
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
16
12