Java 如何在jsp中为spring security auth异常显示自定义错误消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1373407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:17:58  来源:igfitidea点击:

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

javaspringspring-mvcspring-security

提问by Maniganda Prakash

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

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

For wrong username or password,

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

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

For user is disabled,

对于用户被禁用,

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

Do I need to override AuthenticationProcessingFilter just for this ? or else can I do something in jsp itself to find the authentication exception key and display different message

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

采纳答案by rodrigoap

Redefine the properties in messages.propertiesinside spring security jar. For example add to the classpath myMessages.propertiesand add a message source to the context:

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

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

At Salvin Francis:

在萨尔文·弗朗西斯:

  1. Add myMessages.properties to the WAR file inside WEB-INF/classes.
  2. Add this bean to spring context config file
  1. 将 myMessages.properties 添加到 WEB-INF/classes 中的 WAR 文件。
  2. 将此 bean 添加到 spring 上下文配置文件

Message Source Bean

消息源Bean

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

回答by Salvin Francis

I am new to spring, but try this at the server:

我是春天的新手,但在服务器上试试这个:

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

Of course you need a class that is an authentication provider for this to work.

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

回答by Danny C

Here is a JSP EL fix for this. More of a hack than an elegant solution, but gets the job done quick and dirty. Caveat- this is not i18n safe! Only English.

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

This requires the functions tag library:

这需要函数标签库:

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

And the replace code:

和替换代码:

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

回答by brabenetz

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

添加“messageSource”bean 后,我在使用 CookieLocaleResolver 获取错误消息时遇到问题,因为在 Security. 请参阅: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:

我的解决方案是一个自定义过滤器,它设置 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:

以及 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>

I hope this helps others which has this problem.

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