-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45ddf3a
commit 353ba78
Showing
5 changed files
with
216 additions
and
3 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
...ava/com/spiderdiplome/controllers/accountservlets/candidatpotentiel/DashboardServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.spiderdiplome.controllers.accountservlets.candidatpotentiel; | ||
|
||
public class DashboardServlet { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
...main/java/com/spiderdiplome/controllers/accountservlets/superadmin/LockScreenServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package com.spiderdiplome.controllers.accountservlets.superadmin; | ||
|
||
import com.spiderdiplome.models.Utilisateur; | ||
import com.spiderdiplome.repository.implement.UtilisateurDAOImpl; | ||
import com.spiderdiplome.security.authentification.AuthenticationService; | ||
import com.spiderdiplome.security.tokenjjwt.TokenProvider; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.annotation.WebServlet; | ||
import javax.servlet.http.*; | ||
import javax.ws.rs.core.HttpHeaders; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
|
||
@WebServlet(description = "Lock Screen Servlet", urlPatterns = {"/verrouiller"}) | ||
public class LockScreenServlet extends HttpServlet { | ||
|
||
private UtilisateurDAOImpl utilisateurDAO; | ||
private AuthenticationService authService; | ||
|
||
@Override | ||
public void init() throws ServletException { | ||
super.init(); | ||
// Initialisation du servlet | ||
utilisateurDAO = new UtilisateurDAOImpl(); | ||
authService = new AuthenticationService(); | ||
} | ||
|
||
@Override | ||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
HttpSession session = req.getSession(false); | ||
if (session != null && session.getAttribute("user") != null) { | ||
Utilisateur utilisateur = (Utilisateur) session.getAttribute("user"); | ||
req.setAttribute("usermatricule", utilisateur.getMatricule()); | ||
session.invalidate(); | ||
this.getServletContext().getRequestDispatcher("/WEB-INF/views/v1/data/secure/superadmin-area/lock_screen.jsp").forward(req, resp); | ||
} else { | ||
this.getServletContext().getRequestDispatcher("/WEB-INF/views/login.jsp").forward(req, resp); | ||
} | ||
} | ||
|
||
@Override | ||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
String matricule = req.getParameter("matricule"); | ||
String password = req.getParameter("password"); | ||
try { | ||
if (authService.authenticate(matricule, password)) { | ||
handleSuccessfulAuthentication(req, resp, matricule); | ||
} else { | ||
forwardWithError(req, resp, "Le nom d'utilisateur ou le mot de passe que vous avez entré est incorrect. Veuillez réessayer."); | ||
} | ||
} catch (Exception e) { | ||
forwardWithError(req, resp, "Certaines données que vous avez entrées ne sont pas valides. Veuillez vérifier et réessayer."); | ||
} | ||
} | ||
|
||
private boolean isInvalid(String matricule, String password) { | ||
return matricule == null || matricule.isEmpty() || password == null || password.isEmpty(); | ||
} | ||
|
||
private boolean isUserAlreadyLoggedIn(HttpServletRequest req) { | ||
HttpSession session = req.getSession(false); | ||
return session != null && session.getAttribute("user") != null; | ||
} | ||
|
||
private void handleSuccessfulAuthentication(HttpServletRequest req, HttpServletResponse resp, String matricule) throws IOException { | ||
Utilisateur utilisateur = utilisateurDAO.findByMatricule(matricule); | ||
HttpSession session = req.getSession(true); | ||
session.setAttribute("user", utilisateur); | ||
|
||
TokenProvider tokenProvider = (TokenProvider) getServletContext().getAttribute("tokenProvider"); | ||
String token = tokenProvider.createToken(matricule); | ||
session.setAttribute("token", token); | ||
|
||
resp.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token); | ||
|
||
Cookie userCookie = new Cookie("user_spiderdiplome", matricule); | ||
userCookie.setMaxAge(24 * 60 * 60); | ||
resp.addCookie(userCookie); | ||
|
||
String redirectUrl = getRedirectUrl(utilisateur, token); | ||
sendSuccessResponse(resp, redirectUrl); | ||
} | ||
|
||
private String getRedirectUrl(Utilisateur utilisateur, String token) { | ||
switch (utilisateur.getRole()) { | ||
case "superadmin": | ||
return "tableau-de-bord?st=" + token; | ||
case "user": | ||
return "userPage.jsp"; | ||
default: | ||
return "otherPage.jsp"; | ||
} | ||
} | ||
|
||
private void sendSuccessResponse(HttpServletResponse resp, String redirectUrl) throws IOException { | ||
resp.setContentType("text/html"); | ||
PrintWriter out = resp.getWriter(); | ||
out.println("<div class=\"alert alert-success\">\n" + | ||
"\t\t\t\t\t\t\t<h3><strong>Authentification Reussie!</strong> Connexion en cours.......</h3>\n" + | ||
"\t\t\t\t\t\t</div>"); | ||
out.println("<script>"); | ||
out.println("setTimeout(function(){"); | ||
out.println(" window.location.href = '" + redirectUrl + "';"); | ||
out.println("}, 3000);"); // Redirection après 3 secondes | ||
out.println("</script>"); | ||
} | ||
|
||
private void forwardWithError(HttpServletRequest req, HttpServletResponse resp, String errorMessage) throws ServletException, IOException { | ||
req.setAttribute("errorMessage", "<div class=\"alert alert-danger\">\n" + | ||
" <strong>Erreur d'authentification!</strong> " + errorMessage + "\n" + | ||
"</div>"); | ||
this.getServletContext().getRequestDispatcher("/WEB-INF/views/login.jsp").forward(req, resp); | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
super.destroy(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/main/webapp/WEB-INF/views/v1/data/secure/superadmin-area/lock_screen.jsp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> | ||
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Verrouillage</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<meta name="description" content=""> | ||
<meta name="author" content=""> | ||
|
||
<!-- Bootstrap core CSS --> | ||
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet"> | ||
|
||
<!-- Font Awesome --> | ||
<link href="css/font-awesome.min.css" rel="stylesheet"> | ||
|
||
<!-- Perfect --> | ||
<link href="css/app.min.css" rel="stylesheet"> | ||
|
||
</head> | ||
|
||
<body style="background-color:#3a3a3a;"> | ||
|
||
<!--Modal--> | ||
<div class="modal fade lock-screen-wrapper" id="lockScreen"> | ||
<div class="modal-dialog"> | ||
<div class="modal-content"> | ||
<div class="modal-body"> | ||
<div class="lock-screen-img"> | ||
<img src="img/user.jpg" alt=""> | ||
</div> | ||
|
||
<div class="text-center m-top-sm"> | ||
<div class="h4 text-white">${usermatricule}</div> | ||
|
||
<div class="input-group m-top-sm"> | ||
<form class="form-login" method="post" action="verrouiller?matricule=${usermatricule}"> | ||
<input type="password" required name="password" class="form-control text-sm" placeholder="Entrer votre mot de passe"> | ||
<span class="input-group-btn"> | ||
<button><i class="fa fa-arrow-right"></i></button> | ||
</span> | ||
</form> | ||
</div> | ||
</div> | ||
</div> | ||
</div><!-- /.modal-content --> | ||
</div><!-- /.modal-dialog --> | ||
</div><!-- /.modal --> | ||
|
||
<!-- Le javascript | ||
================================================== --> | ||
<!-- Placed at the end of the document so the pages load faster --> | ||
|
||
<!-- Jquery --> | ||
<script src="js/jquery-1.10.2.min.js"></script> | ||
|
||
<!-- Bootstrap --> | ||
<script src="bootstrap/js/bootstrap.min.js"></script> | ||
|
||
<!-- Modernizr --> | ||
<script src='js/modernizr.min.js'></script> | ||
|
||
<!-- Pace --> | ||
<script src='js/pace.min.js'></script> | ||
|
||
<!-- Popup Overlay --> | ||
<script src='js/jquery.popupoverlay.min.js'></script> | ||
|
||
<!-- Slimscroll --> | ||
<script src='js/jquery.slimscroll.min.js'></script> | ||
|
||
<!-- Cookie --> | ||
<script src='js/jquery.cookie.min.js'></script> | ||
|
||
<!-- Perfect --> | ||
<script src="js/app/app.js"></script> | ||
|
||
<script> | ||
$(function () { | ||
$('#lockScreen').modal({ | ||
show: true, | ||
backdrop: 'static' | ||
}) | ||
}); | ||
</script> | ||
</body> | ||
</html> |