From 67a9e868de9019864ad99e870a4987aa8962e47c Mon Sep 17 00:00:00 2001 From: Emanuel Date: Thu, 12 May 2022 11:40:43 -0400 Subject: [PATCH] Login with MD5 HASH ENCRYPTION ADDED --- src/main/java/com/roshka/proyectofinal/DataBase.java | 2 +- src/main/java/com/roshka/proyectofinal/entity/LoginBean.java | 9 +++++++++ src/main/java/com/roshka/proyectofinal/entity/Usuario.java | 1 + src/main/java/com/roshka/proyectofinal/login/LoginDao.java | 14 +++++++------- src/main/java/com/roshka/proyectofinal/login/LoginServlet.java | 16 +++++++++++++++- src/main/java/com/roshka/proyectofinal/login/md5JavaHash.java | 34 ++++++++++++++++++++++++++++++++++ src/main/webapp/login.jsp | 5 +++-- 7 files changed, 70 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/roshka/proyectofinal/login/md5JavaHash.java diff --git a/src/main/java/com/roshka/proyectofinal/DataBase.java b/src/main/java/com/roshka/proyectofinal/DataBase.java index 13fdb92..12d2450 100644 --- a/src/main/java/com/roshka/proyectofinal/DataBase.java +++ b/src/main/java/com/roshka/proyectofinal/DataBase.java @@ -10,7 +10,7 @@ public class DataBase { try{ Class.forName("org.postgresql.Driver"); con= DriverManager - .getConnection("jdbc:postgresql://localhost:5432/Bootcamp_th", + .getConnection("jdbc:postgresql://localhost:5432/bootcamp_th", "postgres", "postgres"); if(con != null){ diff --git a/src/main/java/com/roshka/proyectofinal/entity/LoginBean.java b/src/main/java/com/roshka/proyectofinal/entity/LoginBean.java index 9b2ad7e..21a9de6 100644 --- a/src/main/java/com/roshka/proyectofinal/entity/LoginBean.java +++ b/src/main/java/com/roshka/proyectofinal/entity/LoginBean.java @@ -3,6 +3,7 @@ package com.roshka.proyectofinal.entity; public class LoginBean { private String username; private String password; + private String correo; public String getUsername() { return username; @@ -19,4 +20,12 @@ public class LoginBean { public void setPassword(String password) { this.password = password; } + + public void setCorreo(String correo) { + this.correo = correo; + } + + public String getCorreo() { + return correo; + } } diff --git a/src/main/java/com/roshka/proyectofinal/entity/Usuario.java b/src/main/java/com/roshka/proyectofinal/entity/Usuario.java index d307639..961768b 100644 --- a/src/main/java/com/roshka/proyectofinal/entity/Usuario.java +++ b/src/main/java/com/roshka/proyectofinal/entity/Usuario.java @@ -4,6 +4,7 @@ public class Usuario { private int id; private String nombre,apellido,correo,contrasena; + public Usuario() { } diff --git a/src/main/java/com/roshka/proyectofinal/login/LoginDao.java b/src/main/java/com/roshka/proyectofinal/login/LoginDao.java index 8ab20f8..3605cbb 100644 --- a/src/main/java/com/roshka/proyectofinal/login/LoginDao.java +++ b/src/main/java/com/roshka/proyectofinal/login/LoginDao.java @@ -6,27 +6,27 @@ import com.roshka.proyectofinal.entity.LoginBean; import java.sql.Connection; import java.sql.PreparedStatement; +import java.sql.ResultSet; public class LoginDao { public boolean validate (LoginBean loginBean) { - int status = 0; + boolean status = false; try { Connection con = DataBase.getConnection(); PreparedStatement ps=con.prepareStatement( - "select * from usuarios where username=? and password = ?"); - ps.setString(1,loginBean.getUsername()); + "select * from usuario where correo=? and contrasena = ?"); + ps.setString(1,loginBean.getCorreo()); ps.setString(2, loginBean.getPassword()); - status=ps.executeUpdate(); - + ResultSet rs = ps.executeQuery(); + status = rs.next(); con.close(); } catch (Exception ex) { ex.printStackTrace(); } - if (status > 0) return true ; - else return false ; + return status ; } diff --git a/src/main/java/com/roshka/proyectofinal/login/LoginServlet.java b/src/main/java/com/roshka/proyectofinal/login/LoginServlet.java index c251007..85dcceb 100644 --- a/src/main/java/com/roshka/proyectofinal/login/LoginServlet.java +++ b/src/main/java/com/roshka/proyectofinal/login/LoginServlet.java @@ -2,6 +2,8 @@ package com.roshka.proyectofinal.login; import java.io.IOException; +import java.security.NoSuchAlgorithmException; + import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; @@ -9,6 +11,8 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import com.roshka.proyectofinal.entity.LoginBean; +import com.roshka.proyectofinal.login.md5JavaHash; + /** * Servlet implementation class LoginServlet @@ -38,13 +42,23 @@ public class LoginServlet extends HttpServlet { */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { LoginDao loginDao = new LoginDao(); + md5JavaHash passEncrip = new md5JavaHash(); + String passwordMD5 = ""; String username = request.getParameter("username"); + String correo = request.getParameter("correo"); String password = request.getParameter("password"); LoginBean loginBean = new LoginBean(); loginBean.setUsername(username); - loginBean.setPassword(password); + try { + passwordMD5 = passEncrip.getHashPass(password); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + loginBean.setPassword(passwordMD5); + loginBean.setCorreo(correo); + System.out.println("EL pass encriptado es: " +passwordMD5); if (loginDao.validate(loginBean)) { diff --git a/src/main/java/com/roshka/proyectofinal/login/md5JavaHash.java b/src/main/java/com/roshka/proyectofinal/login/md5JavaHash.java new file mode 100644 index 0000000..b314c66 --- /dev/null +++ b/src/main/java/com/roshka/proyectofinal/login/md5JavaHash.java @@ -0,0 +1,34 @@ +package com.roshka.proyectofinal.login; +import java.security.*; + + +public class md5JavaHash { + + private String hashpass=""; + + public String getHashPass(String password) throws + NoSuchAlgorithmException{ + + String plainText = password; + MessageDigest mdAlgorithm = MessageDigest.getInstance("MD5"); + mdAlgorithm.update(plainText.getBytes()); + + byte[] digest = mdAlgorithm.digest(); + StringBuffer hexString = new StringBuffer(); + + for (int i = 0; i < digest.length; i++) { + plainText = Integer.toHexString(0xFF & digest[i]); + + if (plainText.length() < 2) { + plainText = "0" + plainText; + } + + hexString.append(plainText); + } + hashpass = hexString.toString(); + + return hashpass; + } + + +} diff --git a/src/main/webapp/login.jsp b/src/main/webapp/login.jsp index 1f1dadf..c587790 100644 --- a/src/main/webapp/login.jsp +++ b/src/main/webapp/login.jsp @@ -8,9 +8,10 @@

User Login Form

- - +
User Name:
+ +
Correo:
Password:
-- libgit2 0.26.0