Доступ к RequestContextHolder и HttpServletRequest.getUserPrincipal() из AuthenticationSuccessHandler

У меня есть приложение Spring-MVC (т.е. я использую сервлет-диспетчер Spring). Я также использую Spring Security для аутентификации пользователей. Поскольку я использую Spring's dispatcher servlet, мне НЕ нужно объявлять

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

в моем web.xml, чтобы иметь возможность использовать RequestContextHolder (если я правильно понимаю документацию).

Мой вопрос относится к моей реализации интерфейса 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
    }
}

Не могли бы вы объяснить, почему в приведенном выше коде, RequestContextHolder. currentRequestAttributes() и HttpServletRequest.getUserPrincipal() не работают (внутри контроллера они работают)?

Спасибо!

6
задан rapt 30 September 2011 в 02:58
поделиться