2012-08-09 34 views
0

我有一個Grails應用程序內運行上:Android應用程序認證通過Grails的春季安全核心

http:localhost:8080/myapp

與阿賈克斯POST驗證:

http:localhost:8080/myapp/j_spring_security_check

我是嘗試從Android應用程序進行身份驗證發佈相同的ajax請求在同一地址,身份驗證似乎一切正常,但服務器發送到Android應用程序登錄後的「重定向」,這裏是我的問題:浩避免在Android應用程序重定向和如何訪問Android應用程序到當前用戶屬性?

感謝提前任何提示!

回答

0

您可以檢查是否請求是通過使用ajaxrequest:

springSecurityService.isAjax(請求)

我決定寫我自己的ajaxSubmit的控制器動作,因爲我遇到了一些麻煩重定向問題作爲好。我純粹是爲了證明概念,所以這個例子可能不是那裏最好的。

def ajaxSubmit = { 
    if(!springSecurityService.isAjax(request)) { 
     redirect action: "authfail" 
     return 
    } 

    def token = new UsernamePasswordAuthenticationToken(params.j_username, params.j_password) 
    try { 
     UserDetails userDetails = userDetailsService.loadUserByUsername(params.j_username) 
     token.setDetails(userDetails) 
    } catch (UsernameNotFoundException unfe) { 
     request.getSession().setAttribute(WebAttributes.AUTHENTICATION_EXCEPTION, unfe) 
     redirect action: "authfail" 
     return 
    } 

    try { 
     def authSession = authenticationManager.authenticate(token) 
     SCH.getContext().setAuthentication(authSession) 
     redirect action: "ajaxSuccess" 
     return 
    } catch (AuthenticationException ae) { 
     request.getSession().setAttribute(WebAttributes.AUTHENTICATION_EXCEPTION, ae) 
     redirect action: "authfail" 
     return 
    } 
}