简体   繁体   English

错误 405:不支持请求方法“POST” - Spring Security Java Config

[英]Error 405: Request method 'POST' not supported - Spring Security Java Config

I am using Spring MVC (3.2.2.RELEASE) and Spring Security (3.2.2.RELEASE).我正在使用 Spring MVC (3.2.2.RELEASE) 和 Spring Security (3.2.2.RELEASE)。

I'm trying to do a basic login using spring security, but every time I receive an exception 'HTTP Status 405 - Request method 'POST' not supported'.我正在尝试使用 Spring Security 进行基本登录,但每次收到异常“HTTP 状态 405 - 不支持请求方法 'POST'”时。 I already tried searching for similar issues but I was not able to find any solutions.我已经尝试搜索类似的问题,但找不到任何解决方案。

Following is my code:以下是我的代码:

login.jsp登录.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>

SecurityConfig.java安全配置文件

package com.bitacora.config;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/login").permitAll()
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login").defaultSuccessUrl("/bitacora")
            .failureUrl("/login?error")
            .usernameParameter("nombre").passwordParameter("password")
            .and()
        .logout()
            .logoutSuccessUrl("/login?logout").permitAll()
            .and()
        .csrf();    
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
                .withUser("user").password("123").roles("USER", "ADMIN");
    }
}

MvcWebApplicationInitializer.java 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[] { "/" };
    }
}

SecurityWebApplicationInitializer.java SecurityWebApplicationInitializer.java

package com.bitacora.config.core;

public class SecurityWebApplicationInitializer extends
        AbstractSecurityWebApplicationInitializer {

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

}

LoginController.java登录控制器.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 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

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

Because your service(Controller) login operation is a get operation but your UI (view) is sending a post request因为您的服务(控制器)登录操作是获取操作,但您的 UI(视图)正在发送发布请求

  @RequestMapping(value = "/login", method = RequestMethod.GET)

you should change this to你应该把它改成

  @RequestMapping(value = "/login", method = RequestMethod.POST)

keep your UI (View) as is ( which is with method="post")保持您的 UI(视图)原样(使用 method="post")

<form:form id="loginForm" method="post" action="${loginUrl}"
            modelAttribute="loginBean">

You are posting your login information to /login , but the default login processing URL in Spring Security 3.2 is /j_spring_security_check .您将登录信息发布到/login ,但 Spring Security 3.2 中的默认登录处理 URL 是/j_spring_security_check

Change in your form:改变你的形式:

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

Or set login processing URL explicit:或者显式设置登录处理 URL:

.formLogin()
    .loginProcessingUrl("/login")
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HTTP状态405-请求方法&#39;POST&#39;不支持Spring Security Java Config - HTTP Status 405 - Request method 'POST' not supported Spring Security Java Config "Spring Security - 不支持 405 请求方法“POST”" - Spring Security - 405 Request Method 'POST' Not Supported 405 - 不支持请求方法“POST” Spring MVC + Spring 安全 - 405 - Request method 'POST' not supported Spring MVC + Spring Security 具有Spring Security的Spring-mvc获得HTTP状态405-请求方法&#39;POST&#39;不支持 - Spring-mvc with spring security getting HTTP Status 405 - Request method 'POST' not supported HTTP状态405 - 使用Spring Security的Spring MVC中不支持请求方法'POST' - HTTP Status 405 - Request method 'POST' not supported in Spring MVC with Spring Security 错误405:不支持请求方法POST - Error 405: Request method POST not supported 启用弹簧安全性的POST请求显示“不支持405 GET” - POST request says “405 GET not supported” with Spring security enabled Spring MVC的CURL POST请求给出405方法&#39;POST&#39;不支持 - Curl POST request for spring mvc gives 405 Method 'POST' not supported Spring security登录报错“不支持请求方法‘POST’” - Spring security login error "Request method 'POST' not supported" 405不允许的方法:请求方法&#39;POST&#39;不支持| Ajax / Spring MVC - 405 Method Not Allowed : Request method 'POST' not supported | Ajax/Spring MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM