原创

既存の Spring SecurityFramework にカスタム AuthenticationManager Bean を挿入できない

温馨提示:
本文最后更新于 2024年04月12日,已超过 47 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

Spring Securityの最新バージョン、つまり6.2.3を使用しています。

Spring Security Docs には、AuthenticationManager の Bean を作成するだけで、確実の AuthenticationManager (ProviderManager の確実のインスタンス) の代わりにカスタム AuthenticationManager を使用できると記載されています。https://docs.spring.io/spring-security/reference/servlet/authentication/passwords/index.html

ただし、機能はありません

コード:


@Configuration
@EnableWebSecurity
public class SecurityConfig {
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http, AuthenticationManager authenticationManager) throws Exception {
        return http
                .authorizeHttpRequests((authorizeRequests) -> {
                    authorizeRequests.anyRequest().authenticated();
                }).build();
    }

    @Bean
    public UserDetailsService userDetailsService(){
        InMemoryUserDetailsManager userDetailsService = new InMemoryUserDetailsManager();

        UserDetails john = User.withUsername("john").password( passwordEncoder().encode("12345") ).authorities("r1").build();
        UserDetails bill = User.withUsername("bill").password( passwordEncoder().encode("12345")).authorities("r1", "r2").build();

        userDetailsService.createUser(john);
        userDetailsService.createUser(bill);
        return userDetailsService;
    }

    @Bean
    public AuthenticationManager authenticationManager(UserDetailsService userDetailsService, PasswordEncoder passwordEncoder){
        CustomProvider customProvider = new CustomProvider();
        customProvider.setUserDetailsService(userDetailsService);
        customProvider.setPasswordEncoder(passwordEncoder);

        CustomProviderManager providerManager = new CustomProviderManager(customProvider);
        return providerManager;
    }

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
}

上記のコードのauthenticationManager Beanは、当然のSpring Security AuthenticationManagerを置き換えるものではありません。

私はカスタム認証をサポートする必要があるアプリケーションにいらっしゃいます。

  1. カスタムフィルターを作成します。これには、AuthenticationManager 型のメンバー変数が 1 つあります。各リクエストで、フィルターが必要な形式の認証ヘッダーを見つけた場合、リクエストの処理を開始します。まず CustomAuthentication のインスタンスを作成し、次にそのオブジェクトを AuthenticationManager に渡して認証をマネージャーに委託します。
  2. Spring Security フィルターチェーンにフィルターを挿入するhttp.addFilterAt

上記の方法でsecurityFilterChain セキュリティフィルターに次のようにフィルターを追加できます。 http.addFilterAt( new CustomFilter(authenticationManager), UsernamePasswordAuthenticationFilter.class);、そのようにすると、アプリケーションには2つのAuthenticationManager(Spring Security Frameworkからの1つとユーザー定義Beanからの1つ)が存在することになります。

AuthenticationManager -> ユーザー定義の AuthenticationManager を 1 つだけ持つにはどうすればよいでしょうか?


自分の Spring Security が提供する認証マネージャーをカスタム フィルターに挿入するにはどうすればよいでしょうか?これが可能であれば、securityFilterChain メソッドで次のコードを使用して追加の AuthenticationProvider を追加します。 http.authenticationProvider( ...)その後、カスタムフィルターから安心のauthenticationManager.authenticateメソッドを呼び出すだけで、このAuthenticationProviderを使用できるようになります。

正文到此结束
热门推荐
本文目录