2011-09-30 4 views
6

Mam aplikacji Wiosna-MVC (tj używam wiosną za dyspozytora serwletu). Używam także Spring Security do uwierzytelniania użytkowników. Ponieważ używam wiosną za dyspozytora serwletu, nie trzeba deklarowaćDostęp RequestContextHolder i HttpServletRequest.getUserPrincipal() z AuthenticationSuccessHandler

<listener> 
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
    </listener> 

w moim web.xml, aby móc użyć RequestContextHolder (jeśli dobrze rozumiem, dokumentacja).

Moje pytanie odnosi się do mojej implementacji interfejsu org.springframework.security.web.authentication.AuthenticationSuccessHandler:

public class AuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler { 

    @Override 
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException { 

     int timeout = 60*60; 

     //does work 
     request.getSession().setMaxInactiveInterval(timeout); //60 minutes 
     System.out.println("Session timeout of user: " + authentication.getName() + " has been set to: " + timeout + " seconds."); 

     /* 
     //does not work 
     session().setMaxInactiveInterval(timeout); //60 minutes 
     System.out.println("Session timeout of user: " + request.getUserPrincipal().getName() + " has been set to: " + timeout + " seconds."); 
     */ 

     //now restore the default work flow (SavedRequestAwareAuthenticationSuccessHandler is the default AuthenticationSuccessHandler that Spring uses, 
     // see: http://static.springsource.org/spring-security/site/docs/3.0.x/reference/core-web-filters.html#form-login-flow-handling) 
     (new SavedRequestAwareAuthenticationSuccessHandler()).onAuthenticationSuccess(request, response, authentication); 
    } 

    public static HttpSession session() { 
     ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); 
     return attr.getRequest().getSession(true); // true == allow create 
    } 
} 

mógłbyś wyjaśnić dlaczego w powyższym kodzie, RequestContextHolder.currentRequestAttributes() i HttpServletRequest.getUserPrincipal() nie działa (działają one wewnątrz Controller)?

Dzięki!

+0

EUH, masz już „RequestContextHolder.currentRequestAttributes” (jest to metoda parametr request), i masz już kapitał (jest to metoda uwierzytelniania parametr). Bezsensowne pytanie. BTW, owijanie wniosku o dostarczenie jej mocodawcy, odbywa się w SecurityContextHolderAwareRequestFilter, który przychodzi po form-login (a więc po obsługi sukces). – MikeN

Odpowiedz

4

Wiosna bezpieczeństwo jest oparte na filtr. Dlatego potrzebne są RequestContextListener zdefiniowane od DispatcherServlet nie zostały jeszcze wywoływana, gdy rzeczy wiosna-security dzieje i kontekst prośba wiosna nie zostały ustanowione.

+0

Dzięki. Odpowiada to części 'RequestContextHolder.currentRequestAttributes()'. Teraz patrzę na kodzie 'RequestContextListener' ale nie można tam zobaczyć każdy' request.getUserPrincipal() ', więc skąd' UserPrincipal' zostaną zapisane na 'request'? Nie jest on automatycznie zapisywany przed wpisaniem 'onAuthenticationSuccess()', ponieważ daje mi 'NullPointerException' w tej metodzie. Właściwie to pytam, gdzie w innym apletie filtru/dyspozytora 'UserPrincipal' zostaje zapisany na' request', i czy jest jakiś detektor, który pozwoli mi go odczytać z 'request' w' onAuthenticationSuccess() '? – rapt