vde*_*ris 5 spring spring-security
在 Spring 中将重定向设置为自定义 AuthenticationFailureHandler 的正确方法是什么?
是否可以调用控制器?
代码如下:
@Component
public class MyAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
@Override
public void onAuthenticationFailure(HttpServletRequest request,
HttpServletResponse response, AuthenticationException exception)
throws IOException, ServletException {
super.onAuthenticationFailure(request, response, exception);
if (exception.getClass().isAssignableFrom(
CustomUsernameNotFoundException.class)) {
// TODO Set the redirect
}
}
}
Run Code Online (Sandbox Code Playgroud)
尝试这样的事情
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
saveException(request, exception);
//do your things
getRedirectStrategy().sendRedirect(request, response, "/page/login?error=Retry");
}
Run Code Online (Sandbox Code Playgroud)
您正在调用super.onAuthenticationFailure,它将执行到配置的 URL 的重定向。因此,响应已经提交,您无法决定重定向到其他地方。
您可以配置SimpleUrlAuthenticationFailureHandler重定向到一个 URL,并且仅在您不打算自己进行重定向时才调用 super 方法。
或者,AuthenticationFailureHandler直接实现并在失败方法中实现您想要的所有逻辑 - 一旦事情超出一定的复杂程度,我宁愿完全避免继承:
if (oneCondition) {
// redirect to IdP
} else {
// redirect to registration page
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16276 次 |
| 最近记录: |