Spring Security:如果身份validation失败,则重定向到登录页面

我们有两种登录方式。

  • 用户名和密码由请求标头中的另一个应用程序发送。 检查IT,如果用户名和密码正确,则进入。[为此编写自定义filter]
  • 如果请求标头中不存在用户名和密码,则会显示登录屏幕。
  • 当用户名和密码出现在请求标头中且错误时,我会看到HTTP状态401 – 身份validation失败:错误的凭据页面。

    如果身份validation失败,如何让它显示登录页面。

    下面是security.xml中的代码

           

    如果您需要更多信息,请与我们联系。

    编辑:在我的应用程序中添加RequestHeaderfilter的代码

     public class RequestHeaderProcessingFilter extends AbstractAuthenticationProcessingFilter{ private String usernameHeader = "j_username"; private String passwordHeader = "j_password"; protected RequestHeaderProcessingFilter() { super("/login_direct"); } //getters and setters @Override public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException { String username = request.getHeader(usernameHeader); String password = request.getHeader(passwordHeader); SignedUsernamePasswordAuthenticationToken authRequest = new SignedUsernamePasswordAuthenticationToken(username, password); return this.getAuthenticationManager().authenticate(authRequest); } 

    }

    要在身份validation失败的情况下显示登录页面,您应该在具有相同的URL

    例如:

           

    在该示例中,用户在注销后也被重定向到登录页面。 / procesarLogin是一个发送用户lo login.jsp页面的方法。

    身份validation失败的默认行为是显示HTTP状态401.如果使用SimpleUrlAuthenticationFailureHandler来处理故障(由AbstractAuthenticationProcessingFilter完成,则可以通过将defaultFailureUrl设置为/login.jsp来覆盖此行为。

    或者,可以使用适当的操作定义自定义error handling程序。