java – Spring安全性自定义AuthenticationException消息

前端之家收集整理的这篇文章主要介绍了java – Spring安全性自定义AuthenticationException消息前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

嗨,我需要在Spring安全登录表单中添加一个新的异常,除了我想要自己的错误消息之外,一切都很完美(直到现在它显示错误登录/密码”).

我从用户名密码验证过滤器覆盖默认尝试验证方法

@Override
public Authentication attemptAuthentication(final HttpServletRequest request,final HttpServletResponse response)
{
if (!myTest()) {
throw new CustomAuthenticationException("custom message");
}
}

我的例外:

public class CustomAuthenticationException extends AuthenticationException {

    public CustomAuthenticationException(final String message)
    {
        super(message);
    }

    public CustomAuthenticationException(final String message,final Throwable cause)
    {
        super(message,cause);
    }

}

在我的控制器中,我在SPRING_SECURITY_LAST_EXCEPTION下看到了我的异常,但错误消息始终是来自坏凭证的消息,我怎么能改变它?

谢谢

最佳答案
您应该尝试LOCALIZING SPRING SECURITY MESSAGES.
尝试将这些行添加到ApplicationContext.xml文件中.春天安全豆的其余部分在哪里.

你应该找到你的春季默认类< KEY,MESSAGE>存储.让myMessage文件具有相同的KEY和本地化的MESSAGE.

根据您的评论,您的项目中有一个messages.properties.因此,您需要做的就是为此属性文件中的每个密钥设置一个MESSAGE,以获得完全本地化的消息:

AbstractAccessDecisionManager.accessDenied= your message in any language
AbstractSecurityInterceptor.authenticationNotFound=
AbstractUserDetailsAuthenticationProvider.badCredentials=
AbstractUserDetailsAuthenticationProvider.credentialsExpired=
AbstractUserDetailsAuthenticationProvider.disabled=
AbstractUserDetailsAuthenticationProvider.expired=
AbstractUserDetailsAuthenticationProvider.locked=
AbstractUserDetailsAuthenticationProvider.onlySupports=
AccountStatusUserDetailsChecker.credentialsExpired=
AccountStatusUserDetailsChecker.disabled=
AccountStatusUserDetailsChecker.expired=
AccountStatusUserDetailsChecker.locked=
AclEntryAfterInvocationProvider.noPermission=
AnonymousAuthenticationProvider.incorrectKey=
BindAuthenticator.badCredentials=
BindAuthenticator.emptyPassword=
CasAuthenticationProvider.incorrectKey=
CasAuthenticationProvider.noServiceTicket=
ConcurrentSessionControlStrategy.exceededAllowed=
DigestAuthenticationFilter.incorrectRealm=
DigestAuthenticationFilter.incorrectResponse=
DigestAuthenticationFilter.missingAuth=
DigestAuthenticationFilter.missingMandatory=
DigestAuthenticationFilter.nonceCompromised=
DigestAuthenticationFilter.nonceEncoding=
DigestAuthenticationFilter.nonceExpired=
DigestAuthenticationFilter.nonceNotNumeric=
DigestAuthenticationFilter.nonceNotTwoTokens=
DigestAuthenticationFilter.usernameNotFound=
JdbcDaoImpl.noAuthority=
JdbcDaoImpl.notFound=
LdapAuthenticationProvider.badCredentials=
LdapAuthenticationProvider.credentialsExpired=
LdapAuthenticationProvider.disabled=
LdapAuthenticationProvider.expired=
LdapAuthenticationProvider.locked=
LdapAuthenticationProvider.emptyUsername=
LdapAuthenticationProvider.onlySupports=
PasswordComparisonAuthenticator.badCredentials=
PersistentTokenBasedRememberMeServices.cookieStolen=
ProviderManager.providerNotFound=
RememberMeAuthenticationProvider.incorrectKey=
RunAsImplAuthenticationProvider.incorrectKey=
SubjectDnX509PrincipalExtractor.noMatching=
SwitchUserFilter.noCurrentUser=
SwitchUserFilter.noOriginalAuthentication=
原文链接:https://www.f2er.com/spring/431910.html

猜你在找的Spring相关文章