原创

cas4.0.0中cas-server登录页自定义提示消息实现方法

网上关于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文件获取错误提示信息。

~阅读全文-人机检测~

微信公众号“Java精选”(w_z90110),专注Java技术干货分享!让你从此路人变大神!回复关键词领取资料:如Mysql、Hadoop、Dubbo、Spring Boot等,免费领取视频教程、资料文档和项目源码。微信搜索小程序“Java精选面试题”,内涵3000+道Java面试题!

涵盖:互联网那些事、算法与数据结构、SpringMVC、Spring boot、Spring Cloud、ElasticSearch、Linux、Mysql、Oracle等

评论

  1. #1

    什么 (2017/05/27 14:39:21)回复
    无聊可以用于打发时间,不要调皮捣蛋阿!

  2. #2

    平凡人生 (2017/05/16 12:52:06)回复
    程序员代码还能自救,这时代掌握一门技术是多么的重要阿。

  3. #3

    yoodb (2017/05/10 10:48:19)回复
    无聊的时候分享一些代码也是一种生活吧,天天闲着也是闲着,是不是。

  4. #4

    yoodb (2017/05/26 15:45:14)回复
    需要源码的请评论留下联系方式,我有时间回主动联系你的,哈哈!或者你直接联系我也行。

分享:

支付宝

微信