2011-09-30 7 views
6

Bir Spring-MVC uygulamasına sahibim (örn. Spring'in dağıtıcı sunucu uygulamasını kullanıyorum). Ayrıca, kullanıcıların kimliğini doğrulamak için Spring Security kullanıyorum. Ben Bahar 'hareket memuru sunucu uygulamasını kullandığından (Ben doğru belgelerine anlama varsa), ben RequestContextHolder kullanabilmek için için benim web.xml''dekiRequestContextHolder ve HttpServletRequest.getUserPrincipal() Erişim AuthenticationSuccessHandler from

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

beyan etmek zorunda DEĞIL.

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 
    } 
} 

Eğer yukarıda belirtilen kodu, RequestContextHolder.currentRequestAttributes() ve HttpServletRequest.getUserPrincipal() çalışmıyor neden (bir kontrol cihazının içine çalışır) açıklayabilir misiniz:

Sorum arayüzünün org.springframework.security.web.authentication.AuthenticationSuccessHandler benim uygulanması anlamına gelir?

Teşekkürler!

+0

Euh, zaten 'RequestContextHolder.currentRequestAttributes' (istek yöntemi parametresidir) zaten var, ve siz zaten bir anahtara sahipsiniz (kimlik doğrulama yöntemi parametresidir). Bit anlamsız soru. BTW, isteğe göre ambalaj, bir temel ile temin etmek üzere, bir şekilde oturum açma sonra gelir ki, SecurityContextHolderAwareRequestFilter yapılır (ve dolayısıyla başarılı işleyicisi sonra). – MikeN

cevap

4

Yay güvenliği filtre tabanlı. Eğer yay güvenlik şeyler gerçekleştiğinde DispatcherServlet beri tanımlanan RequestContextListener henüz çağrılan edilmiş olmayacak ve bahar isteği bağlam kurulmuş olmayacak ihtiyaç nedeni budur.

+0

Teşekkürler. Bu, RequestContextHolder.currentRequestAttributes() 'bölümünü yanıtlar. Şimdi RequestContextListener' 'koduna bakmak ama orada göremiyorum herhangi' request.getUserPrincipal() ', bu yüzden' 'request' kayıtlı olsun UserPrincipal' yapmasıdır? Otomatik olarak 'onAuthenticationSuccess()' o yönteme içinde beni 'NullPointerException' veriyor beri girmeden önce kaydedilmiş değil. Aslında diğer filtre/memuru servlet'dir '' request' kayıtlı olsun UserPrincipal' gelmez nerede soruyorum ve bana 'onAuthenticationSuccess()' in 'request' onu okuyalım bazı dinleyici var mı? – rapt