Spring Security导致PageNotFound

时间:2015-07-30 10:31:23

标签: javascript spring-mvc spring-security

当我在Spring MVC Web应用程序上集成Spring Security时,我遇到了一个问题。在客户端,我有一些JavaScript代码,它生成一个带有给定URL的发布请求,然后映射到服务器端的特定控制器,以便执行适当的代码。我得到了以下错误:

" org.springframework.web.servlet.PageNotFound - 在名称为' appServlet'""

任何帮助都将受到高度赞赏。

我将以下内容添加到我的web.xml中以激活Spring Security:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      /WEB-INF/spring-security.xml,
      /WEB-INF/spring-database.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>

JavaScript的:

$.ajax({
        url: "/assign/saveAssignment",
        data: {
            "taskID": taskID,
            "userID": userID,
            "isAssigned": !isActive
        },
        type: "POST",
        success: function () {
            console.log("Saved assignment.");
        }

    })

控制器:

@RequestMapping(value = "/assign/saveAssignment", method = RequestMethod.POST)
    public HttpHeaders saveAssignment(@RequestParam String taskID, @RequestParam String userID, @RequestParam String isAssigned) {
...
}

Servlet定义(在web.xml中)

...
<servlet>
  <servlet-name>appServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
  </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>appServlet</servlet-name>
  <url-pattern>/*</url-pattern>
</servlet-mapping>
...

1 个答案:

答案 0 :(得分:0)

我首先明白csrf保护默认是禁用的,所以我所做的就是测试csrf保护是否阻止我的请求是从我的代码中删除<csrf/>

但是,似乎默认行为实际上是csrf-enabled。在尝试了很多事情后,我终于注意到标签csrf确实可以configured。因此<csrf disabled="true"/>有效地禁用了它,并使我的应用程序在Spring安全性方面正常工作。当然,最好的方法是重新启用它并为每个请求添加一个令牌。