本文介绍了HTTP状态405-具有Spring Security的Spring MVC不支持请求方法'POST'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用freemarker模板作为视图部分创建了spring mvc应用程序.在这个尝试使用表单添加模型的过程中,我也在使用Spring Security这是代码

I created a spring mvc application using freemarker template as view part. In this tried to add a model using forms.I am also using spring securityHere is the code

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

点击提交按钮时,它会返回错误`

When click submit button it returns error `

`我在ftl和controller上都给出了POST方法.那为什么会这样呢?

`I gave POST method on both ftl and controller. Then why would this happen?

推荐答案

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

I am not sure if this helps but I had the same problem.

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

You are using springSecurityFilterChain with CSRF protection. That means you have to send a token when you send a form via POST request. Try to add the next input to your form:

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

这篇关于HTTP状态405-具有Spring Security的Spring MVC不支持请求方法'POST'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 19:04