2010년 5월 1일, 2막

springboot 2.x spring security 중복로그인 방지, logout 시 session 삭제 안될때 처리 본문

Computer/Tips

springboot 2.x spring security 중복로그인 방지, logout 시 session 삭제 안될때 처리

창천(蒼天) 2020. 9. 8. 11:58

출처 : aljjabaegi.tistory.com/508

 

springboot 2.x spring security 중복로그인 방지, logout 시 session 삭제 안될때 처리

springboot 2.x spring security 중복로그인 방지, logout 시 session 삭제 안될때 처리 기본적인 로그인 처리 방법에 대해서는 아래의 link를 참고하세요. link : springboot 2.x + spring security m..

aljjabaegi.tistory.com

 

기본적인 로그인 처리 방법에 대해서는 아래의 link를 참고하세요.

 

link : springboot 2.x + spring security mariadb login 구현 WebSecurityConfigurerAdapter, configAuthentication 사용

 

중복 로그인 처리를 하기 위해서는 위의 link에서 구현한 WebSecurityConfig 클래스를 수정해주면 됩니다. 

configure 메소드내에 .logout() 을 수정하고 .sessionManagement()를 추가해줍니다.

 

.and().logout()
      .logoutUrl("/logout")  /* 로그아웃 url*/
      .logoutSuccessUrl("/login")  /* 로그아웃 성공시 이동할 url */
      .invalidateHttpSession(true)  /*로그아웃시 세션 제거*/
      .deleteCookies("JSESSIONID")  /*쿠키 제거*/
      .clearAuthentication(true)    /*권한정보 제거*/
      .permitAll()
.and().sessionManagement()
      .maximumSessions(1) /* session 허용 갯수 */
      .expiredUrl("/login") /* session 만료시 이동 페이지*/
      .maxSessionsPreventsLogin(true); /* 동일한 사용자 로그인시 x, false 일 경우 기존 사용자 session 종료*/

 

하지만 invalidateHttpSession(true) 가 정상작동하지 않습니다.

로그아웃시 세션이 삭제되지 않기 때문에 로그인 후 로그아웃을 하고 다시 로그인을 하면 에러페이지로 연결이 됩니다.

이를 해결하기 위해서 WebSecurityConfig 내에 httpSessionEventPublisher 메소드를 추가하여 @Bean 등록을 해줍니다.

 

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

 



출처: https://aljjabaegi.tistory.com/508 [알짜배기 프로그래머]

'Computer > Tips' 카테고리의 다른 글

[C#] Windows Service 만들기  (0) 2020.11.03
SpringBoot 세션타임 관리하는 방법  (0) 2020.09.15
Windows용 MySQL 백업 배치 파일 만들기  (0) 2020.09.02
[C#] 트레이 아이콘 적용  (0) 2020.07.19
Git 사용법 관련  (0) 2020.04.24