簡體   English   中英

HTTP狀態405 - 使用Spring Security的Spring MVC中不支持請求方法'POST'

[英]HTTP Status 405 - Request method 'POST' not supported in Spring MVC with Spring Security

我使用freemarker模板作為視圖部分創建了一個spring mvc應用程序。 在這嘗試使用forms添加模型。我也使用spring security這里是代碼

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>

彈簧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和controller上都給了POST方法。 那么為什么會這樣呢?

我不確定這是否有幫助,但我有同樣的問題。

您正在使用帶有CSRF保護的springSecurityFilterChain。 這意味着您必須在通過POST請求發送表單時發送令牌。 嘗試將下一個輸入添加到表單:

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

據我所知,上述解決方案不適用於最新的SpringSecurity。 您可以通過以下操作URL發送它,而不是通過隱藏傳遞:

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

我找到了解決方案。 這是因為春季安全跨站請求偽造(CSRF)保護。 它阻止了網址。 所以我在表單中添加了一個額外的字段。

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

現在它正常運作。

嘗試替換:

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"

這對我有用:

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

另一種解決方案(但每種形式)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM