Spring Security 4:请求方法';邮政';不支持
问题是,当我点击submit时,我得到Spring Security 4:请求方法';邮政';不支持,spring,post,spring-security,Spring,Post,Spring Security,问题是,当我点击submit时,我得到 Etat HTTP 405 - Request method 'POST' not supported 这是控制器: @RequestMapping(value = "/admin/login", method = RequestMethod.GET) public ModelAndView login( @RequestParam(value = "error", required = false) String error,
Etat HTTP 405 - Request method 'POST' not supported
这是控制器:
@RequestMapping(value = "/admin/login", method = RequestMethod.GET)
public ModelAndView login(
@RequestParam(value = "error", required = false) String error,
@RequestParam(value = "logout", required = false) String logout) {
LOGGER.debug("admin login page");
ModelAndView model = new ModelAndView();
if (error != null) {
model.addObject("error", "Invalid username and password!");
}
if (logout != null) {
model.addObject("msg", "You've been logged out successfully.");
}
model.setViewName("/admin/index");
LOGGER.debug("returning admin login page");
return model;
}
及表格:
<form class="m-t" role="form" th:action="@{/admin/login}" method="POST" autocomplete="off">
<div th:if="${param.error}" class="alert alert-danger">Invalid username and password.</div>
<div th:if="${param.logout}" class="alert alert-success">You have been logged out.</div>
<div class="form-group">
<input type="text" class="form-control" id="username" name="username" placeholder="Username" required="" />
</div>
<div class="form-group">
<input type="password" class="form-control" id="username" name="username" placeholder="Username" required="" />
</div>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<button type="submit" class="btn btn-primary block full-width m-b">Login</button>
<a href="#"><small>Forgot password?</small></a>
<p class="text-muted text-center">
<small>Do not have an account?</small>
</p>
<a class="btn btn-sm btn-white btn-block" href="register.html">Create an account</a>
</form>
获取405的原因是,您试图使用http post方法提交表单,并使用http get方法定义端点 您需要将请求映射更改为方法,如下所示:
@RequestMapping(value = "/admin/login", method = RequestMethod.POST)
或者,您也可以在您的表单中执行相反的操作(出于安全原因,不建议这样做),如:
<form class="m-t" role="form" th:action="@{/admin/login}" method="GET" autocomplete="off">
在控制器中,您将表单定义为
GET
:
@RequestMapping(value = "/admin/login", method = RequestMethod.GET)
在表单中,您将其称为POST
:
form class="m-t" role="form" th:action="@{/admin/login}" method="POST"
在控制器或表单中更改其中一项
如果要在控制器处更改,请将现有线路替换为:
@RequestMapping(value = "/admin/login", method = RequestMethod.POST)
或者您可以通过修改表单中的调用来实现,如
form class="m-t" role="form" th:action="@{/admin/login}" method="GET"
将安全配置更改为使用
/admin/login
作为登录页面:
...
.formLogin().loginPage("/admin/login")
此登录页不必更改,因为它将用于正常的网站部分。谢谢@SMA。这就是解决方案:)我不明白这个@Sofiane相同的答案在我写了一个小时后被Sidhu重复了一遍,你接受了复制的答案?当我打开最旧的选项卡时,Sidhu的答案首先出现了。我在你的答案和sidhu的答案上都看不到任何日期。好吧,那不是你的问题。检查Sidhu在回答后(7分钟后)所写的一种方法是单击个人姓名(就在X小时前回答的下方),然后在右侧悬停X小时前,您会看到..@ManuelJordan同意您的意见,如果您阅读了答案,我提到我不推荐相同的。明智发送登录数据如何获取?我不这么认为。
...
.formLogin().loginPage("/admin/login")