복붙노트

[SPRING] 오류 페이지에서 null 인증을 반환하는 Spring SecurityContext

SPRING

오류 페이지에서 null 인증을 반환하는 Spring SecurityContext

403 (액세스 거부) 및 500 (내부 서버 오류)과 같은 오류에 대한 사용자 정의 오류 페이지를 작성하려고합니다. Velocity 템플릿에서 렌더링되고 사용자의 로켈을 사용하여 모든 메시지가 번역됩니다. 인증 및 로켈 분석은 응용 프로그램에서 올바르게 작동합니다.

원하는 페이지로 web.xml에서 위치를 설정하고 webmvc-context.xml에서 requet-to-view 컨트롤러를 통해 추가했습니다.

내가 만난 문제는 SecurityContextHolder.getContext (). getAuthentication ()이 오류 페이지 뷰에서 null을 반환한다는 것입니다. 내가 본 로그를 보면 :

06.10 14:42:26 DEBUG - context.HttpSessionSecurityContextRepository(HttpSessionSecurityContextRepository.java:351) -  - SecurityContext stored to HttpSession: 'org.springframework.security.core.context.SecurityContextImpl@ece7b0b7: Authentication: ...
06.10 14:42:26 DEBUG - context.SecurityContextPersistenceFilter(SecurityContextPersistenceFilter.java:89) -  - SecurityContextHolder now cleared, as request processing completed
06.10 14:42:26 DEBUG - servlet.DispatcherServlet(DispatcherServlet.java:691) -  - DispatcherServlet with name 'foo' processing GET request for [/foo/app/error/403.html]

따라서 Spring이나 Tomcat이 오류 페이지로 리다이렉트하고 요청이 완료되면 컨텍스트가 지워집니다. 새로운 "요청"은 스프링 보안 필터를 거치지 않으므로 컨텍스트를 복원하지 않습니다.

일반적인 방법은 작동하지 않지만 AbstractTemplateView가 다음을 기록하기 때문에 인증 정보가 세션의 어딘가에있는 것으로 보입니다.

Exposing session attribute 'SPRING_SECURITY_CONTEXT' with value [org.springframework.security.core.context.SecurityContextImpl@edfbd958: Authentication: org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken@edfbd958...

정상 페이지와 오류 페이지가 모두 동일하게 작동하도록 올바르게 어떻게 입 력합니까?

해결법

  1. ==============================

    1.실행중인 문제는 예외를 오류 페이지로 변환하는 ExceptionTranslationFilter가 SecurityContextRepository에서 인증을 가져와 SecurityContextHolder에 넣는 SecurityContextPersistenceFilter 앞에 오는 것입니다. 요청이 완료되면 SecurityContextPersistenceFilter는 SecurityContextHolder에서 정보를 다시 가져옵니다.

    실행중인 문제는 예외를 오류 페이지로 변환하는 ExceptionTranslationFilter가 SecurityContextRepository에서 인증을 가져와 SecurityContextHolder에 넣는 SecurityContextPersistenceFilter 앞에 오는 것입니다. 요청이 완료되면 SecurityContextPersistenceFilter는 SecurityContextHolder에서 정보를 다시 가져옵니다.

    SecurityContextHolder를 지우는 이유는 SecurityContextHolder가 일반적으로 스레드 로컬이고 서블릿 컨테이너가 스레드를 재사용하는 경우 (대부분 이렇게 함) 실수로 다른 사람에게 자격 증명을 줄 수 있기 때문입니다.

    일반적으로 ExceptionTranslationFilter는 변환되지 않는 예외의 위험을 피하기 위해 가장 바깥쪽에있는 필터입니다.

    최선의 방법은 아마도 SecurityContextRepository를 사용하고 (언급 한 것처럼 HTTP 세션을 사용하는) 사용자 정의 ExceptionTranslationFilter를 작성하고 SecurityContextHolder가 아닌 SecurityContextRepository를 통해 인증에 대한 액세스를 제공하는 것입니다. 사용자가 로그인하지 않으면 인증은 여전히 ​​null입니다.

  2. ==============================

    2.문제는 springSecurityFilterChain이 ERRORS를 가로 채지 않는 것일 수 있습니다. web.xml에서 매핑을 변경하여보십시오.

    문제는 springSecurityFilterChain이 ERRORS를 가로 채지 않는 것일 수 있습니다. web.xml에서 매핑을 변경하여보십시오.

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>
    
  3. from https://stackoverflow.com/questions/6305801/spring-securitycontext-returning-null-authentication-on-error-pages by cc-by-sa and MIT license