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