【问题标题】:how to display custom error message in jsp for spring security auth exception如何在jsp中为spring security auth异常显示自定义错误消息
【发布时间】:2010-11-25 07:06:24
【问题描述】:

我想在jsp中为spring security认证异常显示自定义错误信息。

用户名或密码错误,

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 本身中做一些事情来查找身份验证异常密钥并显示不同的消息

【问题讨论】:

    标签: java spring spring-mvc spring-security


    【解决方案1】:

    重新定义 messages.properties 在 spring security jar 中的属性。例如添加到类路径 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>
    

    【讨论】:

    • 嗨,您能否更具体地说明如何“绑定”spring 以接受 myMessages.properties 与它自己的 message.properties 相比?
    • 你有萨尔文弗朗西斯
    • 请注意,如果您想要多个消息文件,则顺序很重要。您可以引用多个消息源,但 Spring 使用它找到的定义的第一次出现。您还可以使用以下符号引用类路径之外的文件:&lt;list&gt; &lt;value&gt;file:${uk.co.foo.project.path}/config/customermessages&lt;/value&gt; &lt;value&gt;/WEB-INF/messages&lt;/value&gt; &lt;value&gt;classpath:/config/genericMessages&lt;/value&gt; &lt;/list&gt;
    • 在你的类路径中有myMessages.properties 意味着你可以把它放在 src/main/resources 下。查看更多提示mkyong.com/spring/…
    • 如何在不同的语言环境中显示 sprin 安全的异常消息?
    【解决方案2】:

    添加“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>
    

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

    【讨论】:

      【解决方案3】:

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

      这需要functions标签库:

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

      以及替换代码:

      ${fn:replace(SPRING_SECURITY_LAST_EXCEPTION.message, 'Bad credentials', 'Username/Password are incorrect')}
      

      【讨论】:

      • 这不起作用。我将 taglib 放在 jsp 的顶部,并将 ${SPRING_SECURITY_LAST_EXCEPTION.message} 替换为 ${fn:replace(SPRING_SECURITY_LAST_EXCEPTION.message, 'Bad credentials', 'Username/Password are wrong')}
      【解决方案4】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2021-06-06
        • 2020-05-26
        • 1970-01-01
        • 1970-01-01
        • 2016-04-17
        • 2013-06-30
        • 1970-01-01
        • 2017-05-15
        相关资源
        最近更新 更多