How to do a login authentication?

0

I'm having a problem signing in to.

I have a simple page:

Ijustneedtoquerythedatabaseifthevalueenteredbytheuseristhesame.

Thisonehere:

Model:

@EntitypublicclassUser{@Id@GeneratedValue(strategy=GenerationType.IDENTITY)privateIntegerid;@ColumnprivateStringlogin;@ColumnprivateStringpassword;@ColumnprivateStringbirdayDate;@ColumnprivateStringmail;@OneToOne(mappedBy="user")
    private Company company;

    public User() {

    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getBirdayDate() {
        return birdayDate;
    }

    public void setBirdayDate(String birdayDate) {
        this.birdayDate = birdayDate;
    }

    public String getMail() {
        return mail;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

}

View:

<!-- BEGIN LOGIN FORM -->
<form id="form-login" method="POST" class="p-t-15" role="form"
   action="login">
   <!-- BEGIN USERID -->
   <div class="form-group form-group-default">
      <label>Usuário</label>
      <div class="controls">
         <input type="text" name="userId" id="userId"
            placeholder="Usuário" class="form-control" required>
      </div>
   </div>
   <!-- END USERID -->
   <!-- BEGIN PASSWORD -->
   <div class="form-group form-group-default">
      <label>Senha</label>
      <div class="controls">
         <input type="password" class="form-control" name="password"
            placeholder="Senha" required>
      </div>
   </div>
   <!-- END PASSWORD -->
   <!-- BEGIN SUBMIT BUTTON -->
   <button class="btn btn-primary btn-cons m-t-10" id="loginButton"
      type="submit">Entrar</button>
   <!-- END SUBMIT BUTTON -->
</form>
<!-- END LOGIN FORM -->

Controller:

@RequestMapping(value = "/public/login", method = RequestMethod.GET)
public ModelAndView loginPage() {
    return new ModelAndView("login");
}

@RequestMapping(value = "/public/login", method = RequestMethod.POST)
public ModelAndView verifyLogin(@RequestParam String userId, @RequestParam String password) {

    User user = new User();


    return new ModelAndView("home");
}
    
asked by anonymous 12.06.2017 / 23:21

1 answer

2

Search the bank for someone with the login and password informed. Example:

public Usuario pesquisaUsuarioLogin(String login, String senha) {
    Usuario usuario = new Usuario ();
    usuario.setLogin(login);
    usuario.setSenha(senha);
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    Query select = session.createQuery("from Usuario where login = :login and senha = :senha");
    select.setString("login", gestor.getLogin());
    select.setString("senha", gestor.getSenha());
    usuario = (Usuario) select.uniqueResult();
    return usuario;
}

If the method returns a null that says authentication failed, otherwise the user has successfully logged in.

    
14.06.2017 / 00:16