30

我想在 jsp 中为 spring 安全身份验证异常显示自定义错误消息。

对于错误的用户名或密码,

spring displays : Bad credentials
what I need     : Username/Password entered is incorrect.

对于用户被禁用,

spring displays : User is disabled
what I need     : Your account is diabled, please contact administrator.

我是否需要为此重写 AuthenticationProcessingFilter ?否则我可以在 jsp 本身中做一些事情来查找身份验证异常密钥并显示不同的消息

4

4 回答 4

31

在 spring security jar中重新定义messages.properties中的属性。例如添加到类路径myMessages.properties并将消息源添加到上下文:

AbstractUserDetailsAuthenticationProvider.badCredentials=Username/Password entered is incorrect.
AbstractUserDetailsAuthenticationProvider.disabled=Your account is diabled, please contact administrator.

在萨尔文弗朗西斯:

  1. 将 myMessages.properties 添加到 WEB-INF/classes 内的 WAR 文件中。
  2. 将此 bean 添加到 spring 上下文配置文件中

消息源 Bean

<bean id="messageSource"   
    class="org.springframework.context.support.ResourceBundleMessageSource">  
    <property name="basenames">  
        <list>
            <value>myMessages</value>
        </list>
    </property>
</bean>
于 2009-09-03T16:11:25.227 回答
3

添加“messageSource”bean 后,我在使用 CookieLocaleResolver 获取错误消息时遇到了问题,因为在 Security 之后调用了 DispatcherServlet(它会自动将其用于您的应用程序)。请参阅:http ://static.springsource.org/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#localization

我的解决方案是设置 LocalContextHolder 的自定义过滤器:

public class LocaleContextFilter extends OncePerRequestFilter {
    private LocaleResolver localeResolver;
    public void setLocaleResolver(LocaleResolver localeResolver) {
        this.localeResolver = localeResolver;
    }
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
            FilterChain filterChain) throws ServletException, IOException {
        // store Local into ThreadLocale
        if (this.localeResolver != null) {
            final Locale locale = this.localeResolver.resolveLocale(request);
            LocaleContextHolder.setLocale(locale);
        }
        try {
            filterChain.doFilter(request, response);
        } finally {
            LocaleContextHolder.resetLocaleContext();
        }
    }
}

以及 Spring Security Context 配置:

  <http use-expressions="true">
    <custom-filter ref="localeContextFilter" after="FIRST" />
    .....
  </http>
  <beans:bean id="localeContextFilter" class="at.telekom.ppp.util.opce.fe.interceptor.LocaleContextFilter" >
    <beans:property name="localeResolver" ref="localeResolver" /><!-- e.g.: CookieLocaleResolver -->
  </beans:bean>

我希望这可以帮助其他有这个问题的人。

于 2012-05-09T19:48:48.580 回答
2

这是针对此问题的 JSP EL 修复程序。与其说是一种优雅的解决方案,不如说是一种 hack,但可以快速而肮脏地完成工作。警告 - 这不是 i18n 安全的!只能说英语。

这需要函数标签库:

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

和替换代码:

${fn:replace(SPRING_SECURITY_LAST_EXCEPTION.message, 'Bad credentials', 'Username/Password are incorrect')}
于 2010-02-04T11:19:17.007 回答
0

我是 spring 新手,但在服务器上试试这个:

throw new BadCredentialsException("This is my custom message !!");

当然,您需要一个作为身份验证提供程序的类才能工作。

于 2009-11-09T12:51:51.153 回答