【问题标题】:spring security 3 authentication-failure-url changespring security 3 身份验证失败 url 更改
【发布时间】:2012-11-21 23:39:15
【问题描述】:

我正在 Spring 上开发 Web 应用程序,其中使用 Spring Security 3 和 LDAP 进行身份验证。

这是我的代码中的表单登录 sn-p:

<security:form-login
    authentication-failure-url="/index.xhtml?error=true"
    default-target-url="/SomeDefaultUrl.xhtml"
    login-page="/index.xhtml" />

当身份验证失败时,我的应用程序被重定向到“/index.xhtml?error=true”网址。问题是我不知道如何捕获“错误”变量并在 index.xhtml 文件中显示一些身份验证失败消息。我没有使用 Spring mvc。

第二个问题是更改 authentication-failure-url 不起作用。

<security:form-login
    authentication-failure-url="/error.xhtml"
    default-target-url="/SomeDefaultUrl.xhtml"
    login-page="/index.xhtml" />

我更改了 authentication-failure-url,但尽管进行了此更改,它仍会重定向到 index.xhtml 文件而没有任何变量。

我该如何解决这个问题?

【问题讨论】:

    标签: spring spring-security


    【解决方案1】:

    如果您的 index.xhtml 是 JSP,您可以这样做(直接来自 Spring 3 食谱书):

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <c:if test="${not empty param.error}">
        Login error.
        Reason ${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}
    </c:if>
    

    【讨论】:

      【解决方案2】:

      重要提示:/index.xhtml?error=true 发送 error 作为 GET 参数。

      如果您的 index.xhtml 是 JSP 文件,您可以使用隐式 request 引用访问该参数:

      <%= request.getParameter("error") %>
      

      如果index.xhtml 是您的Web 控制器方法/servlet 的URL,您需要从Request 对象中获取error 参数。

      public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException 
      {       
          String error = req.getParameter("error");
      }
      

      最后,如果您的index.xhtml 是一个纯html 文件,您可以使用Java Script 来获取该参数: How to retrieve GET parameters from javascript?

      第二个问题:确保正确重建项目。它似乎没有注意到您的更改。

      【讨论】:

      • 感谢您的建议。关于第二个问题:我重建项目并注意到我的更改,因为它重定向到 index.xhtml 文件而不是 index.xhtml?error=true 我认为当 authentication-failure-url 参数的值不是 acceptable 它会自动重定向到 index.xhtml。但我不知道如何解决它
      【解决方案3】:
      <security:form-login
          authentication-failure-url="/error.xhtml"
          default-target-url="/SomeDefaultUrl.xhtml"
          login-page="/index.xhtml" />
      

      “我更改了 authentication-failure-url,但尽管进行了更改,它仍然重定向到 index.xhtml 文件,没有任何变量。”

      答案:请在您的&lt;http auto-config="true" use-expressions="true"&gt;&lt;/http&gt; 正文中定义以下拦截网址。

      <intercept-url pattern="/error.xhtml" access="permitAll"/>
      

      您可能没有定义对 /error.xhtml 的访问权限,这就是为什么 authentication-failure-url 值是不可接受的并且正在进入下一个逻辑 url定义即/index.html

      【讨论】:

        【解决方案4】:

        这里的解决方案都不适合我,然后我发现了这个问题,它对我来说有正确的解决方案: spring security 3.1. custom authentication-failure-url with url parameters

        【讨论】:

          猜你喜欢
          • 2015-07-28
          • 1970-01-01
          • 2022-08-16
          • 2013-11-13
          • 2012-08-15
          • 2015-05-13
          • 2013-12-16
          • 2011-10-20
          • 2016-10-01
          相关资源
          最近更新 更多