how to display custom error message in jsp for spring security auth exception

后端 未结 4 2119
天涯浪人
天涯浪人 2020-11-30 00:14

I want to display custom error message in jsp for spring security authentication exceptions.

For wrong username or password,

spring displays : Bad cr         


        
4条回答
  •  被撕碎了的回忆
    2020-11-30 00:27

    After adding the "messageSource" bean, I had problems to get the Error Message work with the CookieLocaleResolver because the DispatcherServlet (which does use this for your application automatically) is invoked after the Security. See: http://static.springsource.org/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#localization

    My Solution was a custom Filter which sets the 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();
            }
        }
    }
    

    And the Spring Security Context configuration:

      
        
        .....
      
      
        
      
    

    I hope this helps others which has this problem.

提交回复
热议问题