cas5.3.2单点登录-自定义错误信息提示(十八)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34021712/article/details/82261959

原文地址,转载请注明出处: https://blog.csdn.net/qq_34021712/article/details/82261959     ©王赛超 

上一篇,我们在登录页面添加了验证码,但是在测试的时候发现,无论是验证码错误,还是密码错误,都是提示的 认证信息无效。我们需要更改错误信息提示来增加用户体验。

cas自定义错误信息需要以下几步:

1.创建 一个异常类继承javax.security.auth.login.AccountExpiredException
2.配置异常到application.properties中
3.在messages_zh_CN.properties 配置文件中,配置异常弹出的消息
使用maven的Overlay编译好之后,默认已经有messages_zh_CN.properties文件了,直接拷贝过来,在基础上进行修改。

具体操作

1.自定义异常类

用户未找到异常

package com.wangsaichao.cas.exception;

import javax.security.auth.login.AccountException;

/**
 * @author: wangsaichao
 * @date: 2018/8/31
 * @description: 用户没找到异常
 */
public class MyAccountNotFoundException extends AccountException {
    public MyAccountNotFoundException() {
        super();
    }

    public MyAccountNotFoundException(String msg) {
        super(msg);
    }
}

验证码错误异常

package com.wangsaichao.cas.exception;

import javax.security.auth.login.AccountException;

/**
 * @author: wangsaichao
 * @date: 2018/8/31
 * @description: 验证码错误异常类
 */
public class CaptchaErrorException extends AccountException {

    public CaptchaErrorException() {
        super();
    }

    public CaptchaErrorException(String msg) {
        super(msg);
    }
}

2.在application.properties中添加异常信息

#自定义错误信息
cas.authn.exceptions.exceptions=com.wangsaichao.cas.exception.CaptchaErrorException,com.wangsaichao.cas.exception.MyAccountNotFoundException

3.配置messages_zh_CN.properties

messages_zh_CN.properties是从编译好的cas中拷贝过来的,直接拷贝到resourece根路径下,并添加自己的异常,也将之前的异常进行更改,完整内容如下:

#Welcome Screen Messages

screen.welcome.welcome=欢迎来到中央认证系统。默认的认证处理器支持那些用户名等于密码的账号,开发者可以试试看。
screen.welcome.security=出于安全考虑,一旦您访问过那些需要您提供凭证信息的应用时,请操作完成之后<a href="logout">登出</a>并关闭浏览器。
screen.welcome.instructions=请输入您的用户名和密码.
screen.welcome.label.netid=用户名:
screen.welcome.label.netid.accesskey=n
screen.welcome.label.password=密\u3000码:
screen.welcome.label.password.accesskey=p
screen.welcome.label.warn=转向其他站点前提示我。
screen.welcome.label.warn.accesskey=w
screen.welcome.button.login=登录
screen.welcome.button.clear=重置

logo.title=转到Apereo网站首页
copyright=版权所有 &copy; 2005&ndash;2012 Apereo, Inc. 保留全部权利。

# Blocked Errors Page
screen.blocked.header=访问被拒绝
screen.blocked.message=输错密码次数太多,账号被锁定。

#Confirmation Screen Messages
screen.confirmation.message=单击 <a href="{0}">这里</a> ,便能够访问到目标应用。

#Generic Success Screen Messages
screen.success.header=登录成功
screen.success.success=您已经成功登录中央认证系统。
screen.success.security=出于安全考虑,一旦您访问过那些需要您提供凭证信息的应用时,请操作完成之后<a href="logout">登出</a>并关闭浏览器。

#Logout Screen Messages
screen.logout.header=注销成功
screen.logout.success=您已经成功退出CAS系统,谢谢使用!
screen.logout.security=出于安全考虑,请关闭您的浏览器。


screen.service.sso.error.header=在访问到到目标服务前,你必须经过重新认证的考验
screen.service.sso.error.message=你正试图访问要求重新认证的服务。请尝试进行<a href="{0}">再次认证</a>。


username.required=必须录入用户名。
password.required=必须录入密码。


#自己添加的
captcha.required=必须输入验证码
authenticationFailure.CaptchaErrorException=验证码错误
authenticationFailure.MyAccountNotFoundException=用户不存在
screen.login.welcome=欢迎请登录

# Authentication failure messages
authenticationFailure.AccountDisabledException=这个账户被禁用了。
authenticationFailure.AccountLockedException=这个账户被上锁了。
authenticationFailure.CredentialExpiredException=你的密码过期了。
authenticationFailure.InvalidLoginLocationException=你不能从这个工作站登录。
authenticationFailure.InvalidLoginTimeException=你的账户现在被禁止登录了。
authenticationFailure.AccountNotFoundException=认证信息无效。
authenticationFailure.FailedLoginException=认证信息无效。
authenticationFailure.UNKNOWN=认证信息无效。

INVALID_REQUEST_PROXY=必须同时提供'pgt''targetService'参数
INVALID_TICKET_SPEC=校验票根失败。您可能采用服务票根来校验代理票根,或没有将renew设为true。
INVALID_REQUEST=必须同时提供'service''ticket'参数
INVALID_TICKET=未能够识别出目标 ''{0}''票根
INVALID_SERVICE=票根''{0}''不符合目标服务
INVALID_PROXY_CALLBACK=所提供的代理回调网址''{0}''不能提供认证。
UNAUTHORIZED_SERVICE_PROXY=所提供的服务''{0}''没有权限使用CAS代理的认证方式。

screen.service.error.header=未认证授权的服务
screen.service.error.message=不允许使用CAS来认证您访问的目标应用。
screen.service.empty.error.message=CAS的服务记录是空的,没有定义服务。 \
希望通过CAS进行认证的应用程序必须在服务记录中明确定义。

# Password policy
password.expiration.warning=你的密码会在{0}天内过期。请立刻<a href="{1}">修改你的密码</a>。
password.expiration.loginsRemaining=在<strong>必须</strong>修改密码之前,你还剩{0}次登录。
screen.accountdisabled.heading=这个账户已经被禁用了。
screen.accountdisabled.message=请联系系统管理员来重新获得访问权限。
screen.accountlocked.heading=这个账户已经被锁住了。
screen.accountlocked.message=请联系系统管理员来重新获得访问权限。
screen.expiredpass.heading=你的密码已经过期了。
screen.expiredpass.message=请<a href="{0}">修改你的密码</a>。
screen.mustchangepass.heading=你必须修改你的密码。
screen.mustchangepass.message=请<a href="{0}">修改你的密码</a>。
screen.badhours.heading=现在你的账户被禁止登录了。
screen.badhours.message=请稍后再试。
screen.badworkstation.heading=你不能从这个工作站登录。
screen.badworkstation.message=请联系系统管理员来重新获得访问权限。

# OAuth
screen.oauth.confirm.header=授权
screen.oauth.confirm.message=要授权"{0}"访问你全部个人信息吗?
screen.oauth.confirm.allow=允许

# Unavailable
screen.unavailable.heading=CAS无法使用
screen.unavailable.message=在试图完成你的请求时出错。请通知你的技术支持或重试。

4.页面上使用错误信息

从上面的properties中可以找到screen.login.welcome 这个属性 这是我自定义的,可以在页面使用以下方式调用

<h2 th:text="#{screen.login.welcome}"></h2>

测试

这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_34021712/article/details/82261959