我是Grails的新手。我已经配置了spring安全插件核心2.0 RC2。我可以使用存储在我的数据库中的用户登录。我配置了一个自定义登录表单,当我尝试在未登录的情况下访问安全URL时,我会被重定向到我的自定义表单。但是,当我使用错误的密码或不存在登录时,我无法显示错误消息。
我的Grails.config中有以下片段:
grails.plugin.springsecurity.auth.loginFormUrl = '/user/login'
grails.plugin.springsecurity.failureHandler.defaultFailureUrl = '/user/login'
这似乎奏效了。我确实成功地达到了我的状态。我使用的是插件附带的默认LoginController。
我已经在我的表格上尝试了以下所有内容:
<g:eachError>
<li><g:message error="${it}"/></li>
</g:eachError>
<g:eachError>
<li>${it}</li>
</g:eachError>
<g:if test="${flash.message}">
<li> class="error">${flash.message}</li>
</g:if>
无显示任何错误。我可以看到登录控制器在flash中放置错误:
def authfail() {
String msg = ''
def exception = session[WebAttributes.AUTHENTICATION_EXCEPTION]
if (exception) {
if (exception instanceof AccountExpiredException) {
msg = g.message(code: "springSecurity.errors.login.expired")
}
else if (exception instanceof CredentialsExpiredException) {
msg = g.message(code: "springSecurity.errors.login.passwordExpired")
}
else if (exception instanceof DisabledException) {
msg = g.message(code: "springSecurity.errors.login.disabled")
}
else if (exception instanceof LockedException) {
msg = g.message(code: "springSecurity.errors.login.locked")
}
else {
msg = g.message(code: "springSecurity.errors.login.fail")
}
}
if (springSecurityService.isAjax(request)) {
render([error: msg] as JSON)
}
else {
flash.message = msg
redirect action: 'auth', params: params
}
}
,但当我的页面显示时,它们似乎不见了。我可以看到错误事件是在我创建了一个安全事件侦听器来记录它们时引发的。Grails和Spring Security专家,我缺少什么?
我建议使用flash.error,它会将您的错误消息放在flash范围内。类似这样的东西:
if (exception instanceof AccountExpiredException) {
flash.error = g.message(code: "springSecurity.errors.login.expired")
}
然后在您的gsp中,您将需要以下内容:
<g:if test="${flash.error}">
<div class="errors">
<ul><li>${flash.error}</li></ul>
</div>