【发布时间】: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