您可以使用以下命令检查请求是否为 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
}
}