AuthenticationManager を取得する必要はありません。HttpSecurity の javadocから、以下は問題なく動作するはずです。
@Configuration
@EnableWebSecurity
public class RememberMeSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/**").hasRole("USER")
.and()
.formLogin()
.permitAll()
.and()
// Example Remember Me Configuration
.rememberMe();
}
}
もちろん、グローバル AuthenticationManager を使用している場合、これも機能します。
@Configuration
@EnableWebSecurity
public class RememberMeSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth)
throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/**").hasRole("USER")
.and()
.formLogin()
.permitAll()
.and()
// Example Remember Me Configuration
.rememberMe();
}
}
唯一の違いは、最初の例では AuthenticationManger を HttpSecurity に分離し、2 番目の例では AuthenticationManager をグローバル メソッド セキュリティまたは別の HttpSecurity (WebSecurityConfigurerAdapter) で使用できるようにすることです。
これが機能する理由は、.rememberMe() が AuthenticationManager、UserDetailsService を自動的に見つけて、RememberMeAuthenticationFilter を作成するときにそれを使用するためです。また、適切な RememberMeServices も作成するため、その必要はありません。もちろん、カスタマイズしたい場合は .rememberMe() に追加のオプションがあります。追加のオプションについては、RememberMeConfigurer javadocを参照してください。
AuthenticationManager インスタンスへの参照が本当に必要な場合は、次の操作を実行できます。
@Configuration
@EnableWebSecurity
public class RememberMeSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private AuthenticationManagerBuilder auth;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth)
throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
@Bean
public AuthenticationManager authenticationManager() {
return auth.build();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/**").hasRole("USER")
.and()
.formLogin()
.permitAll()
.and()
// Example Remember Me Configuration
.rememberMe();
}
}
複数の AuthenticationManager インスタンスが必要な場合は、次の操作を実行できます。
@Autowired
private ObjectPostProcessor<Object> opp;
public AuthenticationManager authenticationManager()
throws Exception {
return new AuthenticationManagerBuilder(opp)
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER").and()
.and()
.build();
}
public AuthenticationManager authenticationManager2()
throws Exception {
return new AuthenticationManagerBuilder(opp)
.inMemoryAuthentication()
.withUser("admin").password("password").roles("ADMIN").and()
.and()
.build();
}
注これは、QUIESENT_POST_PROCESSOR を使用する代わりに @Autowired アノテーションを使用して実際の ObjectPostProcessor を使用していることを除いて、事前に持っていたものとほぼ同じです。
PS: RC2 をお試しいただきありがとうございます。