【问题标题】:HTTP Status 405 - Request method 'POST' not supported in Spring MVC with Spring SecurityHTTP 状态 405 - 使用 Spring Security 的 Spring MVC 不支持请求方法“POST”
【发布时间】:2015-01-31 21:04:21
【问题描述】:

我使用 freemarker 模板作为视图部分创建了一个 spring mvc 应用程序。在此尝试使用表单添加模型。我也在使用弹簧安全性 这是代码

employee.ftl

<fieldset>
    <legend>Add Employee</legend>
  <form name="employee" action="addEmployee" method="post">
    Firstname: <input type="text" name="name" /> <br/>
    Employee Code: <input type="text" name="employeeCode" />   <br/>
    <input type="submit" value="   Save   " />
  </form>

employeeController.java

@RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
    public String addEmployee(@ModelAttribute("employee") Employee employee) {
        employeeService.add(employee);
        return "employee";
    }

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- Spring MVC -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/appServlet/servlet-context.xml,
            /WEB-INF/spring/springsecurity-servlet.xml
        </param-value>
    </context-param>

    <!-- Spring Security -->
    <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>


</web-app>

Spring-security.xml

<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.2.xsd">

    <http security="none" pattern="/resources/**"/>
    <!-- enable use-expressions -->
    <http auto-config="true" use-expressions="true">
        <intercept-url pattern="/login" access="isAnonymous()"/>
        <intercept-url pattern="/**" access="hasRole('ROLE_ADMIN')" />

        <!-- access denied page -->
        <access-denied-handler error-page="/403" />
        <form-login 
            login-page="/login" 
            default-target-url="/"
            authentication-failure-url="/login?error" 
            username-parameter="username"
            password-parameter="password" />
        <logout logout-success-url="/login?logout" />
        <!-- enable csrf protection -->
        <csrf />
    </http>

    <authentication-manager>
        <authentication-provider user-service-ref="userDetailsService" >
            <password-encoder hash="bcrypt" />    
        </authentication-provider>
    </authentication-manager>

</beans:beans>

点击提交按钮返回错误`

HTTP 状态 405 - 不支持请求方法“POST”

` 我在 ftl 和控制器上都给出了 POST 方法。那为什么会这样呢?

【问题讨论】:

  • 您使用的是哪个 Spring 版本?
  • 你能显示你的 web.xml 吗?
  • @AdriaanKoster 我更新了我的问题

标签: java spring spring-mvc spring-security


【解决方案1】:

尝试替换:

action="addEmployee"

与:

action="${pageContext.request.contextPath}/addEmployee"

除非您使用的是 Spring 3.2

看到 XML 后编辑:

尝试将 servlet-context.xml 移动到您的 WEB-INF 目录并将其重命名为“appServlet-context.xml”。然后删除该行:

/WEB-INF/spring/appServlet/servlet-context.xml,

来自 web.xml 中的 contextConfigLocation。

约定是上下文 xml 文件命名为“[servlet-name]-context.xml”,其中 [servlet-name] 是 DispatcherServlet 的名称。

还可以尝试在表单操作中添加“/”,因此:

action="/addEmployee"

【讨论】:

  • 我使用的是spring 3.2.3版
【解决方案2】:

我找到了解决方案。这是因为 spring security 跨站点请求伪造(CSRF)保护。它阻止了网址。所以我在表单中添加了一个额外的字段。

<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>

现在它可以正常工作了。

【讨论】:

  • @AdriaanKoster 我没有注意到他的回答。我在他发布答案之前找到了它。但无论如何,现在我接受了他的回答。
【解决方案3】:

我不确定这是否有帮助,但我遇到了同样的问题。

您正在使用带有 CSRF 保护的 springSecurityFilterChain。这意味着当您通过 POST 请求发送表单时,您必须发送令牌。尝试将下一个输入添加到您的表单中:

<input type="hidden"
name="${_csrf.parameterName}"
value="${_csrf.token}"/>

【讨论】:

  • 修复了部分问题,但您是否调查过为什么缺少 CSRF 令牌会导致“不支持 405 请求方法 'POST'”而不是 403?
【解决方案4】:

据我所见,上述解决方案不适用于最新的 SpringSecurity。除了通过 hidden 传递之外,您还可以通过如下所示的操作 URL 发送它:

<form method="post" action="doUpload?${_csrf.parameterName}=${_csrf.token}" enctype="multipart/form-data">

【讨论】:

    【解决方案5】:

    这对我有用:

    .and().csrf().disable();
    

    另一种解决方案(但每种形式)

    <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
    

    【讨论】:

      猜你喜欢
      • 2015-05-01
      • 2015-09-02
      • 2012-06-24
      • 2015-04-09
      • 2016-03-29
      • 2014-09-24
      • 2020-08-12
      • 2016-04-30
      • 2013-04-09
      相关资源
      最近更新 更多