2011-09-30 29 views
6

Tôi có một ứng dụng Spring-MVC (tức là tôi đang sử dụng servlet dispatcher của Spring). Tôi cũng đang sử dụng Spring Security để xác thực người dùng. Kể từ khi tôi sử dụng servlet phối của mùa xuân, tôi không phải khai báoTruy cập RequestContextHolder và HttpServletRequest.getUserPrincipal() từ AuthenticationSuccessHandler

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

trong web.xml của tôi để có thể sử dụng RequestContextHolder (nếu tôi hiểu đúng các tài liệu hướng dẫn).

Câu hỏi của tôi liên quan đến việc thực hiện của tôi về giao diện 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 
    } 
} 

Ông có thể giải thích tại sao trong mã đề cập ở trên, RequestContextHolder.currentRequestAttributes()HttpServletRequest.getUserPrincipal() không làm việc (họ làm việc bên trong một Controller)?

Cảm ơn!

+0

Euh, bạn đã có 'RequestContextHolder.currentRequestAttributes' (đó là tham số phương thức yêu cầu), và bạn đã có hiệu trưởng (đó là tham số phương thức xác thực). Bit câu hỏi vô nghĩa. Btw, gói yêu cầu, để cung cấp nó với một hiệu trưởng, được thực hiện trong SecurityContextHolderAwareRequestFilter, xuất hiện sau khi đăng nhập biểu mẫu (và do đó sau khi xử lý thành công). – MikeN

Trả lời

4

Bảo mật mùa xuân dựa trên bộ lọc. Đó là lý do tại sao bạn cần RequestContextListener được định nghĩa vì DispatcherServlet sẽ không được gọi ra khi công cụ bảo mật mùa xuân xảy ra và bối cảnh yêu cầu mùa xuân sẽ không được thiết lập.

+0

Cảm ơn. Điều đó trả lời phần 'RequestContextHolder.currentRequestAttributes()'. Bây giờ tôi nhìn vào mã của 'RequestContextListener' nhưng không thể thấy có bất kỳ' request.getUserPrincipal() 'nào, vì vậy' UserPrincipal' được lưu ở đâu trên 'request'? Nó không được lưu tự động trước khi nhập 'onAuthenticationSuccess()' vì nó cho tôi 'NullPointerException' trong phương thức đó. Tôi thực sự yêu cầu nơi khác trong bộ lọc/dispatcher servlet hiện 'UserPrincipal' được lưu trên' request', và có một số người nghe sẽ cho phép tôi đọc nó từ 'request' trong' onAuthenticationSuccess() '? – rapt