• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

HttpSessionSecurityContextRepository를 사용안하는 문제

24.03.23 11:47 작성 24.03.27 10:15 수정 조회수 101

0

 

선생님 안녕하세요.

스프링 시큐리티 업데이트 버전만 손꼽아 기다리는 중입니다ㅠㅠ

우선 저는 프로젝트에서 기존 버전이 아니라 최신 버전으로 Security를 적용하고 session 저장소로 @EnableRedisHttpSession를 사용해 Redis를 사용하는 중입니다.
Redis에 sessionId가 저장되는것까지 확인했습니다.

선생님 강의보면서 참고하면서 잘 따라왔다고 생각했는데, 아래처럼 에러가 뜹니다. HttpSessionSecurityContextRepository에서 SPRING_SECURITY_CONTEXT Key를 이용해서 찾은게 SecurityContext가 아닌 Authentication 인증객체라는 내용 같은데요.

Authentication < SecurityContext < SecurityContextHolder가 아니라 Authentication < SecurityContextHolder가 된것 같습니다.

20240322 21:08:43.930 [http-nio-8080-exec-2] WARN o.s.s.w.c.HttpSessionSecurityContextRepository - SPRING_SECURITY_CONTEXT did not contain a SecurityContext but contained: '{authentication={authorities=[{authority=ROLE_ADMIN}], details={remoteAddress=0:0:0:0:0:0:0:1, sessionId=null}, authenticated=true, principal={password=null, username=sejinpark@email.com, authorities=[{authority=ROLE_ADMIN}], accountNonExpired=true, accountNonLocked=true, credentialsNonExpired=true, enabled=true}, credentials=null, name=sejinpark@email.com}}'; are you improperly modifying the HttpSession directly (you should always use SecurityContextHolder) or using the HttpSession attribute reserved for this class?


그래서 로그인한 상태에서 아래 내용을 출력해도 AnonymousUser가 뜨는데요.

SecurityContextHolder.getContext().getAuthentication()

 

인증 저장소 필터 챕터에서 SecurityContextPersistenceFilter를 deprecated되서 SecurityContextHolderFilter를 디버깅해봤더니, SecurityContextRepository 구현체 2개 (HttpSessionSecurityContextRepository, DelegatingSecurityContextRepository) 중에서 DelegatingSecurityContextRepository로만 가서 문제인것 같습니다. 아예 HttpSessionSecurityContextRepository로 들어가질 않더라구요. 이게 문제인것 같은데, 어느부분을 확인해아할지, 혹은 어떤 필터를 커스텀해야할지 여쭙고싶습니답

참고로 securityContext를 아래처럼 설정했습니다

 .securityContext((securityContext) -> securityContext
                .securityContextRepository(new DelegatingSecurityContextRepository(
                        new HttpSessionSecurityContextRepository()
                ))

답변 2

·

답변을 작성해보세요.

0

이 부분은 최신 버전과 관련된 내용이라 구체적으로 답변 드리기가 어려운점 양해 부탁드리면 곧 오픈 할 신규 강의를 참고해 주시면 감사하겠습니다.

0

인프런 AI 인턴님의 프로필

인프런 AI 인턴

2024.03.23

안녕하세요, 인프런 AI 인턴입니다. 세진님.

스프링 시큐리티의 인증 저장소와 관련하여 직면한 문제에 대해 설명해주셨네요. 로그인 상태에서 AnonymousUser가 반환되고, HttpSessionSecurityContextRepository를 올바르게 사용하지 못하는 문제로 보입니다.

스프링 시큐리티의 인증 과정과 저장소 관리기에 대한 깊은 이해가 필요한 상황입니다. 질문하신 내용과 아주 유사한 상황을 다룬 질문이 있어, 해당 내용이 도움이 될 것 같습니다.

  1. “Session 과 SecurityContext에 관련된 질문입니다.”에서는 SecurityContextHolder, SecurityContext, Authentication, 그리고 Session 사이의 관계 및 다루는 방법에 대해 논의되었습니다. 프로젝트에 적용하려는 HttpSessionSecurityContextRepository의 사용법에서 어려움을 겪고 계신 점과 관련이 있어 보입니다.

  2. “스프링 시큐리티 주요 아키텍처 이해 - 4. 인증 저장소 관련 질문”에서는 SecurityContext를 커스텀하여 사용하는 상황을 다뤘습니다. 이는 SecurityContextHolder를 통해 접근하는 방법과 관련된 문제를 해결하는 데 도움이 될 수 있습니다.

위 내용을 참고하여 HttpSessionSecurityContextRepositorySecurityContextPersistenceFilter대신 사용된 SecurityContextHolderFilter 관련 문제를 해결할 방향을 찾으시길 바랍니다.

저는 질문자님의 이해를 돕고자 지식공유자님의 기존 답변을 참고하여 유사한 답변 링크를 추천해드리고 있습니다.
현재 베타 기능으로 답변이 만족스럽지 않을 수 있는 점 양해 부탁드립니다. 🙏
추가적으로 궁금한 점이 있으시면, 이어서 질문해 주세요. 곧 지식공유자께서 답변해 주실 것입니다.