How do I login?

Asked

Viewed 1,391 times

0

I’m having a problem logging in.

I have a simple page:

Essa aqui:

I only need to consult in the database if the value informed by the user is equal.

This one here:

Model:

@Entity
public class User {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;
    @Column
    private String login;
    @Column
    private String password;
    @Column
    private String birdayDate;
    @Column
    private String mail;

    @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");
}
  • 1

    When I learned about the subject I used this blog, I found it very explanatory: http://blog.thiagobelem.net/criando-um-systems-de-login-com-php-e-mysql

  • @Bilico, I liked this blog, the only problem is that I have a little difficulty in PHP. I started working with Java web development a couple of weeks ago. Until yesterday my exits were all on the console, and now I’m with this universe of things.

1 answer

2


Search the bank for someone with the login and password entered. 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 the authentication failed, otherwise the user has logged in correctly.

  • This piece of code I put where?

  • 1

    In the data layer (DAO), and you can call this method in the controller.

  • Thanks! Solved the problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.