首页 > 解决方案 > Spring boot http request always handled by the same method

问题描述

I'm trying to perform the user login functionality in my spring boot application, but it seems that all the http requests are handled by the same method, means that they're redirected into the same URL.

Here is my login controller

    package com.lyes.webmail.demo.controllers;


    import com.lyes.webmail.demo.DAO.UserDAO;
    import org.springframework.beans.factory.annotation.Autowired;
    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;

   @Controller
   public class LoginController {

@Autowired
UserDAO asi;


@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(){
    System.out.println("i'm here"); return "login";
}



@RequestMapping(value = "/connection", method = RequestMethod.POST)
public String logMe(@RequestParam String email, @RequestParam String password){
    if(asi.login(email,password)){
        System.out.println("success");
        return "home";
    }else{
        System.out.println("failure");
        return "error";
    }
}

}

my login form

               <form id="login-form" class="form" action="/connection" method="POST">
                    <h3 class="text-center text-info">Login to lyes web mail</h3>
                    <div class="form-group">
                        <label for="username" class="text-info">Email:</label><br>
                        <input type="text" name="email" id="username" class="form-control">
                    </div>
                    <div class="form-group">
                        <label for="password" class="text-info">Password:</label><br>
                        <input type="text" name="password" id="password" class="form-control">
                    </div>
                    <div class="form-group">
                        <input type="submit" name="submit" class="btn btn-info btn-md" value="Login">
                    </div>
                </form>

And the SecurityConfig which extends the WebSecurityConfigurerAdapter in order to render the login page as the welcome page.

   @EnableWebSecurity
   public class SecurityConfig extends WebSecurityConfigurerAdapter {

  @Override
  protected  void configure(HttpSecurity httpSecurity) throws Exception {
    System.out.println("lyes");
    httpSecurity.authorizeRequests()
            .anyRequest()
            .authenticated()
            .and()
            .formLogin()
            .loginPage("/login")
            .permitAll();
   }
}

In the login controller method i've put a sysout to analyse the behaviour, and the message i'm here is printed twice, the first time when the login page loads and the second time when i submit the form.

Any suggestions !??

标签: springspring-boot

解决方案


推荐阅读