これはスーパークリーンで正しい方法であるかどうかはわかりませんが、私が管理してきた方法はクッキーを使用しています。
私がしなければならなかったのは、SimpleUrlAuthenticationFailureHandler
を拡張して、最後に を使用して認証例外を取得し、私のカスタムエラーコードのクッキーを書きます。コードは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}');
}