복붙노트

[SPRING] Spring의 SecurityContextHolder.getContext (). getAuthentication ()은 RedirectView가 HTTPS / SSL에서 사용 된 후에 null을 반환합니다.

SPRING

Spring의 SecurityContextHolder.getContext (). getAuthentication ()은 RedirectView가 HTTPS / SSL에서 사용 된 후에 null을 반환합니다.

Tomcat에서 실행되는 전형적인 스프링 MVC가있다. 시스템이 HTTPS에서 실행되도록 전환 한 후 (모든 것이 정상 HTTP에서 정상적으로 작동 함) 로그인이 작동하지 않습니다. 그 이유는 RedirectView가 사용 된 후에 Spring의 SecurityContextHolder.getContext (). getAuthentication () 객체가 null이되기 때문입니다.

이미 해답을 찾았습니다. 단, viewResolver 빈 설정에서 redirectHttp10Compatible 속성을 false로 설정하도록 제안했습니다. 이것은 도움이되지 못했습니다.

또한 리디렉션을 통해 세션 ID가 동일하게 유지되고 연결 상태가 안전하다는 것을 확인했습니다. 즉, http와 https 사이의 변경 사항 (적어도 내가 알 수있는 한 최대의 문제는 아닙니다) 또는 그 반대입니다.

무엇이 문제 일 수 있습니까?

<beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">


  <http auto-config="true">
    <intercept-url pattern="/**" requires-channel="https" />

    <intercept-url pattern="/index*" access="ROLE_USER"/>


    <intercept-url pattern="/dashboard*" access="ROLE_USER" requires-channel="https"/>  

    <intercept-url pattern="/login*" access="ROLE_GUEST, ROLE_ANONYMOUS, ROLE_USER"/>
    <intercept-url pattern="/signin*" access="ROLE_GUEST, ROLE_ANONYMOUS, ROLE_USER"/>
    <intercept-url pattern="/signup*" access="ROLE_GUEST, ROLE_ANONYMOUS, ROLE_USER"/>    


    <form-login login-page="/home" 
                default-target-url="/home" 
                authentication-failure-url="/home?authentication_error=true"
                authentication-success-handler-ref="redefineTargetURL"
    />


    <anonymous username="guest" granted-authority="ROLE_GUEST" key="anonymousKey"/>
    <logout invalidate-session="true" logout-success-url="/logout?message=Logout Successful" />

    </http>



<authentication-manager alias="authenticationManager">
    <authentication-provider user-service-ref="userDetailsService" />
</authentication-manager>


<beans:bean id="redefineTargetURL" class="com.groupskeed.common.RedefineTargetURL" />
<beans:bean id="userDetailsService" class="com.groupskeed.security.UserDetailsServiceImpl" />

해결법

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

    1.리디렉션 후 SecurityContextHolder.getContext (). getAuthentication ()이 스레드 바운드이므로 null이됩니다. 그러나 세션에서 다시 채워야합니다. 따라서 세션에서 SPRING_SECURITY_CONTEXT 특성을 추적하십시오. 다음은 아이디어를 얻을 수있는 몇 가지 예제 코드입니다.

    리디렉션 후 SecurityContextHolder.getContext (). getAuthentication ()이 스레드 바운드이므로 null이됩니다. 그러나 세션에서 다시 채워야합니다. 따라서 세션에서 SPRING_SECURITY_CONTEXT 특성을 추적하십시오. 다음은 아이디어를 얻을 수있는 몇 가지 예제 코드입니다.

    HttpSession session = request.getSession(true);
    System.out.println(session.getAttribute("SPRING_SECURITY_CONTEXT"));
    

    Spring Security 문서에는 HTTPS / HTTP 전환이 세션을 망칠 수있는 방법에 대한 부분이있을 것입니다. 아마도 거기에 문제가 있다는 암시가있을 것입니다. http://static.springsource.org/spring-security/site/faq.html#d0e223

    위의 FAQ를 통해 세션이 응용 프로그램에서 어떻게 처리되는지 확인할 수 있습니다. 아마도 AuthenticationSuccessHandler 구현을 살펴볼 것입니다. (원할 경우 질문에 psot 할 수 있습니다.)

    웹 응용 프로그램에서 보안 컨텍스트가 처리되는 방법에 대한 자세한 내용은 다음을 참조하십시오 (5.4 절). http://static.springsource.org/spring-security/site/docs/3.0.x/reference/technical-overview. HTML

  2. from https://stackoverflow.com/questions/16952718/springs-securitycontextholder-getcontext-getauthentication-returns-null-aft by cc-by-sa and MIT license