【问题标题】:j_spring_security_check 404 issuej_spring_security_check 404 问题
【发布时间】:2013-07-11 00:03:15
【问题描述】:

我正在尝试让我的 spring + hibernate + spring-security 和 tiles2 - “HelloWorld”应用程序工作,遵循this guide(不幸的是它是德语)。

我的问题是登录应用程序时收到“404”错误消息。重定向到登录页面按预期工作,但当我点击登录按钮时无法访问“http://localhost:8080/App/j_spring_security_check”。

我的 web.xml 看起来是这样的:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/defs/applicationContext.xml
        /WEB-INF/defs/applicationContext-security.xml
    </param-value>
</context-param>

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
  <filter-name>springSecurityFilterChain</filter-name> 
  <url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
    <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>

applicationContext-security.xml 文件看起来是这样的......

<http use-expressions="true">
    <intercept-url pattern="/index.html" access="permitAll" />
    <intercept-url pattern="/timeout.html" access="permitAll" />
    <intercept-url pattern="/redirect.html" access="permitAll" />
    <intercept-url pattern="/media/**" access="permitAll" />
    <intercept-url pattern="/includes/**" access="permitAll" />
    <intercept-url pattern="/office/**" access="hasRole('ROLE_USER')" />
    <intercept-url pattern="/office/admin/**" access="hasRole('ROLE_ADMIN')" />

    <form-login login-page="/index.html" 
            authentication-failure-url="/index.html?login_error=1"
            default-target-url='/office/kunden.html'
            always-use-default-target='true'
            />
    <logout logout-success-url="/index.html" />
    <remember-me />
    <session-management invalid-session-url="/index.html">
        <concurrency-control max-sessions="2" error-if-maximum-exceeded="true" />
    </session-management>
</http>

<authentication-manager>
    <authentication-provider>
        <jdbc-user-service data-source-ref="mysqldataSource" 
                    authorities-by-username-query="select username, authority from benutzer where username = ?"
                    users-by-username-query="select username, password, enabled from benutzer where username = ?"/>
    </authentication-provider>
</authentication-manager>

数据库连接似乎没问题。

如果有人能给我一个提示,我会很高兴,因为我已经做了很多谷歌搜索,但还没有找到解决方案。

我使用 spring 3.1 和 tomcat 7.0.23

【问题讨论】:

  • 您的意思是“j_security_check”还是“j_spring_security_check”?你两个都在这里...
  • 对不起,这是 j_spring_security_check
  • 检查该请求的调试日志输出,并查看 Spring Security 关于处理它的说明。您应该会看到它通过过滤器链的详细报告。

标签: java spring spring-security


【解决方案1】:

我会检查两件事:

  1. 请求调度
  2. 弹簧安全配置

要检查请求分派,首先确保您的应用程序可在 servlet 容器中访问。意思是,你提到了http://localhost:8080/App/j_spring_security_check。您的应用程序是否可以通过该 URL 访问? http://localhost:8080/App 是否显示正确的内容(HTTP 200)? 另外确保调度程序 servlet 配置正确。在您提供的教程中,有以下部分:

  <!--  Spring Hauptteil -->

  <servlet>
      <servlet-name>spring</servlet-name>
      <servlet-class>
          org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>spring</servlet-name>
      <url-pattern>*.html</url-pattern>
  </servlet-mapping>

如果您没有在 web.xml 中提供它,那么在通过 spring-security 最终检查之前,您的请求甚至可能无法正确分派。

如果这对你没有帮助,试试这个。

按照documentation,最低配置应该足以检查您的设置是否正确。如果您按照教程进行操作,您可能会犯一些小错误(例如 typeo),这将导致 spring-security 无法正常启动。然后很容易在记录器输出中跳过一些错误信息。 我建议您执行以下操作。

  1. 更改您的 applicationContext-security.xml 以支持文档中提供的最低配置。
  2. 启动应用程序并转到http://localhost:8080/App/j_spring_security_check

如果你得到正确的响应 - 尝试修改配置直到你完成。

学习要点 DelegatingFilterProxy(在 web.xml 中定义)真正做的是将请求委托给 Spring 的 IoC 管理的其他过滤器。这个过滤器是通过安全命名空间在 applicationContext-security 中定义的。如果由于某种原因这不起作用,则过滤器将不会被初始化,并且您最终可能会看到 http 404,而不管应用程序的其余部分是否正常启动。

呃,很多文字;)

【讨论】:

    【解决方案2】:

    您的配置看起来不错。可能出现 404 的一件事是 default-target-url='/office/kunden.html' 指向不存在的控制器或视图。

    检查 url /office/kunden.html 是否有效 - 因此停用安全功能(只需添加 &lt;security:intercept-url pattern="/**" access="permitAll" /&gt;)并尝试。

    另一个可能出错的地方是本教程适用于 spring 3.0 而不是 spring 3.0。我不认为这是原因,但请尝试降级。

    【讨论】:

    • 这是我先做的,关闭安全限制。同样的问题。也许你是对的,降级会有所帮助。
    • 您能否准确说明导致 404 的请求是什么?您应该能够从日志中解决这个问题,并通过监视浏览器发送的请求(例如使用 Firebug)。
    • @xSNRG:如果在启用 spring security 的情况下出现同样的问题,那么问题与kunden 控制器或视图有关。 -- 您是否将教程实施到最后,因为看起来kunden 控制器是在安全性内容之后实施的?
    【解决方案3】:

    对于那些面临相同症状但情况不同的人,即那些在负载均衡器背后执行 SSL 卸载的人,以下答案可能会让您朝着正确的方向前进。我遇到了类似的问题,结果证明传入的请求处理正确,但是作为响应,spring security 将重定向发送到由default-target-url 属性定义的绝对 URL(以 http 而不是 https 开头)

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

    现在客户端浏览器尝试在 http 上打开重定向的位置,在负载均衡器(仅接受 https 流量)上失败并报告 404 NOT FOUND

    我们通过为负载均衡器中端口 443 (https) 上的所有传入请求添加以下 mod_header 指令来解决此问题:

    RequestHeader set X-Forwarded-Proto "https"

    将添加一个额外的标题。如果您运行像 Jetty 这样的应用程序服务器,它将识别此标头并翻译传入的请求。 (见http://www.gossamer-threads.com/lists/apache/users/407272

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-20
      • 2015-08-13
      • 2015-02-12
      • 1970-01-01
      • 2021-09-06
      • 2015-10-14
      • 2013-10-14
      相关资源
      最近更新 更多