diff --git a/build/web/WEB-INF/authentifJSP.jsp b/build/web/WEB-INF/authentifJSP.jsp index c8db0a1..70a4047 100644 --- a/build/web/WEB-INF/authentifJSP.jsp +++ b/build/web/WEB-INF/authentifJSP.jsp @@ -44,7 +44,11 @@
-
+
diff --git a/build/web/WEB-INF/classes/bean/Utilisateur.class b/build/web/WEB-INF/classes/bean/Utilisateur.class new file mode 100644 index 0000000..808e259 Binary files /dev/null and b/build/web/WEB-INF/classes/bean/Utilisateur.class differ diff --git a/build/web/WEB-INF/classes/form/AuthentifForm.class b/build/web/WEB-INF/classes/form/AuthentifForm.class new file mode 100644 index 0000000..4d44388 Binary files /dev/null and b/build/web/WEB-INF/classes/form/AuthentifForm.class differ diff --git a/build/web/WEB-INF/classes/mysql/Connexion.class b/build/web/WEB-INF/classes/mysql/Connexion.class new file mode 100644 index 0000000..5828255 Binary files /dev/null and b/build/web/WEB-INF/classes/mysql/Connexion.class differ diff --git a/build/web/WEB-INF/classes/mysql/utilisateurMysql.class b/build/web/WEB-INF/classes/mysql/utilisateurMysql.class new file mode 100644 index 0000000..acdc012 Binary files /dev/null and b/build/web/WEB-INF/classes/mysql/utilisateurMysql.class differ diff --git a/build/web/WEB-INF/classes/servlet/AuthentifServlet.class b/build/web/WEB-INF/classes/servlet/AuthentifServlet.class index 26bb2ee..97a65a4 100644 Binary files a/build/web/WEB-INF/classes/servlet/AuthentifServlet.class and b/build/web/WEB-INF/classes/servlet/AuthentifServlet.class differ diff --git a/build/web/WEB-INF/web.xml b/build/web/WEB-INF/web.xml index cd9fedf..1f83678 100644 --- a/build/web/WEB-INF/web.xml +++ b/build/web/WEB-INF/web.xml @@ -4,8 +4,4 @@ AuthentifServlet servlet.AuthentifServlet - - AuthentifServlet - /authentification - diff --git a/nbproject/private/private.xml b/nbproject/private/private.xml new file mode 100644 index 0000000..4750962 --- /dev/null +++ b/nbproject/private/private.xml @@ -0,0 +1,4 @@ + + + + diff --git a/src/java/bean/Utilisateur.java b/src/java/bean/Utilisateur.java new file mode 100644 index 0000000..6152000 --- /dev/null +++ b/src/java/bean/Utilisateur.java @@ -0,0 +1,59 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package bean; + +/** + * + * @author francois + */ +public class Utilisateur { + public String prenom; + public String nom; + public String pseudo; + public String motDePasse; + + public String getPseudo() { + return this.pseudo; + } + + public String getNom() { + return this.nom; + } + + public String getPrenom() { + return this.prenom; + } + + public String getMotDePasse() { + return this.motDePasse; + } + + public void setPseudo(String pseudo) { + this.pseudo = pseudo; + } + + public void setPrenom(String prenom) { + this.prenom = prenom; + } + + public void setNom(String nom) { + this.nom = nom; + } + + public void getMotDePasse(String motDePasse) { + this.motDePasse = motDePasse; + } + + public Utilisateur(String nom, String prenom, String pseudo, String motDePasse) { + this.nom = nom; + this.prenom = prenom; + this.pseudo = pseudo; + this.motDePasse = motDePasse; + } + + public boolean equals(String one, String two) { + return one.equals(two); + } +} diff --git a/src/java/form/AuthentifForm.java b/src/java/form/AuthentifForm.java new file mode 100644 index 0000000..2988adb --- /dev/null +++ b/src/java/form/AuthentifForm.java @@ -0,0 +1,33 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package form; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpSession; +import mysql.utilisateurMysql; + +/** + * + * @author francois + */ +public class AuthentifForm { + HttpServletRequest request; + + public AuthentifForm(HttpServletRequest request) { + this.request = request; + } + + public boolean ctrlAuthentif(String username, String mail, String mdp) { + utilisateurMysql utilisateurSQL = new utilisateurMysql(); + var utilisateurLogged = utilisateurSQL.readAuthentif(username, mdp); + if (utilisateurLogged != null) { + HttpSession maSession = request.getSession(); + maSession.setAttribute("currentUtilisateur", utilisateurLogged); + return true; + } else { + return false; + } + } +} diff --git a/src/java/mysql/Connexion.java b/src/java/mysql/Connexion.java new file mode 100644 index 0000000..6ecdd58 --- /dev/null +++ b/src/java/mysql/Connexion.java @@ -0,0 +1,51 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package mysql; + +import java.sql.Connection; +import java.sql.DriverManager; + +/** + * + * @author francois + */ +public class Connexion { + private static Connection connect; + + /** + * Constructeur + * + * @param serveur nom du serveur, localhost si local + * @param bdd nom de la base de données + * @param nomUtil nom utilisateur + * @param mdp mot de passe lié à l'utilisateur + */ + public Connexion(String serveur, String bdd, String nomUtil, String mdp) { + try { + Class.forName("com.mysql.cj.jdbc.Driver"); + + String url = "jdbc:mysql://" + serveur + "/" + bdd; + + connect = DriverManager.getConnection(url, nomUtil, mdp); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Retourne la connection établie (Création d'une connection si elle n'existe + * pas) + * + * @param serveur nom du serveur, localhost si local + * @param bdd nom de la base de données + * @param nomUtil nom utilisateur + * @param mdp mot de passe lié à l'utilisateur + * @return connection établie + */ + public Connection getConnection() { + return connect; + } +} diff --git a/src/java/mysql/utilisateurMysql.java b/src/java/mysql/utilisateurMysql.java new file mode 100644 index 0000000..a5b5b73 --- /dev/null +++ b/src/java/mysql/utilisateurMysql.java @@ -0,0 +1,72 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package mysql; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.sql.*; + +import bean.Utilisateur; + +/** + * + * @author francois + */ +public class utilisateurMysql { + private Connection theConnection; + + public utilisateurMysql() { + Connexion connexion = new Connexion("127.0.0.1", "bdclient", "java", "java"); + this.theConnection = connexion.getConnection(); + } + + public Utilisateur readAuthentif(String login, String mdp) { + Utilisateur util = null; + try { + PreparedStatement prepStmt = null; + String sql = "SELECT * FROM utilisateur WHERE login=? AND mdp=MD5(?) "; + prepStmt = theConnection.prepareStatement(sql); + prepStmt.setString(1, login); + prepStmt.setString(2, mdp); + ResultSet result = prepStmt.executeQuery(); + if (result.next()) { + util = new Utilisateur(result.getString(1), result.getString(2), + result.getString(3), result.getString(4)); + } + prepStmt.close(); + } catch (SQLException ex) { + System.out.println("SQLExeption : " + ex.getMessage()); + System.out.println("SQLState : " + ex.getSQLState()); + System.out.println("Code erreur : " + ex.getErrorCode()); + } + return util; + } + + public int createUser(String firstname, String lastname, String mdp) throws SQLException { + String query = "INSERT INTO clients(nom, prenom, mdp, mail) VALUES (?, ?, ?, ?)"; + PreparedStatement stmt = theConnection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); + stmt.setString(1, firstname); + stmt.setString(2, lastname); + stmt.setString(3, mdp); + stmt.setString(4, firstname + lastname + "@gmail.com"); + int affectedRows = stmt.executeUpdate(); + + if (affectedRows > 0) { + ResultSet generatedKeys = stmt.getGeneratedKeys(); + if (generatedKeys.next()) { + long newId = generatedKeys.getLong(1); + return (int) newId; + } else { + return 0; + } + } else { + return 0; + } + } +} diff --git a/src/java/servlet/AuthentifServlet.java b/src/java/servlet/AuthentifServlet.java index d6fb621..8b6cdad 100644 --- a/src/java/servlet/AuthentifServlet.java +++ b/src/java/servlet/AuthentifServlet.java @@ -6,7 +6,10 @@ package servlet; import java.io.IOException; import java.io.PrintWriter; + +import form.AuthentifForm; import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; @@ -15,6 +18,7 @@ import jakarta.servlet.http.HttpServletResponse; * * @author francois */ +@WebServlet(name = "AuthentifServlet", urlPatterns = { "/AuthentifServlet" }) public class AuthentifServlet extends HttpServlet { /** @@ -70,7 +74,15 @@ public class AuthentifServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - processRequest(request, response); + //processRequest(request, response); + AuthentifForm authForm = new AuthentifForm(request); + String username = request.getParameter("username"); + String password = request.getParameter("password"); + if (authForm.ctrlAuthentif(username, null, password)) { + response.sendRedirect("http://localhost:8080/PersonnelTP/menuJSP"); + } else { + response.sendRedirect("http://localhost:8080/PersonnelTP/AuthentifServlet"); + } } /** diff --git a/web/WEB-INF/authentifJSP.jsp b/web/WEB-INF/authentifJSP.jsp index c8db0a1..70a4047 100644 --- a/web/WEB-INF/authentifJSP.jsp +++ b/web/WEB-INF/authentifJSP.jsp @@ -44,7 +44,11 @@
- +
diff --git a/web/WEB-INF/web.xml b/web/WEB-INF/web.xml index cd9fedf..1f83678 100644 --- a/web/WEB-INF/web.xml +++ b/web/WEB-INF/web.xml @@ -4,8 +4,4 @@ AuthentifServlet servlet.AuthentifServlet - - AuthentifServlet - /authentification -