Springbootで@AuthenticatedPrincipalしてもnullが入る

今さっきやらかしてしまったのでメモ。

SpringSecurity の設定が間違っていると @AuthenticatedPrincipal した引数に null が入ってしまう。

1
2
3
4
5
6
7
8
9
10
11
12
// これは間違い。
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/mypage/**");
}

// 正しくはこう書く
public void configure(WebSecurity web) throws Exception {
http.authorizeRequests()
.antMatchers("/mypage").permitAll()
}

間違いの方も必ず間違いというわけではなく、静的なコンテンツは上の書き方でよい。
上の書き方は SpringSecurity 自体をバイパスしてしまうので、ログイン状態であっても
ログインユーザーの情報が取れないと思われる。