【问题标题】:JASIG Central Authentication Service 4, custom authentication and custom exceptionJASIG Central Authentication Service 4、自定义身份验证和自定义异常
【发布时间】:2014-10-24 08:26:59
【问题描述】:

我将 CAS 从 3.5.2 升级到 4。

在 3.5.2 版本中,我创建了一个 CustomAuthentication 类扩展 AbstractJdbcUsernamePasswordAuthenticationHandler 来执行我的自定义身份验证并根据我的需要抛出异常

示例:

throw new BadCredentialsAuthenticationException("error.accountNotExist");

所以在 message.properties 中我只是添加了 error.accountNotExist=Account 不存在。

那么前端就会返回上面的字符串。

在 CAS 版本 4 中,AbstractJdbcUsernamePasswordAuthenticationHandler 发生了变化。不再支持 BadCredentialsAuthenticationException。那么如何在 cas 版本 4 中执行它以返回我想要的任何自定义字符串?

【问题讨论】:

  • 简单,抛出 new AccountNotFoundException("Account not exist"),为什么将消息存储在属性文件中如此重要?
  • 因为问题标题是自定义异常,意思是有多个AccountNotFoundException,喜欢账号不活跃,账号过期,账号现在不启用。但返回消息是通用的“无效凭据”。为了实现这一点,需要自定义身份验证。也许你有更简单的方法来添加自定义异常?
  • 不,你得到了更好的解决方案,但我仍然在想为什么向用户显示超详细的消息如此重要,无论如何,为什么不将“authenticationFailure.AccountNotFoundException”更新为更合适的“Account” Not Exist”而不是不恰当的文本“Invalid credentials”?
  • 它只是一个例子,我选择它更容易让人们理解我所说的。我确实更改了消息而不是“无效凭据”并且还添加了很多自定义异常:)

标签: single-sign-on cas jasig


【解决方案1】:

由于没有 1 个答案,我尝试自己找出答案。

添加自定义异常的步骤

1 创建新的异常类扩展 LoginException

messages.properties 中的2个为新添加的异常添加相应的消息

login-webflow.xml 中的 3 --> handleAuthenticationFailure action-state 添加<transition on="<yourCustomExcception>" to="generateLoginTicket"/>

4 创建一个CustomAuthenticationExceptionHandler.java,复制原始源代码,并将新异常添加到DEFAULT_ERROR_LIST

cas-servlet.xml 中的 5 将 authenticationExceptionHandler 替换为您的 CustomAuthenticationExceptionHandler

【讨论】:

    猜你喜欢
    • 2019-09-18
    • 2013-03-28
    • 2014-03-30
    • 2019-05-14
    • 2010-12-17
    • 1970-01-01
    • 2020-04-01
    • 2022-12-10
    • 1970-01-01
    相关资源
    最近更新 更多