-1

I have this error when I try to get access to the loginCheck method, don’t redirect me to see CONTACTS view or redirect me to LOGIN view, for both, I added an if sentence.

I don’t know the reason for this error, only for method request of type POST.

I added logs to see what's happen when I do request to those methods and I obtain this: Logs

When going to URL “localhost/8080/” I redirect to LOGIN view as appear in the image and when I push button to sign in in LOGIN view the error are present. This is the code of my controller:

@Controller

public class LoginController {

private static final Log LOG = LogFactory.getLog(LoginController.class);
@GetMapping("/")
public String redirectToLogin() {
    LOG.info("METHOD: redirectToLogin()");
    return "redirect:/login";
}

@GetMapping("/login")
public String showLoginForm(Model model,
        @RequestParam(name="error", required=false) String error,
        @RequestParam(name="logout", required=false) String logout) {
    LOG.info("METHOD: showLoginForm() -- PARAMS: error"+ error + ", logout:" + logout);
    model.addAttribute("error", error);
    model.addAttribute("logout", logout);
    model.addAttribute("userCredentials", new UserCredential());
    LOG.info("Returning to login view");
    return "login";
}

@PostMapping("/logincheck")
public String loginCheck(@ModelAttribute(name="userCredentials") UserCredential userCredential) {
    LOG.info("METHOD: loginCheck() -- PARAMS: " + userCredential.toString());
    if(userCredential.getUsername().equals("user") && userCredential.getPassword().equals("user")) {
        LOG.info("Returning to contacts view");
        return "contacts";
    }
    LOG.info("Redirect to login?error");
    return "redirect:/login?error";
}
}

this is the LOGIN view code:

Signin

<div class="container">

  <form class="form-signin" action="#" th:action="@{/logincheck}" th:object="${userCredentials}" metod="post">
    <h2 class="form-signin-heading">Sign in</h2>
    <label for="inputEmail" class="sr-only">Username</label>
    <input type="text" id="inputUsername" class="form-control" placeholder="Username" th:field="*{username}"/>
    <label for="inputPassword" class="sr-only">Password</label>
    <input type="password" id="inputPassword" class="form-control" placeholder="Password" th:field="*{password}"/>
    <div th:if="${error != null}" class="alert alert-danger" role="alert">Invalid username or password</div>
    <button class="btn btn-lg btn-primary btn-black" type="submit">
        <span class="glyphicon glyphicon-log-in" aria-hiden="true"></span>Sign in
    </button>
  </form>

</div> <!-- /container -->

Arpit
  • 6,212
  • 8
  • 38
  • 69

1 Answers1

0

When you access the loginCheck url on the browser it sends a GET request to the application server. But your controller is expecting a POST request, hence the error. You can modify the controller to handle GET or use another way, for example AJAX, to login with a POST.

rohit-biswas
  • 815
  • 1
  • 11
  • 23