HTTP Status 405 - Request method 'POST' not supported in Spring MVC with 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 security Here is the code

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>

When click submit button it returns error `

HTTP Status 405 - Request method 'POST' not supported

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

Answers


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

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}"/>

As far as I saw, the mentioned solutions didn't work for latest SpringSecurity. Instead of passing through with hidden you can also send it through the action URL like below:

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

I found the solution. It is because of spring security Cross Site Request Forgery (CSRF) protection. It blocks the url. So i added an extra field inside the form.

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

Now it is working properly.


Try to replace:

action="addEmployee"

with:

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

Unless you are using Spring 3.2

EDIT after seeing XML:

Try to move servlet-context.xml to your WEB-INF directory and rename it 'appServlet-context.xml'. Then remove the line:

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

From the contextConfigLocation in your web.xml.

The convention is that the context xml file is named '[servlet-name]-context.xml' where [servlet-name] is the name of the DispatcherServlet.

Also try to add a '/' to your form action, so:

action="/addEmployee"

This works for me:

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

another solution (but every form)

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

Need Your Help

Angular ng-repeat with ng-form, accessing validation in controller

angularjs validation angularjs-ng-repeat angularjs-ng-form

I am trying to generate an editable list using ng-repeat. I want to remind the user to update any edits before moving on, so I am using ng-form to create "nested" forms on the fly because the

Bulk update attribute on multiple models in ActiveRecord?

ruby-on-rails activerecord

I have a simple has_many association, and I want to change an attribute from public to private the associated object. What's the best way to do this: