Ho un'applicazione Spring-MVC (ad esempio sto utilizzando il servlet di Spring dispatcher). Sto anche utilizzando Spring Security per autenticare gli utenti. Da quando uso servlet dispatcher della primavera, non devo dichiarareAccesso a RequestContextHolder e HttpServletRequest.getUserPrincipal() da AuthenticationSuccessHandler
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
nel mio web.xml in modo da essere in grado di utilizzare RequestContextHolder
(se ho capito bene la documentazione).
La mia domanda si riferisce alla mia implementazione dell'interfaccia 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
}
}
Potrebbe spiegare perché nel codice di cui sopra, e RequestContextHolder.currentRequestAttributes()
HttpServletRequest.getUserPrincipal()
non funzionano (che funzionano all'interno di un Controller)?
Grazie!
Euh, hai già 'RequestContextHolder.currentRequestAttributes' (è il parametro del metodo di richiesta) e hai già il principal (è il parametro del metodo di autenticazione). Domanda inutile bit. Btw, il wrapping della richiesta, per fornirgli un principal, viene fatto in SecurityContextHolderAwareRequestFilter, che viene dopo il login form (e quindi dopo il gestore di successo). – MikeN