Spring Security

2022-05-26

/etc/hosts

localhost とかの設定が書いてある

127.0.0.1       localhost
127.0.1.1       desktop.localdomain     desktop
192.168.11.17   host.docker.internal
192.168.11.17   gateway.docker.internal
127.0.0.1       kubernetes.docker.internal

2022-10-14

SecurityConfiguration

pre/post authorize

@EnableGlobalMethodSecurity(prePostEnabled = true) をつけることで有効化

configure(WebSecurity web)

  • 静的リソースのignore等を行うためにある.
    • Security Filter Chainから除外される
override fun configure(web: WebSecurity) {
  web.ignoring()
    .antMatchers("/css/**","/js/**","/images/**");
}

configure(http: HttpSecurity)

  • URLによって認証を切り替える時, Auth Filterの追加
override fun configure(http: HttpSecurity?) {
    require(http != null)
 
    http.csrf().disable()
    http
        .authorizeRequests()
        .mvcMatchers(
            "/api/login",
        ).permitAll()
    http.authorizeRequests()
        .anyRequest().authenticated()
    http
        .addFilter(authFilter())
    http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
    http.cors()
        .configurationSource(corsConfigurationSource())
}

Auth Filter

2022-12-19

流れ

  • PreAuthentifiatedFilter で認証情報を取り出し
    • フィルタ内の処理で、AuthenticationManager に認証処理を委譲し
    • 認証結果となる Authentication オブジェクトをセッションとスレッドローカルに保存する。
    • (Spring Securityは基本、Authentication オブジェクトを見て、認証、アクセス制御を行う。)
  • AuthenticationManager のデフォルト実装である ProviderManager は内部の AuthenticationProvider に処理を委譲する。今回は、Spring Securityが提供するPreAuthenticatedAuthenticationProvider をそのまま設定する。
  • PreAuthenticatedAuthenticationProvider 内部ではreAuthentifiatedFilter で取得された認証情報からAuthenticationUserDetailsService を利用して、ユーザー情報を作成しているので、今回は AuthenticationUserDetailsService を拡張したクラスを設定する。

2022-12-19