错误405:请求方法';邮政';不受支持-Spring安全Java配置

错误405:请求方法';邮政';不受支持-Spring安全Java配置,java,spring,spring-mvc,spring-security,Java,Spring,Spring Mvc,Spring Security,我使用的是SpringMVC(3.2.2.RELEASE)和SpringSecurity(3.2.2.RELEASE) 我尝试使用spring安全性进行基本登录,但每次收到异常“HTTP状态405-请求方法”POST“不受支持”。我已经尝试过寻找类似的问题,但没有找到任何解决方案 以下是我的代码: login.jsp <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="

我使用的是SpringMVC(3.2.2.RELEASE)和SpringSecurity(3.2.2.RELEASE)

我尝试使用spring安全性进行基本登录,但每次收到异常“HTTP状态405-请求方法”POST“不受支持”。我已经尝试过寻找类似的问题,但没有找到任何解决方案

以下是我的代码:

login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
</head>
<body>
    <c:if test="${not empty error}">
                <div>
                    <p style="color: red;">${error}</p>
                </div>
        </c:if>

        <c:if test="${not empty message}">
                <div>
                    <p style="color: red;">${message}</p>
                </div>
        </c:if>

        <c:url var="loginUrl" value="/login" />
        <form action="${loginUrl}" method="post">
            <div>
                <table>
                    <tr>
                        <td><label for="username">Email</label></td>
                        <td><input type="text" id="nombre" name="nombre" placeholder="Enter Name" required></td>
                    </tr>
                    <tr>
                        <td><label for="password">Password</label></td>
                        <td><input type="password" id="password" name="password" placeholder="Enter Password" required></td>
                    </tr>
                </table>
            </div>

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

            <div>
                <input type="submit" value="Log In">
            </div>
        </form>
</body>
</html>
MvcWebApplicationInitializer.java

package com.it2.config.core;

public class MvcWebApplicationInitializer extends
        AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { SecurityConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }
}
package com.bitacora.config.core;

public class SecurityWebApplicationInitializer extends
        AbstractSecurityWebApplicationInitializer {

    public SecurityWebApplicationInitializer() {
        super(SecurityConfig.class);
    }

}
LoginController.java

package com.bitacora.controller;

@Controller
public class LoginController extends HttpServlet {

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ModelAndView loginPage(@RequestParam(value = "error",required = false) String error) {

        ModelAndView model = new ModelAndView();
        if (error != null) {
            model.addObject("error", "Invalid Email OR Password");
        }

        model.setViewName("login");
        return model;
    }
}
bitacora-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    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.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.bitacora" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <mvc:resources mapping="/img/**" location="/img/" />
    <mvc:resources mapping="/css/**" location="/css/" />
    <mvc:annotation-driven />

    <import resource="classpath://Spring.xml"/>

</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_3_0.xsd"
    version="3.0">
    <display-name>BitacoraWEB</display-name>

    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>bitacora</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>bitacora</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

BitacoraWEB
login.jsp
比塔科拉
org.springframework.web.servlet.DispatcherServlet
1.
比塔科拉
/
因为您的服务(控制器)登录操作是get操作,但您的UI(视图)正在发送post请求

  @RequestMapping(value = "/login", method = RequestMethod.GET)
你应该把这个改成

  @RequestMapping(value = "/login", method = RequestMethod.POST)
保持您的UI(视图)原样(使用method=“post”)


您正在将登录信息发布到
/login
,但是Spring Security3.2中的默认登录处理URL是
/j\u Spring\u Security\u check

更改您的表格:

<c:url var="loginUrl" value="/j_spring_security_check" />

您在登录后或之前获得405?您能否对您的问题进行最低限度的配置/设置?这有助于我们发现问题,并让您进行调试..当然,我根据您的建议更改了代码,我的错误与您在本文档中看到的相同--这是test Spring安全性的最低配置。docs.spring.io/spring-security/site/docs/current/reference/html/OK,我们将支持我添加的,但现在发生的情况是,我没有得到异常,但凭据与spring-security不匹配,我将被重定向到bitacora页面。我想做的是,一旦我输入凭据,如果凭据正确,我应该被重定向到bitacora页面,如果凭据为false,我应该停留在登录页面上并显示错误消息。对不起,我是spring security的新手,所以不了解它。谢谢您可能需要在此页面使用inMemoryAuthentication。。。。仔细阅读,你能在我的代码中看到吗?我已经拥有了你提到的页面中的所有配置/设置,因此我假设我不需要POST操作,因为在本文档中说:.inMemoryAuthentication()和.formLogin().loginPage(“/login”).defaultSuccessUrl(“/bitacora”)就足够了配置问题是由于某种原因表单无法找到此配置。我认为您不理解GET和POST之间的区别。你应该读一读。您没有登录GET操作。读兄弟
<form:form id="loginForm" method="post" action="${loginUrl}"
            modelAttribute="loginBean">
<c:url var="loginUrl" value="/j_spring_security_check" />
.formLogin()
    .loginProcessingUrl("/login")
    ...