강의

멘토링

로드맵

Inflearn brand logo image

인프런 커뮤니티 질문&답변

Dae-Hyun Kim님의 프로필 이미지
Dae-Hyun Kim

작성한 질문수

스프링 시큐리티 완전 정복 [6.x 개정판]

인가 설정 실시간 반영하기

동시세션제어 기능에서 로그아웃하기

작성

·

67

0

안녕하세요

"인가 설정 실시간 반영하기" 코드에서 동시 세션 제어 기능을 추가할려고 합니다.

http.sessionManagement(session -> session

.maximumSessions(1)

.maxSessionsPreventsLogin(true)

)

위 코드를 추가한 후 REST 방식으로 로그인후 로그아웃을 하면 SessionRegistry의 principals 객체에 있는 정보가 삭제되지 않아 다시 로그인을 시도하면 인증오류가 발생합니다.

로그아웃을 할 경우 SessionRegistry.removeSessionInformation(sessionId)가 호출되어 principals 정보가 삭제되어야 할 것 같은데 제가 잘못 이해하고 있는 지 궁금합니다.

 

답변 3

1

Dae-Hyun Kim님의 프로필 이미지
Dae-Hyun Kim
질문자

@GetMapping(value = "/api/logout")
    public String logout(HttpServletRequest request, HttpServletResponse response) {
        Authentication authentication = SecurityContextHolder.getContextHolderStrategy().getContext().getAuthentication();
        if (authentication != null) {
            new SecurityContextLogoutHandler().logout(request, response, authentication);
        }

        return "logout";
    }

위 코드를 사용합니다.

잠시 구글링을 해보니

@Bean
public static ServletListenerRegistrationBean httpSessionEventPublisher() {
    return new ServletListenerRegistrationBean(new HttpSessionEventPublisher());
}

이렇게 HttpSessionEventPublisher를 등록하면 SeesionRegistry가 이벤트를 감지하여 onApplicationEvent()에서 removeSessionInformation()을 호출한다고 하여 테스트 해보니 로그아웃시 잘 동작하네요

0

정수원님의 프로필 이미지
정수원
지식공유자

Rest 라면 세션을 사용하고 있지 않은가요?

보통 세션기반으로 로그아웃하면 세션이 무효화되고 그 때 세션폐기 이벤트가 발생하면서 SessionRegisry 가 세션정보를 삭제하는 것으로 압니다

Rest 로 로그아웃할 때 어떤 식으로 로그아웃이 진행되는가요?

Dae-Hyun Kim님의 프로필 이미지
Dae-Hyun Kim

작성한 질문수

질문하기