2017-04-25 6 views
0

Spring SecurityCASを使用していますが、次の問題があります。認証エラーは、それが形にも示されているCAS Server(例えば無効なユーザー名/パスワード)からスローされ、使用して正しく表示されている場合、タグ:Spring Security CAS:login.jspでクライアントエラーを表示する

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

しかし、場合によってはCAS Serverリターンの成功とAuthenticationExceptionCAS Clientなしにスローされたときのエラーは基本的に表示されますCAS Clienthttp://localhost:8080/cas/login?service=http%3A%2F%2Flocalhost%3A8080%2Fj_spring_cas_security_check

私は本当にクライアント側で何が間違って表示することはできません。 AuthenticationExceptionを投げた場合、クライアントからのエラーを何らかの形で表示することは可能でしょうか?JSP

答えて

0

これはスーパークリーンで正しい方法であるかどうかはわかりませんが、私が管理してきた方法はクッキーを使用しています。

私がしなければならなかったのは、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}'); 
     }