Java user authentication using Mysql database and Framework spring boot

Asked

Viewed 397 times

0

good afternoon. I am in the development of a web application using the framework spring boot, Ibernate and jpa with the mysql database. Finally I have a question about the authentication of this user because until now I can only save this user in the database but how can I authenticate it and know if the data in the database is the same that the user is typing in the form? good.. I will show here the only part of the backend because the doubt is all in the backend. this and the part where I used Hibernate.

/**
 Classe de persistência de dados ao banco mysql
 */

package com.web.Models;

import java.io.Serializable;

import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Generatedvalue; import javax.persistence.Generationtype; import javax.persistence.Id; import javax.persistence.Table; import javax.validation.constraints.Notnull;

@Entity @Table(name = "login") public class Login Implements Serializable{

private static final long serialVersionUID = -7123394242940032066L;

@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private long id;

@NotNull
@Column(name = "usuario")
private String usuario;

@NotNull
@Column(name = "senha")
private String senha;


public Login() {

}
public Login(String usuario, String senha, long id) {
    this.usuario = usuario;
    this.senha = senha;
    this.id = id;
}


public long getId() {
    return id;
}
public void setId(long id) {
    this.id = id;
}
public String getUsuario() {
    return usuario;
}
public void setUsuario(String usuario) {
    this.usuario = usuario;
}
public String getSenha() {
    return senha;
}
public void setSenha(String senha) {
    this.senha = senha;
}

}

here the Repository

package com.web.Repository;

import org.springframework.data.repository.Crudrepository; import org.springframework.stereotype.Repository;

import com.web.Models.Login;

@Repository public interface Loginrepository extends Crudrepository{

}

and here the controller the user registration mappings usually work only the user validatorUsuario that does not work yet.

package com.web.Controller;

import org.springframework.Beans.factory.Annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.Annotation.Requestbody; import org.springframework.web.bind.Annotation.Requestmapping; import org.springframework.web.bind.Annotation.Requestmethod; import org.springframework.web.bind.Annotation.Responsebody;

import com.web.Models.Login; import com.web.Repository.Loginrepository; import com.web.Util.Log;

import ch.qos.logback.classic.Logger;

@Controller public class Logincontroller { @Autowired Private loginrepository Repository;

@RequestMapping(value="/cadastrarUsuario", method=RequestMethod.GET)
public String form() {
    return "addUser/cadastrarLogin";
}
@RequestMapping(value="/cadastrarUsuario", method=RequestMethod.POST)
public String form(Login login) {
    repository.save(login);
    return "redirect:/cadastrarUsuario";
}


@RequestMapping(value="/validarUsuario", method=RequestMethod.GET)
public String validform() {
    return "validUsuario/validarLogin";
}

because only the part of the controller that I cannot validate this user, I did not create a service class for reasons of exercise of the faculty only with Repository,controller and model, who can help me?

}
  • Weltonsistemas, have you ever thought about using JWT or another authentication and authorization mechanism? I’m sending you a link to take a look. https://receitasdecodigo.com.br/spring-boot/autenticacao-com-jwt-em-projetos-spring-boot

  • If you actually seek to do only one simulation without using any authentication mechanism, that is, if you want to do only one query validating whether or not the user exists.

No answers

Browser other questions tagged

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