Reputation: 950
The issue is that I want to use object AdminLogin to authenticate administrators.
Here is the AdminLogin class
package com.mintad.admin.beans;
import java.io.Serializable;
import org.springframework.stereotype.Component;
/**
*
* Used to authenticate admin
*
*/
@Component("adminLogin")
public class AdminLogin implements Serializable {
private static final long serialVersionUID = -6394045014528037520L;
private String username;
private String password;
public AdminLogin() {
}
public AdminLogin(String username, String password) {
super();
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
And controllers methods
@ModelAttribute("adminLogin")
public AdminLogin createModel() {
return new AdminLogin();
}
@RequestMapping(value = "/admin/login", method = RequestMethod.POST)
public ModelAndView login(@ModelAttribute AdminLogin adminLogin, HttpServletRequest request,
@RequestParam(value = "error", required = false) String error, @RequestParam(value = "logout", required = false) String logout) {
LOGGER.debug("admin login page");
ModelAndView model = new ModelAndView();
model.addObject("adminLogin", adminLogin);
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;
}
@RequestMapping(value = "/admin/login", method = RequestMethod.GET)
public String greetingForm(Model model) {
model.addAttribute("adminLogin", new AdminLogin());
return "/admin/login";
}
The issue is that the form is submitted with GET although It's POST like the following ;
<form class="m-t" role="form" th:action="@{/admin/login}" th:object="${adminLogin}" 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" th:field="*{username}" placeholder="Username" required="" />
</div>
<div class="form-group">
<input type="password" class="form-control" id="password" name="password" th:field="*{password}" placeholder="Password" 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>
I have added the GET to confirm my thaughts and also because I've seen examples about how to use objects in login forms.
Please help me fix this or would you please give a cleaner way to achieve my goals.
Thanks !
Upvotes: 0
Views: 251
Reputation: 9155
Try removing the space in your method=" POST"
in your form. It's case-insensitive, but acceptable values are GET
and POST
. The default method is GET
.
Upvotes: 2