gpt4 book ai didi

java - Spring Security 自定义 AuthenticationException 消息

转载 作者:搜寻专家 更新时间:2023-10-31 08:12:15 27 4
gpt4 key购买 nike

您好,我需要在 Spring 安全登录表单中添加一个新的异常,除了我想要自己的错误消息(直到现在它显示“错误的登录名/密码”)之外,一切正常。

我已经覆盖了用户名密码身份验证过滤器的默认尝试身份验证方法:

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

我的异常(exception):

public class CustomAuthenticationException extends AuthenticationException {

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

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

}

在我的 Controller 中,我在 SPRING_SECURITY_LAST_EXCEPTION 下看到我的异常,但错误消息总是来自错误的凭据,我该如何更改它?

谢谢

最佳答案

您应该尝试LOCALIZING SPRING SECURITY MESSAGES
尝试将这些行添加到您的 ApplicationContext.xml 中文件。其余的 Spring Security Bean 所在的位置。

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="yourFolder/myMessages"/>
</bean>

你应该找到你的 spring 默认类 <KEY, MESSAGE>被存储。有你的myMessage具有相同 KEY 的文件s 和本地化 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=

关于java - Spring Security 自定义 AuthenticationException 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16759264/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com