Spring安全性自定义AuthenticationException消息

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

我从用户名密码validationfilter覆盖默认尝试validation方法:

@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下看到了我的exception,但错误消息始终是来自坏凭证的消息,我怎么能改变它?

谢谢

你应该尝试LOCALIZING SPRING SECURITY MESSAGES
尝试将这些行添加到ApplicationContext.xml文件中。 spring安全豆的其余部分在哪里。

    

您应该找到存储 spring默认类。 让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= 

在您的messages.properties(或您命名的任何内容)中,添加如下行:

 AbstractUserDetailsAuthenticationProvider.badCredentials=The credentials you supplied are invalid. 

您不需要CustomAuthenticationException。

在类路径中创建属性文件,如loginMessage.properties

在该属性文件中,指定

AbstractUserDetailsAuthenticationProvider.badCredentials =输入的用户名/密码不正确。

在applicationContext.xml中添加以下bean,

    loginMessage    

之后,您将收到输入的用户名/密码错误的消息。 而不是坏证书