2016-01-23 162 views
1

您好,我正面临'HTTP状态405 - 请求方法'POST'不支持' 尝试使用spring security进行登录时发生异常。HTTP状态405 - 不支持请求方法'POST'Spring Security Java配置

以下是我的代码:

pgLogin.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Login</title> 

<script type="text/javascript"> 
    function focusOnLoad() { 
     document.getElementById("username").focus(); 
    } 
</script> 

</head> 
<body onload="focusOnLoad()"> 
    <div align="center"> 

     <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="username" name="email" placeholder="Enter Email" 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> 

    </div> 
</body> 
</html> 

DesertLampSecurityConfiguration.java

package co.in.desertlamp.configuration; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 
import org.springframework.security.config.annotation.web.builders.HttpSecurity; 
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 

@Configuration 
@EnableWebSecurity 
public class DesertLampSecurityConfiguration extends WebSecurityConfigurerAdapter { 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder authenticationMgr) throws Exception { 
     authenticationMgr.inMemoryAuthentication() 
      .withUser("[email protected]") 
      .password("Dlpl123#") 
      .authorities("ROLE_SUPER_USER"); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.authorizeRequests() 
      .antMatchers("/home").access("hasRole('ROLE_SUPER_USER')") 
      .and() 
       .formLogin().loginPage("/loginPage") 
       .defaultSuccessUrl("/homePage") 
       .failureUrl("/loginPage?error") 
       .usernameParameter("email").passwordParameter("password") 
      .and() 
       .logout().logoutSuccessUrl("/loginPage?logout"); 
    } 
} 

DefaultController.java

package co.in.desertlamp.controller; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.servlet.ModelAndView; 

@Controller 
public class DefaultController { 

    @RequestMapping(value = { "/"}, method = RequestMethod.GET) 
    public ModelAndView welcomePage() { 
     ModelAndView model = new ModelAndView(); 
     model.setViewName("common/pgDefault"); 
     return model; 
    } 

    @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("common/pgLogin"); 
     return model; 
    } 

    @RequestMapping(value = { "/home"}, method = RequestMethod.GET) 
    public ModelAndView homePage() { 
     ModelAndView model = new ModelAndView(); 
     model.setViewName("common/pgWelcome"); 
     return model; 
    } 
} 

回答

1

更改您的合作nfig如下,以配合您的方法处理:

​​
+0

你好, 感谢。当我根据您的建议更改代码时, 在我从默认页面导航到登录页面时得到'HTTP状态405 - 请求方法'GET'不受支持''。 –

+0

您是否删除了@RequestMapping(value =“/ login”,method = RequestMethod.GET)'? –

+0

您需要两个控制器,一个用于呈现可能带有表单错误的表单,另一个用于处理认证。保留旧的@RequestMapping(value =“/ login”,method = RequestMethod.GET)'方法并为'POST'添加一个新的方法。 –

相关问题