【问题标题】:Android App Authentication through Grails spring-security-core通过 Grails spring-security-core 进行 Android 应用身份验证
【发布时间】:2012-08-06 17:39:21
【问题描述】:
我有一个 grails-app 正在运行:
http:localhost:8080/myapp
通过 ajax POST 在以下位置进行身份验证:
http:localhost:8080/myapp/j_spring_security_check
我正在尝试从在同一地址发布相同 ajax 请求的 Android 应用程序进行身份验证,身份验证似乎没问题,但服务器在登录后向 android 应用程序发送“重定向”,这是我的问题:如何避免在 android 上进行重定向应用程序以及如何在 android 应用程序中访问当前用户属性?
提前感谢您提供的任何提示!
【问题讨论】:
标签:
android
rest
authentication
grails
spring-security
【解决方案1】:
您可以使用以下方法检查请求是否为 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
}
}