Post

Spring 중복 로그인 방지

Spring 중복 로그인 방지

중복 로그인을 방지하는 방법

  1. 로그인 사용자 세션을 1개로 고정
  2. 로그인하려고 submit 하기 전 요청을 가로채서
  3. 세션에 이미 로그인한 사용자 정보가 존재할 경우,
  4. 새로운 로그인 사용자에게 기존 로그인 사용자의 로그인이 풀림을 경고
  5. 경고를 확인하고도 로그인할 경우, 1번 조건에 의해 기존 사용자는 로그아웃시킨다

💡 로그인 사용자 세션을 1개로 고정하는 것 .maximumSessions(1) // maximunSessions : Session 허용 개수

Spring Security를 사용해서 sercurityConfig을 수정

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    http.csrf().disable()
        .cors().disable()
        .authorizeHttpRequests(request -> request
                .dispatcherTypeMatchers(DispatcherType.FORWARD).permitAll()
                .anyRequest().authenticated()
        )
        .formLogin(login -> login
                .loginPage("/login")
                .loginProcessingUrl("/login_process")
                .defaultSuccessUrl("/", true)
                .permitAll()
        )
        .logout(withDefaults())
        .sessionManagement() // session 관리
        .maximumSessions(1) // maximunSessions : Session 허용 개수
        .maxSessionsPreventsLogin(false);
//maxSessionPreventsLogin : true 일 경우 기존에 동일한 사용자가 로그인한 경우에는 login 이 안된다. 
// false 일경우는 로그인이 되고 기존 접속된 사용자는 Session이 종료되고 로그아웃된다. false 가 기본이다.

    return http.build();
}

References: https://hoonzi-text.tistory.com/139

This post is licensed under CC BY 4.0 by the author.