한울이

[SPRING] Spring security ChangeSessionIdAuthenticationStrategy 이슈 본문

PROGRAMMING

[SPRING] Spring security ChangeSessionIdAuthenticationStrategy 이슈

gksdnf050 2023. 10. 13. 16:00

1. 문제상황

  • spring security 를 사용하여 인증 인가 시스템을 구현해 놓은 프로젝트에서 로그인을 하던중 'creationTime key must not be null' 이라는 error 가 발생

2. 프로젝트 설정 상태

  • Spring security, RedisIndexedSessionRepository 사용 중

3. 해결 과정

  • 검색
    • 구글링을 통해 error 가 발생한 원인을 찾아보던 중 비슷한 이슈가 발생한 사례를 찾음

https://github.com/spring-projects/spring-session/issues/2021

 

Allow Customizing Redis Session Mapper · Issue #2021 · spring-projects/spring-session

Describe the bug We are using redis to save sessions in spring cloud gateway, and see some missing key errors in the log. Versions: spring-session-data-redis:2.3.3.RELEASE 2022-02-08 01:04:07.219,E...

github.com

  1. 이슈를 읽어 보다가 동시 요청이 발생 했을 때 이러한 error 가 나타난다는 힌트를 얻었다.
  2. 그리고 이슈가 발생한 사람들의 설정이 ChangeSessionIdAuthenticationStrategy 를 사용하고 있다는 것을 알게되었다.
  • debugging

위에서 알게된 내용을 토대로 debugging 을 시작했는데 현재 프로젝트도 ChangeSessionIdAuthenticationStrategy 를 사용하고 있었다. 따라서 ChangeSessionIdAuthenticationStrategy 의 applySessionFixation 메소드에 break point (쓰레드 단위로) 를 걸고 2개의 같은 로그인 요청을 하고 debuggin 을 시작했다.

break point 부터 천천히 코드를 따라가다가 RedisIndexedSessionRepository 에서 saveChangeSessionId() 메소드에서 원인을 찾았다.

첫번째 요청 과 두번째 요청 둘다 같은 originalSessionIdKey 값을 가지고 있는데 첫번째 요청이 먼저 완료되고 나서 (session key 값 변경 완료) 두번째 요청에서 rename 을 하려고 할때 originalSessionIdKey 에 해당하는 session 이 없어서 rename 이 실패하고 있었다.

그래서 두번째 요청은 변경 된 sessionId 값을 가지고 saveChangeSessionId() 이후에 saveDelta() 로 delta 에 저장되어 있던 값만 저장하는 session 이 생기게 되었다.

  • 첫번째 요청으로 변경된 session

  • 동시에 접근한 두번째 요청으로 새로 생성된 session

즉 두번째 session 에 creationTime 필드가 없어서 이후에 다시 로그인을 할 때  'creationTime key must not be null' 이라는 error 가 발생했 던 것이었다.

 

4. 해결

SessionFixation protection 전략 들중 changeSessionId 대신 migrateSession 을 사용하도록 변경 하였다.

migrateSession 을 사용하면 아예 새로운 세션을 만들고 기존의 세션의 attribute를 migration 해주는 방식이라 이전 세션의 키값이 변경되어서 발생 했던 문제가 나타나지 않게 되었다.

 

 

참조

https://docs.spring.io/spring-security/reference/index.html

 

Spring Security :: Spring Security

If you are ready to start securing an application see the Getting Started sections for servlet and reactive. These sections will walk you through creating your first Spring Security applications. If you want to understand how Spring Security works, you can

docs.spring.io

https://github.com/spring-projects/spring-session/issues/2021

 

Allow Customizing Redis Session Mapper · Issue #2021 · spring-projects/spring-session

Describe the bug We are using redis to save sessions in spring cloud gateway, and see some missing key errors in the log. Versions: spring-session-data-redis:2.3.3.RELEASE 2022-02-08 01:04:07.219,E...

github.com

 

Comments