복붙노트

[SPRING] 봄 보안 인증 예외에 대한 JSP로 사용자 정의 오류 메시지를 표시하는 방법

SPRING

봄 보안 인증 예외에 대한 JSP로 사용자 정의 오류 메시지를 표시하는 방법

스프링 보안 인증 예외를 위해 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 자체에서 인증 예외 키를 찾고 다른 메시지를 표시 할 수 있습니까?

해결법

  1. ==============================

    1.스프링 보안 jar 내의 messages.properties에서 특성을 다시 정의하십시오. 예를 들어, classpath myMessages.properties에 추가하고 컨텍스트에 메시지 소스를 추가하십시오.

    스프링 보안 jar 내의 messages.properties에서 특성을 다시 정의하십시오. 예를 들어, classpath myMessages.properties에 추가하고 컨텍스트에 메시지 소스를 추가하십시오.

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

    살빈 프란시스 :

    메시지 소스 Bean

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

    2."messageSource"빈을 추가 한 후 보안 후 DispatcherServlet (응용 프로그램에 자동으로 사용함)이 호출되기 때문에 CookieLocaleResolver에서 오류 메시지가 작동하는 데 문제가있었습니다. 참조 : http://static.springsource.org/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#localization

    "messageSource"빈을 추가 한 후 보안 후 DispatcherServlet (응용 프로그램에 자동으로 사용함)이 호출되기 때문에 CookieLocaleResolver에서 오류 메시지가 작동하는 데 문제가있었습니다. 참조 : 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. ==============================

    3.여기에 JSP로 EL 픽스가있다. 우아한 솔루션보다 해킹이 많지만 작업이 빠르고 빠르다. 주의하십시오 - 이것은 i18n 안전하지 않습니다! 영어 만.

    여기에 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')}
    
  4. ==============================

    4.나는 봄이 처음이지만 서버에서 이것을 시도한다.

    나는 봄이 처음이지만 서버에서 이것을 시도한다.

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

    물론이 작업을 수행하는 인증 공급자 인 클래스가 필요합니다.

  5. from https://stackoverflow.com/questions/1373407/how-to-display-custom-error-message-in-jsp-for-spring-security-auth-exception by cc-by-sa and MIT license