【问题标题】:An explanation about overriding SimpleUrlAuthenticationFailureHandler关于覆盖 SimpleUrlAuthenticationFailureHandler 的解释
【发布时间】:2014-12-17 15:18:14
【问题描述】:

我在业余时间学习java和Spring有一段时间了,所以我既没有掌握java也没有掌握Spring。

对于我创建的用于研究 java 和 Spring 的 Web 项目,我必须扩展 SimpleUrlAuthenticationFailureHandlerm

不清楚的是为什么在扩展SimpleUrlAuthenticationFailureHandler 并覆盖onAuthenticationFailure 之后,在我自己的onAuthenticationFailure()... 方法中我不得不调用super.onAuthenticationFailure(...) 方法。

可能我没有得到java的主要规则之一。

这是我正在谈论的课程

public class CustomAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler { 

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {    

       if(exception.getClass().isAssignableFrom(BadCredentialsException.class)) {
              setDefaultFailureUrl("/url1");
        }

       else if (exception.getClass().isAssignableFrom(DisabledException.class)) {        
            setDefaultFailureUrl("/url2");
       }

       else if (exception.getClass().isAssignableFrom(SessionAuthenticationException.class)) {      
            setDefaultFailureUrl("/url3");  
      }

      super.onAuthenticationFailure(request, response, exception);  //why this???
    }    
}

【问题讨论】:

  • 在调用 super.onAuthenticationFailure(request, response, exception); 之前你到底做了什么?
  • 我编辑了我的帖子。无论如何,我检查异常类型并将用户重定向到特定页面。
  • 这里你只设置了默认的url。因此,您需要编写发生故障时重定向的逻辑。该逻辑是在 SimpleUrlAuthenticationFailureHandler 类中编写的。所以需要调用 super.onAuthenticationFailure 方法。
  • 希望你对我的回答很清楚。

标签: java spring jsp jakarta-ee spring-security


【解决方案1】:

这里你试图重写 SimpleUrlAuthenticationFailureHandler 的方法。

这个方法在 SimpleUrlAuthenticationFailureHandler 中定义:

public void onAuthenticationFailure(HttpServletRequest request,
            HttpServletResponse response, AuthenticationException exception)
            throws IOException, ServletException {
        if (this.defaultFailureUrl == null) {
            this.logger.debug("No failure URL set, sending 401 Unauthorized error");
            response.sendError(401,"Authentication Failed: " + exception.getMessage());
        } else {
            saveException(request, exception);

            if (this.forwardToDestination) {
                this.logger.debug("Forwarding to " + this.defaultFailureUrl);
                request.getRequestDispatcher(this.defaultFailureUrl).forward(request, response);
            } else {
                this.logger.debug("Redirecting to " + this.defaultFailureUrl);
                this.redirectStrategy.sendRedirect(request, response,
                        this.defaultFailureUrl);
            }
        }
    }

在您的 CustomAuthenticationFailureHandler 中,您将覆盖 SimpleUrlAuthenticationFailureHandler 的方法:

基本上在这里您只是设置默认网址。你需要编写重定向策略。但它是在超类 SimpleUrlAuthenticationFailureHandler 中定义的。所以需要调用 super.onAuthenticationFailure(request, response, exception);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多