2010-08-09 35 views
16

Ho utilizzato Spring Security 3.0 per il meccanismo di accesso al nostro sito Web utilizzando una pagina di accesso dedicata. Ora ho bisogno che quella pagina di login sia invece una finestra lightbox/pop-up su ogni pagina web del nostro sito, dove al momento del login ottengo un risultato AJAX indipendentemente dal fatto che abbia avuto successo o meno. Qual è il modo migliore per farlo con Spring Security e Spring Webmvc 3.0?login ajax con molla webMVC e sicurezza a molla

risposta

13

Sul lato client è possibile simulare un normale invio di moduli al proprio URL di accesso tramite ajax. Per esempio, in jQuery:

$.ajax({ 
    url: "${pageContext.request.contextPath}/j_spring_security_check", 
    type: "POST", 
    data: $("#loginFormName").serialize(), 
    beforeSend: function (xhr) { 
     xhr.setRequestHeader("X-Ajax-call", "true"); 
    }, 
    success: function(result) { 
     if (result == "ok") { 
      ... 
     } else if (result == "error") { 
      ... 
     } 
    } 
}); 

Sul lato server, è possibile personalizzare AuthenticationSuccessHandler e AuthenticationFailureHandler per restituire un valore invece di reindirizzamento. Perché probabilmente avete bisogno di una pagina di login normale così (per il tentativo di accedere a una pagina protetta tramite URL diretto), si dovrebbe dire chiamate ajax dalle chiamate normali, ad esempio, utilizzando intestazione:

public class AjaxAuthenticationSuccessHandler implements AuthenticationSuccessHandler { 
    private AuthenticationSuccessHandler defaultHandler; 

    public AjaxAuthenticationSuccessHandler() { 

    } 
    public AjaxAuthenticationSuccessHandler(AuthenticationSuccessHandler defaultHandler) { 
     this.defaultHandler = defaultHandler; 
    } 

    public void onAuthenticationSuccess(HttpServletRequest request, 
     HttpServletResponse response, Authentication auth) 
     throws IOException, ServletException { 
    if ("true".equals(request.getHeader("X-Ajax-call"))) { 
     response.getWriter().print("ok"); 
     response.getWriter().flush(); 
    } else { 
     defaultHandler.onAuthenticationSuccess(request, response, auth); 
    } 
} 
} 
+0

non ho capito. nome utente e password stanno arrivando a null da requst – gsagrawal

7

ho fatto qualcosa di simile (grazie axtavt):

public class AjaxAuthenticationSuccessHandler extends 
    SimpleUrlAuthenticationSuccessHandler { 

public void onAuthenticationSuccess(HttpServletRequest request, 
     HttpServletResponse response, Authentication auth) 
     throws IOException, ServletException { 
    if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) { 
     response.getWriter().print(
       "{success:true, targetUrl : \'" 
         + this.getTargetUrlParameter() + "\'}"); 
     response.getWriter().flush(); 
    } else { 
     super.onAuthenticationSuccess(request, response, auth); 
    } 
}} 

ho scelto di estendere il semplice gestore di successo per il comportamento di default sulle richieste non Ajax. Ecco l'XML per farlo funzionare:

<http auto-config="false" use-expressions="true" entry-point-ref="authenticationProcessingFilterEntryPoint"> 
    <custom-filter position="FORM_LOGIN_FILTER" ref="authenticationFilter" /> 
... 
... 
</http> 

<beans:bean id="authenticationProcessingFilterEntryPoint" 
    class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint"> 
    <beans:property name="loginFormUrl" value="/index.do" /> 
    <beans:property name="forceHttps" value="false" /> 
</beans:bean> 

<beans:bean id="authenticationFilter" class= 
    "org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter"> 
    <beans:property name="authenticationManager" ref="authenticationManager"/> 
    <beans:property name="filterProcessesUrl" value="/j_spring_security_check"/> 
    <beans:property name="sessionAuthenticationStrategy" ref="sas" /> 
    <beans:property name="authenticationFailureHandler" ref="failureHandler"/> 
    <beans:property name="authenticationSuccessHandler" ref="successHandler"/> 
</beans:bean> 

<beans:bean id="successHandler" class="foo.AjaxAuthenticationSuccessHandler"> 
    <beans:property name="defaultTargetUrl" value="/login.html"/> 
</beans:bean> 

<beans:bean id="failureHandler" class="foo.AjaxAuthenticationFailureHandler" /> 
Problemi correlati