【发布时间】:2013-05-21 11:44:14
【问题描述】:
您好,我需要在 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 下看到了我的异常,但错误消息始终是来自错误凭据的消息,我该如何更改?
谢谢
【问题讨论】:
-
您需要在登录页面或日志文件中显示不同的消息吗?如果是登录页面需要配置:
<form-login login-page="/login.jsp" authentication-failure-url="/login.jsp?login_error=1"/> -
在登录页面中,他们正在为每个 AuthenticationException(BadCredentialsException,LockedException... 参见 org.springframework.security.authentication)从异常到自定义消息的某个位置映射,我想显示我的自己的错误信息。
标签: java spring jakarta-ee spring-security