개발/Spring

Spring Security 중복 로그인 방지

뽀글뽀글 개발자 2024. 5. 21. 13:22

시큐리티 4,3 버전에서 XML 설정 방식을 사용했다.

 

securiry-context.xml에 아래 내용을 추가한다.

<session-management>
    <concurrency-control max-sessions="1" error-if-maximum-exceeded="false" expired-url="/signin.do"></concurrency-control>
</session-management>
  • max-sessions : 중복 허용할 세션 수
  • error-if-maximum-exceede="false": 첫 번째 사용자의 세션이 끊김
  • error-if-maximum-exceede="true": 두 번째 사용자의 세션이 끊김
  • expired-url: 중복 로그인 발생 시 이동할 페이지

 

만일 유저의 인증 정보를 담는 UserDetail 객체를 Custom해서 사용하는 경우에는 반드시 equals and hashcode를 구현해줘야한다.

그렇지 않으면 유저 정보를 비교할 때 서로 다른 유저로 인지해서 중복 로그인한 세션을 끊을 수 없다.

 

 

 

Reference

세션 클러스터링과 시큐리티 없이 중복 로그인을 관리하는 방법을 정리해놓은 블로그