java - 登录表单 : @RequestMapping cannot handle the POST request

标签 java spring spring-mvc spring-security

我是 Spring 新手。

我正在尝试使用放入 UserForm 类中的注释来验证表单。

不知何故,post 方法根本不运行。

我可以在浏览器中看到 POST 请求,但 Controller POST 方法不会运行。

这是我的LoginController类:

@Controller
public class LoginController {

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(Map<String, Object> model) {
        UserForm loginUser = new UserForm();
        model.put("userForm", loginUser);

        return "Login";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String doLogin(
            @Valid @ModelAttribute("userForm") UserForm userForm,
            BindingResult result, 
            Map<String, Object> model) {

        if (result.hasErrors()) {
            return "Login";
        }

        return "Home";
    }
}

任何帮助指示表示赞赏。

编辑: 这是我的表格:

<form:form action="/bookmarks/login" method="POST" commandName="userForm">
    <label for="idInput">Username:</label>
    <form:input type="text" path="username" placeholder="Username" />
    <div><form:errors path="username" cssClass="error"/></div>
    <br>
    <label for="idInput">Passwors:</label>
    <form:input type="password" path="password" placeholder="Password" />
    <div><form:errors path="password" cssClass="error"/></div>
    <br>
    <button type="submit" value="login">Submit</button>
</form:form>

这是我的 security.xml,添加了 login-processing-url="/j_spring_security_check"。这实际上解决了我的问题,但我无法完全解释原因:

<beans xmlns="http://www.springframework.org/schema/beans"
    ...
    <security:http auto-config="true" use-expressions="true">
        <security:form-login 
            login-page="/login" 
            default-target-url="/" 
            login-processing-url="/j_spring_security_check"/>
            <!-- authentication-failure-url="/login.html?error=true" /> --> <!-- /login.html?error=true -->
        <!-- default-target-url="/homepage.html" -->
        <security:csrf disabled="true"/>
        <security:intercept-url 
            pattern="/login" access="permitAll"/>
        <!-- <security:intercept-url pattern="/studentRegister" access="permitAll"/> -->
        <security:intercept-url 
            pattern="/**" access="hasRole('USER')"/>
        <security:logout 
            logout-success-url="/login?logout=true"
            invalidate-session="true"
            delete-cookies="JSESSIONID"/>
        <security:session-management
            invalid-session-url="/login"></security:session-management>
    </security:http>
    <bean id="userDetailsService" class="bg.jwd.bookmarks.security.UserDetailsServiceImpl" />
    <security:authentication-manager>
        <security:authentication-provider
            user-service-ref="userDetailsService">
            <security:password-encoder hash="md5" />
        </security:authentication-provider>
    </security:authentication-manager>
    <security:global-method-security secured-annotations="enabled" />
</beans>

最佳答案

我找到了解决问题的方法。我刚刚将 login-processing-url="/j_spring_security_check" 添加到 security.xml 中的 form-login 标记中。现在可以接受 POST 请求了。

编辑:

你好, 我对这段代码还有另一个问题。 现在我通过 POST 请求并已经进行了验证,但是当我尝试使用正确的用户名和密码登录时,Spring 将我重定向回登录表单。 (Spring 4.0.3.RELEASE)。

关于java - 登录表单 : @RequestMapping cannot handle the POST request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36159972/

相关文章:

java - 使用 JGit 从 Git 检索提交消息日志

java - 处理 REST 请求以在单独的线程中通过 Hibernate 检索数据

Spring @Transactional 和 JDBC 自动提交

java - 如何扩展/修改斯坦福 NLP 中的字典?

java - "Cannot find symbol"制作图形时

java - 使用displayTag导出到pdf,excel,xml, Spring 不工作

java - file.html 在本地主机上正确显示,但在服务器上不正确显示

java - Spring - 在哪里可以找到发行版 jar 文件?

Java 应用程序部署/安装程序选项?

java - Spring 安全: Who passes the AuthenticationManagerBuilder to the WebSecurityConfigurerAdapter configure method?