스프링 시큐리티 중복로그인 검사 : Spring security duplication login check
스프링 시큐리티 중복로그인 검사 : Spring security duplication login check
중복로그인 검사는 spring security filter 를 활용하였다.
우선 동시 세션설정에서 세션을 1개만 생성되도록 설정한다.
<b:bean class="org.springframework.security.web.authentication.session.ConcurrentSessionControlAuthenticationStrategy">
<b:constructor-arg ref="sessionRegistry"/>
<b:property name="maximumSessions" value="1" />
<b:property name="exceptionIfMaximumExceeded" value="false" />
</b:bean>
GenericFilterBean 클래스를 상속받아서 DuplicationLoginCheckFilter 시큐리티 필터를 만든다.
loginProcessingUrl (즉 로그인 요청) 요청에 의해 로그인 처리가 실행되기전에 중복로그인체크필터를 실행될 수 있게 필터를 추가한다.
<custom-filter ref="duplicationLoginCheckFilter" before="FORM_LOGIN_FILTER" />
그리고 로그인 요청이 되었는 지를 감지하기 위해 loginProcessingUrl과 post 메서드 요청인지 필터에서 확인한다. 참인 경우 이미 로그인한 사용자가 있는 지 여부를 sessionRegistry 에서 확인한다.
프론트엔드에서 로그인 요청후 중복로그인에 관련해 오류가 발생한 경우 메세지를 출력하고 다음 이벤트를 실행한다.
// 중복로그인 체크인 경우...
if (code === 403) {
if (confirm(message)) {
$("#login #${ignoreParameterName}").val("success");
ajax();
} else {
$("#login #${ignoreParameterName}").val();
}
} else {
$('#message div').text(message);
$('#message').show(0, function() {
$(this).delay(1000).hide(0);
});
}
소스 : https://github.com/syakuis/springframework/tree/duplication-login-filter