网上关于cas4.0.0版本的文章比较少,公司需要验证用户信息账号密码是否正确,长度限制等并提示关于对象的错误信息提示,因此本站总结了关于如何自定义cas4.0.0验证登录时错误提示消息,对于cas-server登录页登录时自定义提示消息的实现方法有什么疑问,在本站素文宅blog.yoodb.com中留言即可。
cas4实现自定义验证登录提示错误信息,具体步骤配置
1、首先在cas-server-core工程中新建PasswordAuthenticationException.java类,代码如下:
package org.jasig.cas.authentication.handler; public class PasswordAuthenticationException extends java.security.GeneralSecurityException { private static final long serialVersionUID = 1L; /** * Constructs a PasswordAuthenticationException with no detail message. * A detail message is a String that describes this particular exception. */ public PasswordAuthenticationException() { super(); } /** * Constructs a PasswordAuthenticationException with the specified * detail message. A detail message is a String that describes * this particular exception. * * <p> * * @param msg the detail message. */ public PasswordAuthenticationException(String msg) { super(msg); } }
2、修改验证错误返回异常提示AuthenticationExceptionHandler.java类,具体代码如下:
static { DEFAULT_ERROR_LIST.add(javax.security.auth.login.AccountLockedException.class); DEFAULT_ERROR_LIST.add(javax.security.auth.login.FailedLoginException.class); DEFAULT_ERROR_LIST.add(javax.security.auth.login.CredentialExpiredException.class); DEFAULT_ERROR_LIST.add(javax.security.auth.login.AccountNotFoundException.class); DEFAULT_ERROR_LIST.add(org.jasig.cas.authentication.AccountDisabledException.class); DEFAULT_ERROR_LIST.add(org.jasig.cas.authentication.InvalidLoginLocationException.class); DEFAULT_ERROR_LIST.add(org.jasig.cas.authentication.InvalidLoginTimeException.class); /*----新增--------*/ DEFAULT_ERROR_LIST.add(org.jasig.cas.authentication.handler.PasswordAuthenticationException.class); }
3、修改抽象用户密码验证AbstractUsernamePasswordAuthenticationHandler.java类,具体代码如下:
@Override protected final HandlerResult doAuthentication(final Credential credential) throws GeneralSecurityException, PreventedException { final UsernamePasswordCredential userPass = (UsernamePasswordCredential) credential; if (userPass.getUsername() == null || userPass.getUsername().equals("")) { throw new AccountNotFoundException("Username is null.(blog.yoodb.com)"); } final String transformedUsername= this.principalNameTransformer.transform(userPass.getUsername()); if (transformedUsername == null || transformedUsername.equals("")) { throw new AccountNotFoundException("Transformed username is null."); } /*----新增(简单例子)--------*/ if(8 < password.length()){ throw new PasswordAuthenticationException("password is too long ---(blog.yoodb.com)"); } userPass.setUsername(transformedUsername); return authenticateUsernamePasswordInternal(userPass); }
4、在login-webflow.xml配置文件中增加关于PasswordAuthenticationException类配置,具体配置代码如下:
<action-state id="handleAuthenticationFailure"> <evaluate expression="authenticationExceptionHandler.handle(currentEvent.attributes.error, messageContext)" /> <transition on="AccountDisabledException" to="casAccountDisabledView"/> <transition on="AccountLockedException" to="casAccountLockedView"/> <transition on="CredentialExpiredException" to="casExpiredPassView"/> <transition on="InvalidLoginLocationException" to="casBadWorkstationView"/> <transition on="InvalidLoginTimeException" to="casBadHoursView"/> <transition on="FailedLoginException" to="generateLoginTicket"/> <transition on="AccountNotFoundException" to="generateLoginTicket"/> <transition on="PasswordAuthenticationException" to="generateLoginTicket"/> <transition on="UNKNOWN" to="generateLoginTicket"/> </action-state>
5、在messages.properties文件中增加提示信息,具体信息如下:
authenticationFailure.PasswordAuthenticationException=密码超出限制范围
分析:步骤3新增代码DEFAULT_ERROR_LIST.add(org.jasig.cas.authentication.handler.PasswordAuthenticationException.class);是为了之后代码中判断返回class异常类是否有匹配项,存在后判断会生成authenticationFailure.PasswordAuthenticationException字符串,通过读取messages.properties文件获取错误提示信息。
什么 (2017/05/27 14:39:21)回复
无聊可以用于打发时间,不要调皮捣蛋阿!