首页 > 解决方案 > Spring Security Failurehandler 中的自定义登录表单 Bean

问题描述

版本:Spring Security 4.2.3

我需要一些附加值,例如登录 Jsp 页面中的“表单操作”。是否可以在自定义 SuccessHandler 或 FailureHandler 中获取表单 bean“logonForm”。我尝试了 Autowired,但这没有帮助。有什么建议么?

自定义登录 JSP

 <form:form action="userLogon" commandName="logonForm" >  
 <input type="hidden" name="action" id="action"/>

故障处理程序

public class MyAppAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler  {
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
@Autowired(required=false)
@Qualifier("logonForm")
private LogonForm logonForm;

@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authException) throws IOException, ServletException {
    setUseForward(true); // ensure it is a server forward than client redirect

    if (authException != null) {
        String userId = logonForm.getUserId();

标签: springspring-security

解决方案


结果更简单,登录字段值可从 API 获得:

String userId = request.getParameter("userId");
String action= request.getParameter("action");

推荐阅读