6

最近、RC1 から spring-security-3.2.0.RC2 に更新しました。ブログ投稿によると、QUIESCENT_POST_PROCESSOR が削除されました。以前は、以下のような AuthenticationManager Bean を作成していました。

@Bean(name = {"defaultAuthenticationManager", "authenticationManager"})
public AuthenticationManager defaultAuthenticationManager() throws Exception {
    return new AuthenticationManagerBuilder(null).userDetailsService(context.getBean(MyUserDetailsService.class)).passwordEncoder(new Md5PasswordEncoder()).and().build();
}

だから私はそれを次のように変更しました:

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws BeansException, Exception {
    auth.userDetailsService(context.getBean(MyUserDetailsService.class)).passwordEncoder(new Md5PasswordEncoder());
}

残念ながら、AuthenticationManager を取得できなくなりました。また、次のように RememberMeAuthenticationFilter を作成しています。

@Bean(name = { "defaultRememberMeAuthenticationFilter", "rememberMeAuthenticationFilter" })
protected RememberMeAuthenticationFilter defaultRememberMeAuthenticationFilter() throws Exception {
    return new RememberMeAuthenticationFilter(defaultAuthenticationManager(), context.getBean(DefaultRememberMeServices.class));
}

ご覧のとおり、AuthenticationManager を取得する必要がありますが、方法がわかりませんか???

4

2 に答える 2

15

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、UserDetailsS​​ervice を自動的に見つけて、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 をお試しいただきありがとうございます。

于 2013-11-06T14:01:43.487 に答える