一、前提
在CAS中,为了让登陆界面更加友好,在CAS验证用户和密码出错时给用户提示相应的错误信息。虽然CAS已经内置了一些异常,比如用户名密码错误、未知的用户名错误等。
CAS AuthenticationException结构如下图,CAS已经内置了一些异常,比如用户名密码错误、未知的用户名错误等。在CAS WEBFLOW流转的过程中,对应的action就会捕获这些TicketCreationException,并在表单中显示该异常信息。

对应的登陆页面:casLoginView.jsp
<form:errors path="*" id="msg" cssClass="errors" element="div" />
这个标签代表把所有错误提示信息显示出来。
相关的信息提示配置文件在如下路径:
WEB-INF\classes\messages_zh_CN.properties
提示的值用了native2ascii 转换后的值
但是还是有部分提示信息需要我们定义的。自定义提示信息的步骤如下:
二、自定义错误信息
A、定义错误类
代码如下:
package com.victop.platform.cas.server.exception; import org.jasig.cas.authentication.handler.BadUsernameOrPasswordAuthenticationException; public class UnUsernameAuthenticationException extends BadUsernameOrPasswordAuthenticationException { /** * */ private static final long serialVersionUID = 201825730136492339L; /** Static instance of UnknownUsernameAuthenticationException. */ public static final UnUsernameAuthenticationException ERROR = new UnUsernameAuthenticationException(); /** The code description of this exception. */private static final String CODE = "error.authentication.credentials.bad.unregister.username"
; /** * * Default constructor that does not allow the chaining of exceptions and * * uses the default code as the error code for this exception. */ public UnUsernameAuthenticationException() { super(CODE); } /** * * Constructor that allows for the chaining of exceptions. Defaults to the * * default code provided for this exception. * * * * @param throwable the chained exception. */ public UnUsernameAuthenticationException(final Throwable throwable) { super(CODE, throwable); } /** * * Constructor that allows for providing a custom error code for this class. * * Error codes are often used to resolve exceptions into messages. Providing * * a custom error code allows the use of a different message. * * * * @param code the custom code to use with this exception. */ public UnUsernameAuthenticationException(final String code) { super(code); } /** * Constructor that allows for chaining of exceptions and a custom error * code. @param code the custom error code to use in message resolving. * * @param throwable the chained exception. */ public UnUsernameAuthenticationException(final String code, final Throwable throwable) { super(code, throwable); } }
B:添加错误提示信息
在messages_zh_CN.properties 添加错误提示信息.
error.authentication.credentials.bad.unregister.username=\u8fd9\u662f\u6211\u5b9a\u4e49\u7684\u9519\u8bef\u4fe1\u606f\u0020\u0062\u0079\u0020\u006a\u0069\u006e
c:handler类抛出相应错误
代码如下:
package com.victop.platform.cas.server.handler; import org.jasig.cas.authentication.handler.AuthenticationException; import org.jasig.cas.authentication.handler.support.AbstractUsernamePasswordAuthenticationHandler; import org.jasig.cas.authentication.principal.UsernamePasswordCredentials; import org.springframework.util.StringUtils; import com.victop.platform.cas.server.exception.UnUsernameAuthenticationException; public class VictopSimpleAuthenticationHandler extends AbstractUsernamePasswordAuthenticationHandler { @Override protected boolean authenticateUsernamePasswordInternal( UsernamePasswordCredentials credentials) throws AuthenticationException { final String username = credentials.getUsername(); final String password = credentials.getPassword(); if (StringUtils.hasText(username) && StringUtils.hasText(password) && username.equals(getPasswordEncoder().encode(password))) { log .debug("User [" + username + "] was successfully authenticated."); return true; }else{ throw new UnUsernameAuthenticationException(); } } }
结果如下:
注意: handler类一旦抛出错误后,不会执行下一个handler类而是直接翻来错误信息到页面。



浙公网安备 33010602011771号