mirror of
https://github.com/kmitresse/Cards-Rush.git
synced 2026-05-14 09:05:23 +00:00
feat(DevWeb): Projet - Login page, Login Api
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
package uppa.project;
|
||||
|
||||
import java.util.Calendar;
|
||||
import uppa.project.dao.DAO;
|
||||
import uppa.project.dao.DAOException;
|
||||
import uppa.project.dao.jpa.Game_JPA_DAO_Factory;
|
||||
import uppa.project.pojo.Game;
|
||||
import uppa.project.pojo.Player;
|
||||
import uppa.project.pojo.User;
|
||||
|
||||
public class Main {
|
||||
@@ -17,34 +15,43 @@ public class Main {
|
||||
// DAO<Player> daoJpaPlayer = jpaDaoFactory.getDAOPlayer();
|
||||
|
||||
// Contenu de la BD au début
|
||||
User[] users = daoJpaUser.findAll();
|
||||
for (User u : users) {
|
||||
System.out.println(u.toString());
|
||||
// User[] users = daoJpaUser.findAll();
|
||||
// for (User u : users) {
|
||||
// System.out.println(u.toString());
|
||||
// }
|
||||
// System.out.println();
|
||||
|
||||
// // Ajout d'User :
|
||||
// Calendar cal1 = Calendar.getInstance();
|
||||
// cal1.set(1996, Calendar.FEBRUARY, 20);
|
||||
// User user1 = new User("Kevin", "Mitresse", cal1.getTime(), User.Gender.MALE);
|
||||
//
|
||||
// Calendar cal2 = Calendar.getInstance();
|
||||
// cal2.set(2002, Calendar.JUNE, 28);
|
||||
// User user2 = new User("Lucàs", "Vabre", cal2.getTime(), User.Gender.MALE);
|
||||
//
|
||||
// daoJpaUser.create(user1);
|
||||
// daoJpaUser.create(user2);
|
||||
|
||||
System.out.println("test récupération user");
|
||||
User[] users2 = daoJpaUser.findByField("username", "Kevin");
|
||||
for (User user : users2) {
|
||||
System.out.println(user.toString());
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
// Ajout d'User :
|
||||
Calendar cal1 = Calendar.getInstance();
|
||||
cal1.set(1996, Calendar.FEBRUARY, 20);
|
||||
User user1 = new User("Kevin", "Mitresse", cal1.getTime(), User.Gender.MALE);
|
||||
|
||||
Calendar cal2 = Calendar.getInstance();
|
||||
cal2.set(2002, Calendar.JUNE, 28);
|
||||
User user2 = new User("Lucàs", "Vabre", cal2.getTime(), User.Gender.MALE);
|
||||
|
||||
daoJpaUser.create(user1);
|
||||
daoJpaUser.create(user2);
|
||||
|
||||
// Contenu de la BD après ajout
|
||||
users = daoJpaUser.findAll();
|
||||
for (User u : users) {
|
||||
System.out.println(u.toString());
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
EntityManagerProvider.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("fin test");
|
||||
} catch (DAOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
// Contenu de la BD après ajout
|
||||
// users = daoJpaUser.findAll();
|
||||
// for (User u : users) {
|
||||
// System.out.println(u.toString());
|
||||
// }
|
||||
// System.out.println();
|
||||
//
|
||||
// EntityManagerProvider.close();
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,10 @@
|
||||
|
||||
package uppa.project.dao;
|
||||
|
||||
import jakarta.persistence.TypedQuery;
|
||||
import java.util.List;
|
||||
import uppa.project.pojo.User;
|
||||
|
||||
/**
|
||||
* DAO abstrait et générique pour tout type de données
|
||||
*
|
||||
@@ -52,4 +56,6 @@ public abstract class DAO<D> {
|
||||
* @throws DAOException en cas de problème
|
||||
*/
|
||||
public abstract void delete(D data) throws DAOException;
|
||||
|
||||
public abstract D[] findByField(String field, String value) throws DAOException;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import uppa.project.EntityManagerProvider;
|
||||
import uppa.project.dao.DAO;
|
||||
import uppa.project.dao.DAOException;
|
||||
import uppa.project.pojo.Game;
|
||||
import uppa.project.pojo.User;
|
||||
|
||||
public class DAO_JPA_Game extends DAO<Game> {
|
||||
|
||||
@@ -24,6 +25,14 @@ public class DAO_JPA_Game extends DAO<Game> {
|
||||
return result;
|
||||
}
|
||||
|
||||
public Game[] findByField(String field, String value) throws DAOException {
|
||||
TypedQuery<Game> query = entityManager.createQuery("SELECT u FROM Game u WHERE ?1=?2", Game.class);
|
||||
query.setParameter(1, field);
|
||||
query.setParameter(2, value);
|
||||
List<Game> results = query.getResultList();
|
||||
return results.toArray(new Game[0]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Game[] findAll() throws DAOException {
|
||||
TypedQuery<Game> query = entityManager.createQuery("SELECT g FROM Game g", Game.class);
|
||||
|
||||
@@ -8,6 +8,7 @@ import uppa.project.EntityManagerProvider;
|
||||
import uppa.project.dao.DAO;
|
||||
import uppa.project.dao.DAOException;
|
||||
import uppa.project.pojo.Player;
|
||||
import uppa.project.pojo.User;
|
||||
|
||||
public class DAO_JPA_Player extends DAO<Player> {
|
||||
|
||||
@@ -24,6 +25,14 @@ public class DAO_JPA_Player extends DAO<Player> {
|
||||
return result;
|
||||
}
|
||||
|
||||
public Player[] findByField(String field, String value) throws DAOException {
|
||||
TypedQuery<Player> query = entityManager.createQuery("SELECT p FROM Player p WHERE ?1=?2", Player.class);
|
||||
query.setParameter(1, field);
|
||||
query.setParameter(2, value);
|
||||
List<Player> results = query.getResultList();
|
||||
return results.toArray(new Player[0]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Player[] findAll() throws DAOException {
|
||||
TypedQuery<Player> query = entityManager.createQuery("SELECT p FROM Player p", Player.class);
|
||||
|
||||
@@ -24,6 +24,15 @@ public class DAO_JPA_User extends DAO<User> {
|
||||
return result;
|
||||
}
|
||||
|
||||
public User[] findByField(String field, String value) throws DAOException {
|
||||
String sqlQuery = String.format("SELECT u FROM User u WHERE u.%s = (:val)", field);
|
||||
|
||||
TypedQuery<User> query = entityManager.createQuery(sqlQuery, User.class);
|
||||
query.setParameter("val", value);
|
||||
List<User> results = query.getResultList();
|
||||
return results.toArray(new User[0]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public User[] findAll() throws DAOException {
|
||||
TypedQuery<User> query = entityManager.createQuery("SELECT u FROM User u", User.class);
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
package uppa.project.pojo;
|
||||
|
||||
public class Card {
|
||||
/**
|
||||
* The color of the card:
|
||||
* Heart -> Coeur
|
||||
* Clubs -> Trèfle
|
||||
* Spades -> Pique
|
||||
* Diamonds -> Carreau
|
||||
*/
|
||||
public enum Color{HEART, CLUBS, SPADES, DIAMONDS}
|
||||
|
||||
public enum Color{coeur, carreau, pique, trefle}
|
||||
|
||||
public enum Value{un, deux, trois, quatre, cinq, six, sept, huit, neuf, dix, valet, dame, roi}
|
||||
public enum Value{ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING}
|
||||
private final Color color;
|
||||
|
||||
private final Value value;
|
||||
|
||||
@@ -106,7 +106,7 @@ public class User implements Serializable {
|
||||
return (int) (diffDays / 365);
|
||||
}
|
||||
|
||||
private String hashPassword(String password) {
|
||||
public static String hashPassword(String password) {
|
||||
try {
|
||||
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||
|
||||
@@ -118,7 +118,6 @@ public class User implements Serializable {
|
||||
if (hex.length() == 1) hexString.append('0');
|
||||
hexString.append(hex);
|
||||
}
|
||||
|
||||
return hexString.toString();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
package uppa.project.servlet;
|
||||
|
||||
import java.io.*;
|
||||
import jakarta.servlet.http.*;
|
||||
import jakarta.servlet.annotation.*;
|
||||
|
||||
@WebServlet(name = "helloServlet", value = "/hello-servlet")
|
||||
public class HelloServlet extends HttpServlet {
|
||||
private String message;
|
||||
|
||||
public void init() {
|
||||
message = "Hello World!";
|
||||
}
|
||||
|
||||
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||
response.setContentType("text/html");
|
||||
|
||||
// Hello
|
||||
PrintWriter out = response.getWriter();
|
||||
out.println("<html><body>");
|
||||
out.println("<h1>" + message + "</h1>");
|
||||
out.println("</body></html>");
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package uppa.project.servlet;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import jakarta.servlet.annotation.WebServlet;
|
||||
import jakarta.servlet.http.HttpServlet;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import uppa.project.dao.DAO;
|
||||
import uppa.project.dao.DAOException;
|
||||
import uppa.project.dao.jpa.Game_JPA_DAO_Factory;
|
||||
import uppa.project.servlet.json.ErrorApi;
|
||||
import uppa.project.pojo.User;
|
||||
import uppa.project.servlet.utils.RequestUtils;
|
||||
|
||||
@WebServlet(name = "loginApiServlet", value = "/api/login")
|
||||
public class LoginServlet extends HttpServlet {
|
||||
|
||||
private final Gson gson = new Gson();
|
||||
|
||||
public void init() {
|
||||
}
|
||||
|
||||
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||
response.setContentType("application/json");
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
PrintWriter out = response.getWriter();
|
||||
|
||||
// Convert the string to a JSON object
|
||||
JsonObject jsonBody;
|
||||
try {
|
||||
String requestBody = RequestUtils.getRequestBody(request);
|
||||
jsonBody = JsonParser.parseString(requestBody).getAsJsonObject();
|
||||
} catch (Exception e) {
|
||||
int STATUS = 400;
|
||||
|
||||
ErrorApi error = new ErrorApi(STATUS, "Bad Request", "Invalid JSON");
|
||||
response.setStatus(STATUS);
|
||||
out.println(error.toJson());
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if the username and password are present
|
||||
JsonElement username = jsonBody.get("username");
|
||||
JsonElement password = jsonBody.get("password");
|
||||
if (username == null || password == null) {
|
||||
int STATUS = 400;
|
||||
|
||||
ErrorApi error = new ErrorApi(STATUS, "Bad Request", "Username and password are required");
|
||||
response.setStatus(STATUS);
|
||||
out.println(error.toJson());
|
||||
return;
|
||||
}
|
||||
|
||||
// Get User from database matching the username and password
|
||||
User user = LoginServlet.loginUser(username.getAsString(), password.getAsString());
|
||||
if (user == null) {
|
||||
int STATUS = 401;
|
||||
ErrorApi error = new ErrorApi(STATUS, "Unauthorized", "Invalid username or password");
|
||||
|
||||
response.setStatus(STATUS);
|
||||
out.println(error.toJson());
|
||||
return;
|
||||
}
|
||||
|
||||
// Return the user as JSON
|
||||
String json = gson.toJson(user);
|
||||
out.println(json);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
private static User loginUser(String username, String password) {
|
||||
Game_JPA_DAO_Factory factory = new Game_JPA_DAO_Factory();
|
||||
try {
|
||||
DAO<User> userDao = factory.getDAOUser();
|
||||
User[] users = userDao.findByField("username", username);
|
||||
|
||||
for (User user : users) {
|
||||
if (user.verifyPassword(password)) return user;
|
||||
}
|
||||
} catch (DAOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package uppa.project.servlet.json;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
public class ErrorApi {
|
||||
private final int status;
|
||||
private final String error;
|
||||
private final String message;
|
||||
|
||||
public ErrorApi(int status, String error, String message) {
|
||||
this.status = status;
|
||||
this.error = error;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public int getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public String getError() {
|
||||
return error;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public String toJson() {
|
||||
Gson gson = new Gson();
|
||||
return gson.toJson(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package uppa.project.servlet.utils;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
|
||||
public class RequestUtils {
|
||||
public static String getRequestBody(HttpServletRequest request) throws IOException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
BufferedReader reader = request.getReader();
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
sb.append(line);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user