2016-04-26 112 views
-1

我使用Spring MVC(3.2.2.RELEASE)和Spring Security(3.2.2.RELEASE)。錯誤405:請求方法'POST'不支持 - Spring Security Java Config

我試圖做一個基本的登錄使用春季安全,但每次我收到一個異常'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> 

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

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

SecurityWebApplicatio nInitializer.java

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; 
    } 
} 

Bitácora酒店-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> 
+1

你登錄或之前後得到405? – Tin

+0

登錄後..... –

+0

你可以對你的問題做一個最小的配置/設置嗎?這可以幫助我們看到問題,並且您可以調試它。 – xeor

回答

0

因爲你的服務(控制器)登錄操作是一個獲得操作,但你的UI(視圖)正在發送POST請求

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

你應該改變這

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

保持你的UI(查看)如(這與方法= 「郵報」)

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

好的,我們會添加,但現在會發生什麼,我沒有得到異常,但憑據與彈簧安全性不匹配我正在重定向到bitacora頁面。我想要做的是一旦我輸入憑證,如果憑證是正確的,我應該重定向到bitacora頁面,如果憑證是假的,我應該留在登錄頁面上,並顯示錯誤消息。對不起,我是新手來彈簧安全,所以沒有得到它。謝謝 –

+0

你可能需要使用inMemoryAuthentication從這個頁面... http://docs.spring.io/spring-security/site/docs/current/reference/html/jc.html。仔細閱讀 –

+0

正如你可以在我的代碼中看到的,我已經擁有了你提到的頁面中的所有配置/設置,因爲這個原因,我假設我不需要POST操作,因爲在本文檔中說:使用.inMemoryAuthentication()和.formLogin() .loginPage(「/ login」)。defaultSuccessUrl(「/ bitacora」)配置問題是由於某種原因FORM沒有找到此配置。 –

-1

您正在將您的登錄信息發佈到/login,但是Spring Security中的默認登錄處理URL 3.2/j_spring_security_check

變化的形式:

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

或者組登錄處理URL明確:

.formLogin() 
    .loginProcessingUrl("/login") 
    ... 
相關問題