2016-04-26 116 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; 
    } 
} 

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> 
+1

あなたがでたりする前にログの後に405を得ましたか。 – Tin

+0

ログイン後..... –

+0

あなたはあなたの質問の最小の設定/セットアップを行うことができますか?それは私たちが問題を見て、それをデバッグするのに役立ちます.. – xeor

答えて

0

あなたのサービス(コントローラー)ログイン操作ため、取得操作ですが、あなたのUI(ビュー)は、ポスト要求

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

を送信しているあなたは

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

にこれを変更する必要があります(メソッド= "ポスト" である)であるとして、あなたのUI(ビュー)を維持

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

OK、私は追加しましたが、今は何が起こるのですか?例外はありませんが、 bitacoraページにリダイレクトされています。私がしたいのは、資格情報が正しい場合、資格情報が正しい場合、私はbitacoraページにリダイレクトする必要があり、資格情報がfalseの場合、ログインページにエラーメッセージを残す必要があります。申し訳ありませんが、私はそれを取得しないようにセキュリティを春に初心者です。ありがとう –

+0

このページのinMemoryAuthenticationを使う必要があるかもしれません... http://docs.spring.io/spring-security/site/docs/current/reference/html/jc.html。それをよく読んでください –

+0

私のコードでは、あなたが言及したページにあるすべての設定/設定を既に持っているので、この文書ではPOST操作が必要ないと仮定しています。 .inMemoryAuthentication()と.formLogin()で十分です。 .loginPage( "/ login")。defaultSuccessUrl( "/ bitacora")設定問題は、何らかの理由により、この設定が見つかりません。 –

-1

/loginにログイン情報を投稿していますが、Spring Security 3.2のデフォルトのログイン処理URLは/j_spring_security_checkです。フォームで

変更:明示的な

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

またはセットのログイン処理URL:

.formLogin() 
    .loginProcessingUrl("/login") 
    ... 
関連する問題