0

我正在使用Spring SecurityCAS遇到以下问题。当从CAS Server(例如无效的用户名/密码)抛出身份验证错误时,它会很好地显示在表单中,并使用标签正确显示:

<form:errors path="*" id="msg" cssClass="alert alert-danger" element="div"/>

但是在CAS Server返回成功并且AuthenticationException抛出的情况下,CAS Client没有任何错误显示为基本上CAS Client重定向回http://localhost:8080/cas/login?service=http%3A%2F%2Flocalhost%3A8080%2Fj_spring_cas_security_check

所以我无法真正显示客户端出了什么问题。JSP如果它抛出,是否有可能以某种方式显示来自客户端的错误AuthenticationException

4

1 回答 1

0

不确定这是否是超级干净和正确的方法,但我设法做到这一点的方法是使用 cookie。

我所要做的就是使用 并编写我的自定义错误代码 cookie来扩展SimpleUrlAuthenticationFailureHandler、获取最后一个身份验证异常。request.getSession().getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION)代码在 中Scala,但非常简单:

class CookieAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler{

  val clientErrorCookie = "clientError"

  override def onAuthenticationFailure(request: HttpServletRequest, response: HttpServletResponse, exception: AuthenticationException): Unit = {
    val authenticationException = SecurityUtility.getSessionAuthException(request).getOrElse(exception)

    ClientErrors.values
      .filter(clientError => clientError.exceptionClass.equals(authenticationException.getClass))
      .foreach(clientError => response.addCookie(new Cookie(clientErrorCookie, clientError.errorCode)))

    super.onAuthenticationFailure(request, response, authenticationException)
  }
}

然后,在CAS server侧面,我JSP通过以下方式显示错误:

<c:set var="clientErrorCookie" value="clientError"/>
<c:if test="${cookie.containsKey(clientErrorCookie)}">
                    <div class="alert alert-danger">
                        <spring:message code="error.client.authentication.${cookie.get(clientErrorCookie).value}"
                                        text="Client authentication error"/>
                    </div>
                </c:if>

在页面加载并显示错误后,我刚刚删除了该 cookie JS

function deleteCookie(name) {
            document.cookie = name + '=; Path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
        }

$(document).ready(function(){
          deleteCookie('${clientErrorCookie}');
        }
于 2017-04-27T15:13:10.747 回答