在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
}
}
}
您正在呼叫super.onAuthenticationFailure
,它将执行到已配置URL的重定向。因此,响应已经提交,您不能决定重定向其他地方。
您可以配置SimpleUrlAuthenticationFailureHandler
为重定向到一个URL,并且仅在您不想自己进行重定向的情况下才调用super方法。
或者,AuthenticationFailureHandler
直接实现并在故障方法中实现所需的所有逻辑-一旦事情超出了一定的复杂度,我宁愿完全避免继承:
if (oneCondition) {
// redirect to IdP
} else {
// redirect to registration page
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句