【问题标题】:Request method 'POST' not supported - SPRING不支持请求方法“POST” - SPRING
【发布时间】:2016-09-13 07:29:11
【问题描述】:

login.jsp

<div id="login" class="animate form">
                    <form action="${loginUrl}" method="POST">
                        <h1>Log in</h1>

                        <c:url var="loginUrl" value="/login" />
                        <c:if test="${param.error != null}">
                            <input type="text" class="alert-danger" id="danger" name="danger"
                                placeholder="Invalid username and password." disabled />
                            <br />
                        </c:if>
                        <c:if test="${param.logout != null}">
                            <input type="text" class="alert-success" id="success"
                                name="success"
                                placeholder="You have been logged out successfully." disabled />
                            <br />
                        </c:if>


                        <p>
                            <label for="username" class="uname" data-icon="u"> Your
                                email </label> <input id="username" name="login" required="required"
                                type="text" placeholder="mymail@atos.net" />
                        </p>
                        <p>
                            <label for="password" class="youpasswd" data-icon="p">
                                Your password </label> <input id="password" name="password"
                                required="required" type="password" placeholder="eg. X8df!90EO" />
                        </p>
                        <p class="keeplogin">
                            <input type="checkbox" name="remember-me" id="rememberme"
                                value="rememberme" /> <label for="rememberme">Remember
                                Me</label>
                        </p>
                        <p class="login button">
                            <input type="submit" value="Login" />
                        </p>
                        <p class="change_link"></p>
                    </form>
                </div>

userlist.jsp

<div class="generic-container">
        <%@include file="authheader.jsp" %>   
        <div class="panel panel-default">
              <!-- Default panel contents -->
            <div class="panel-heading"><span class="lead">List of Users </span></div>
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>Prenom</th>
                        <th>Nom</th>
                        <th>Matricule</th>
                        <th>Login</th>
                        <sec:authorize access="hasRole('ADMIN') or hasRole('READ')">
                            <th width="100"></th>
                        </sec:authorize>
                        <sec:authorize access="hasRole('ADMIN')">
                            <th width="100"></th>
                        </sec:authorize>

                    </tr>
                </thead>
                <tbody>
                <c:forEach items="${users}" var="user">
                    <tr>
                        <td>${user.prenom}</td>
                        <td>${user.nom}</td>
                        <td>${user.matricule}</td>
                        <td>${user.login}</td>
                        <sec:authorize access="hasRole('ADMIN') or hasRole('READ')">
                            <td><a href="<c:url value='/edit-user-${user.login}' />" class="btn btn-success custom-width">edit</a></td>
                        </sec:authorize>
                        <sec:authorize access="hasRole('ADMIN')">
                            <td><a href="<c:url value='/delete-user-${user.login}' />" class="btn btn-danger custom-width">delete</a></td>
                        </sec:authorize>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>
        <sec:authorize access="hasRole('ADMIN')">
            <div class="well">
                <a href="<c:url value='/newuser' />">Add New User</a>
            </div>
        </sec:authorize>
    </div>

AppController.java

@Controller
@RequestMapping("/")
@SessionAttributes("roles")
public class AppController {

    @Autowired
    IService_User<USER> userService;

    @Autowired
    IService<COMPTE> compteService;

    @Autowired
    MessageSource messageSource;

    @Autowired
    PersistentTokenBasedRememberMeServices persistentTokenBasedRememberMeServices;

    @Autowired
    AuthenticationTrustResolver authenticationTrustResolver;

@RequestMapping(value = { "/", "/list" }, method = { RequestMethod.GET, RequestMethod.POST })
    public String listUsers(ModelMap model) {

        List<USER> users = userService.findAllOBJECTS();
        model.addAttribute("users", users);
        model.addAttribute("loggedinuser", getPrincipal());
        return "userslist";
    }

@RequestMapping(value = {"/login"}, method = { RequestMethod.GET, RequestMethod.POST })
    public String loginPage() {
        if (isCurrentAuthenticationAnonymous()) {
            return "login";
        } else {
            return "redirect:/list";
        }
    }
}

有 2 个页面: login.jsp - 起始页面,其中包括要填充登录名和密码的表单 - userlist.jsp 结果列表“显示所有用户持久保存在数据库中”.. 首先显示登录页面,当我单击提交按钮时出现此错误: org.springframework.web.servlet.PageNotFound - 不支持请求方法“POST”

【问题讨论】:

    标签: spring-mvc spring-security


    【解决方案1】:

    如果您使用的是 spring security 4.x.x。 , CSRF 默认开启。因此,您必须提供表单中提交的 csrf。

    将 csrf 令牌添加为隐藏字段就可以了:

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

    【讨论】:

      【解决方案2】:

      在您的登录表单中,您明确发出 POST 请求...而在您的控制器中,url 映射到 GET 请求。这就是问题...请将控制器设为 POST... 就像

      @RequestMapping(value = {"/login"}, method = RequestMethod.POST)
          public String loginPage() {
              if (isCurrentAuthenticationAnonymous()) {
                  return "login";
              } else {
                  return "redirect:/list";
              }
          }
      

      【讨论】:

      • 在您的 login.jsp 页面中...您的变量“loginUrl”为空/null ...请移动该行“" div 标签上方/div 标签外的行 "
        "
      • @FuSsA 请尝试 tis,.. 在您的 login.jsp 页面中...您的变量“loginUrl”为空/null ...请移动该行“ " div 标签上方/div 标签外"
        "
      • @FuSsA 请把你的 loginUrl 的值映射到 "//login" 类似 " " ...因为在您将“/”作为 RequestMapping 提供的控制器顶部..
      【解决方案3】:

      在您的 login.jsp 中,您使用的是 http 方法 POST

      <form action="${loginUrl}" method="POST">
      

      在控制器中,您使用的是 http 方法 GET

      @RequestMapping(value = {"/login"}, method = RequestMethod.GET)
          public String loginPage() {
              if (isCurrentAuthenticationAnonymous()) {
                  return "login";
              } else {
                  return "redirect:/list";
              }
          }
      

      像这样在控制器中更改 method = RequestMethod.POST 后问题将得到解决

      @RequestMapping(value = {"/login"}, method = RequestMethod.POST)
          public String loginPage() {
              if (isCurrentAuthenticationAnonymous()) {
                  return "login";
              } else {
                  return "redirect:/list";
              }
          }
      

      【讨论】:

        【解决方案4】:

        在@RequestMapping注解中添加post方法,如下所示;)

        @RequestMapping(value = {"/login"}, method = {RequestMethod.GET, RequestMethod.POST})
        public String loginPage() {
            if (isCurrentAuthenticationAnonymous()) {
                return "login";
            } else {
                return "redirect:/list";
            }
        }
        

        【讨论】:

        猜你喜欢
        相关资源
        最近更新 更多
        热门标签