Spring Security 기본편(7)에서 배운 것
SecurityContextHolder를 통해 인증된 세션 정보 (예: 사용자 ID, 권한 등) 를 가져와 Model에 담아 템플릿으로 전달
세션 관련 설정
사용자가 로그인을 진행한 뒤 사용자 정보는 SecurityContextHolder에 의해서 서버 세션에 관리됨
이때 세션에 관해 세션의 소멸 시간, 아이디당 세션 생성 개수 를 설정하는 방법
세션 소멸 시간 설정
세션 타임 아웃 설정을 통해 로그인 이후 세션이 유지되고 소멸하는 시간을 설정할 수 있음
세션 소멸 시점은 사용자의 마지막 요청 이후 설정한 시간 만큼 유지됨(기본 시간 1800초)
세션이 만료되면 사용자는 자동으로 로그아웃되며, 재인증을 요구함.
application.properties
//초 기반
server.servlet.session.timeout=1800
//분 기반
server.servlet.session.timeout=90m
다중 로그인 관련 설정
Authentication Persistence and Session Management :: Spring Security
Session fixation attacks are a potential risk where it is possible for a malicious attacker to create a session by accessing a site, then persuade another user to log in with the same session (by sending them a link containing the session identifier as a p
docs.spring.io
동일한 아이디로 다중 로그인을 진행할 경우에 대한 설정
1. 다중로그인을 허용할 것인가
- 허용시 sessionManagement() 메소드를 추가
2. 하나의 아이디에 대한 다중 로그인 허용 개수를 작성(동시접속 가능 갯수)
- maximumSession(int) : 하나의 아이디에 대한 다중 로그인 허용 개수
3. 다중 로그인 개수를 초과할 경우 어떻게 처리할 것인가(동시접속 가능 갯수 초과 시 어떻게 처리할 것인가)
- maxSessionPreventsLogin(boolean) : 다중 로그인 개수를 초과하였을 경우 처리 방법
- true : 초과시 새로운 로그인 차단
- false : 초과시 기존 세션 하나 삭제
동일한 계정으로 여러 사용자가 동시 접속할 경우 보안 위험이 증가할 수 있으므로, 비즈니스 요구 사항에 맞춰 다중 로그인 설정은 신중하게 고려해야함.
SpringSecurityConfig파일 예시
@Bean
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.sessionManagement((auth)->auth
.maximumSessions(1)
.maxSessionsPreventsLogin(true));
return http.build();
}
세션 고정 공격 보호 설정
세션 고정(Session Fixation) 공격: 공격자가 서버로부터 세션 ID를 먼저 획득한 후, 이 세션 ID를 사용자에게 심어 사용자가 인증(로그인)하면, 공격자도 동일한 세션 ID를 사용해 같은 인증 권한을 얻어 이를 악용하는 공격
sessionManagement() 메소드의 sessionFixation() 메소드를 통해서 세션 고정 공격 보호 설정할 수 있음.
- sessionMangement().sessionFiaxation().none(): 로그인 시 세셔 정보 변경 안함
- none으로 하면 세션고정공격으로부터 안전하지 않음
- sessionManagement().sessionFixation().newSession() : 로그인 시 세션 새로 생성
- 세션쿠키와 내부 세션을 로그인 진행시 새로 생성, 아이디 값들이 다 변경
- 세션 자체를 새로 생성하므로 가장 안전항 방식, 세션 정보가 새로 설정
- sessionManagement().sessionFixation().changeSessionId() : 로그인 시 동일한 세션에 대한 id 변경
- 동일한 세션을 유지하면서 세션 ID만 변경되므로, 기존 세션 데이터를 유지하면서도 세션 고정 공격을 방지하는 실용적인 방법
공식문서 코드
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) {
http
.sessionManagement((session) - session
.sessionFixation((sessionFixation) -> sessionFixation
.newSession()
)
);
return http.build();
}
구현 코드
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception{
http
.sessionManagement((auth) -> auth
.sessionFixation().changeSessionId());
return http.build();
}
'Spring Security_로그인 기능 구현 > Spring Security Basic' 카테고리의 다른 글
로그인 기능_Spring Security 기본편 (10) (0) | 2024.10.31 |
---|---|
로그인 기능_Spring Security 기본편 (9) (1) | 2024.10.22 |
로그인 기능_Spring Security 기본편 (7) (0) | 2024.10.11 |
로그인 기능_Spring Security 기본편 (6) (0) | 2024.10.07 |
로그인 기능_Spring Security 기본편 (5) (0) | 2024.10.01 |