HTTP 状态 405 - 在具有 Spring 安全性的 Spring MVC 中不支持请求方法“POST”

2022-09-03 17:51:47

我使用自由标记模板作为视图部分创建了一个弹簧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>

员工主计长.java

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

网.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>

弹簧安全.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 - 不支持请求方法“开机自检”

' 我在 ftl 和控制器上都给出了 POST 方法。那么为什么会发生这种情况呢?


答案 1

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

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

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

答案 2

据我所知,提到的解决方案不适用于最新的SpringSecurity。除了通过隐藏进行传递之外,您还可以通过操作URL发送它,如下所示:

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

推荐