30

春のセキュリティ認証例外のjspでカスタムエラーメッセージを表示したい。

ユーザー名またはパスワードが間違っている場合は、

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. WEB-INF/classes 内の WAR ファイルに myMessages.properties を追加します。
  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 で動作させるのに問題がありました。これは、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 修正です。洗練されたソリューションというよりはハックですが、仕事はすばやく汚れています。警告 - これは 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

春は初めてですが、サーバーでこれを試してください:

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

もちろん、これを機能させるには、認証プロバイダーであるクラスが必要です。

于 2009-11-09T12:51:51.153 に答える