0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SpringSecurityの自作フィルタでセッション数を制御

0
Posted at

概要

ユーザーのセッション数を一つにしてくれというプロジェクトがありました。
UsernamePasswordAuthenticationFilterを拡張した環境にてSpringSecurityのユーザー単位の最大セッション数を変更できるか試したところ、ハマったのでとりあえず動いているコードだけでも載せておこうと思います。
同一の症状に悩まされてる方は参考にしてみてください。

環境

  • SpringBoot 3.5.8
  • Java 17
  • spring-boot-starter-security
  • spring-session-jdbc
  • UsernamePasswordAuthenticationFilterクラスを使用して拡張

方法

@EnableWebSecurityのアノテーションを指定しているクラス一つだけでできます。
追加したコードを参考にうまく組み込んでみてください。

//...
@EnableWebSecurity
@Configuration
public class WebSecurityConfig {

    //...

    //必要なBeanを追加
    @Bean
    protected SessionRegistryImpl sessionRegistry(){
        return new SessionRegistryImpl();
    }
    @Bean
    public CompositeSessionAuthenticationStrategy sessionAuthenticationStrategy(SessionRegistry sessionRegistry) {
        ConcurrentSessionControlAuthenticationStrategy concurrent =
                new ConcurrentSessionControlAuthenticationStrategy(sessionRegistry);
        SessionFixationProtectionStrategy fixation = new SessionFixationProtectionStrategy();
        RegisterSessionAuthenticationStrategy register = new RegisterSessionAuthenticationStrategy(sessionRegistry);
        return new CompositeSessionAuthenticationStrategy(List.of(concurrent, fixation, register));
    }
    
    //...

    @Bean
    public SecurityFilterChain securityFilterChain(
            HttpSecurity http ,
            AuthenticationManager authenticationManager,
            CompositeSessionAuthenticationStrategy sessionAuthStrategy
    ) throws Exception {
        //セッションの設定
        http.sessionManagement(
                httpSecuritySessionManagementConfigurer -> httpSecuritySessionManagementConfigurer
                    .sessionConcurrency(concurrent -> concurrent
                        .maximumSessions(1)
                        .maxSessionsPreventsLogin(false) //ログイン先負けあと勝ち
                        .sessionRegistry(sessionRegistry())
                        .expiredUrl("/login")
                    )
        );

        //...
        
        //カスタムフィルター変更部分
        CustomAuthenticationFilter filter = new CustomAuthenticationFilter();
        //セッション数変更に対応するため追加
        filter.setSessionAuthenticationStrategy(sessionAuthStrategy);
        
        //...
        
        http.addFilterAt(filter, CustomAuthenticationFilter.class);
        return http.build();
    }
}

備考

要はCompositeSessionAuthenticationStrategyを独自filterにセットするだけなんですが何故か記事が見つからなかったです。
HttpSecurityで設定するまでは見かけるのですが、そのあと何をすれば分からず迷子状態でした。
AIの知識で無理やり発案させて、動いたあとから余分なコードを消すという泥臭いことをしてます。
TIPSとして参考にしていただければ幸いです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?