Spring POST方法不起作用

时间:2016-08-04 12:25:42

标签: spring http post spring-security

我是Spring Security的初学者,当我尝试发布数据时,会显示以下错误:

HTTP Status 405 - Request method 'POST' not supported

我的控制器方法是:

@RequestMapping(value="/save", method=RequestMethod.POST)
public String create(@ModelAttribute(value="employee") Employee employee,ModelMap modelMap,@PathVariable String save)
{



    //Validation code start
    boolean error = false;

    System.out.println(employee); //Verifying if information is same as input by user


     System.out.println("get");
    //validation code ends

    //Store the employee information in database
    //manager.createNewRecord(employee);

    //Mark Session Complete
    return "redirect:user";
}

我的Spring安全配置是:

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

    <!-- enable use-expressions -->
    <http auto-config="true" use-expressions="true">
        <intercept-url pattern="/admin**" access="hasRole('ROLE_ADMIN')" />
        <intercept-url pattern="/admin/**" access="isAuthenticated()" />
        <intercept-url pattern="/user**" access="hasRole('ROLE_USER')" />
        <intercept-url pattern="/user/**" access="isAuthenticated()" />

        <!-- access denied page -->
        <access-denied-handler error-page="/403" />

        <form-login 
            login-page="/login" 
            default-target-url="/welcome" 
            authentication-failure-url="/login?error" 
            username-parameter="username"
            password-parameter="password"/>

        <session-management>
            <concurrency-control max-sessions="1"  error-if-maximum-exceeded="false" /> 
        </session-management> 

        <logout logout-success-url="/user"  />
        <!-- enable csrf protection -->
        <csrf/>


    </http>

    <!-- Select users and user_roles from database -->
    <authentication-manager>
        <authentication-provider>
            <jdbc-user-service data-source-ref="dataSource"
                users-by-username-query=
                    "select username,password, enabled from users where username=?"
                authorities-by-username-query=
                    "select username, role from user_roles where username =?  " />
        </authentication-provider>
    </authentication-manager>

</beans:beans>

我的jsp页面是:

<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

<html>
<head>
    <title>Add Employee Form</title> 
    <style>
    .error
    {
        color: #ff0000;
        font-weight: bold;
    }
    </style>
</head>

<body>
    <h2><spring:message code="lbl.page" text="Add New Employee" /></h2>
    <br/>
        <form:form  action="save?${_csrf.parameterName}=${_csrf.token}" method='POST' modelAttribute="employee">

            <table>
                <tr>
                    <td>name:</td>
                    <td><input type='text' name='username'></td>
                </tr>
                <tr>
                    <td>address:</td>
                    <td><input type='password' name='password' /></td>
                </tr>
                <tr>
                    <td colspan='2'><input name="submit" type="submit"
                        value="submit" /></td>
                </tr>
            </table>

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

        </form:form>
</body>
</html>

Web.xml Spring安全性内容是:

<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

请提出宝贵的建议......

0 个答案:

没有答案