From 3588e67afff14d33e3afae9b64046505979a5a52 Mon Sep 17 00:00:00 2001 From: B3rciq Date: Wed, 19 Nov 2025 21:10:56 +0100 Subject: [PATCH] =?UTF-8?q?Pasek=20si=C5=82y=20has=C5=82a=20oraz=20podpowi?= =?UTF-8?q?edzi=20do=20stworzenia=20silnego=20has=C5=82a?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../_11a/passmetric/PasswordController.java | 68 +++++++-- src/main/resources/templates/password.html | 137 ++++++++---------- 2 files changed, 117 insertions(+), 88 deletions(-) diff --git a/src/main/java/iz/_11a/passmetric/PasswordController.java b/src/main/java/iz/_11a/passmetric/PasswordController.java index 87f8974..da64119 100644 --- a/src/main/java/iz/_11a/passmetric/PasswordController.java +++ b/src/main/java/iz/_11a/passmetric/PasswordController.java @@ -3,7 +3,7 @@ package iz._11a.passmetric; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; -import java.util.Map; +import java.util.*; @Controller public class PasswordController { @@ -15,19 +15,38 @@ public class PasswordController { @PostMapping(path = "/api/password/strength") @ResponseBody - public Map checkPasswordLive(@RequestParam("password") String password) { - return Map.of("message", analyze(password)); + public Map checkPasswordLive(@RequestParam("password") String password) { + + Map response = new HashMap<>(); + + int score = calculateScore(password); + String strengthText = strengthText(score); + List tips = generateTips(password); + + response.put("strengthText", strengthText); + response.put("progress", score * 20); // pasek postępu 0–100% + response.put("tips", tips); + + return response; } - private String analyze(String password) { + // ------------------------------------------ + // Ocena siły hasła + // ------------------------------------------ + private int calculateScore(String password) { int score = 0; - if (password != null) { - if (password.matches(".*[a-z].*")) score++; - if (password.matches(".*[A-Z].*")) score++; - if (password.matches(".*[0-9].*")) score++; - if (password.matches(".*[@$!%*?&#].*")) score++; - if (password.length() >= 8) score++; - } + if (password == null || password.isEmpty()) return 0; + + if (password.matches(".*[a-z].*")) score++; + if (password.matches(".*[A-Z].*")) score++; + if (password.matches(".*[0-9].*")) score++; + if (password.matches(".*[@$!%*?&#].*")) score++; + if (password.length() >= 12) score++; + + return score; + } + + private String strengthText(int score) { return switch (score) { case 5 -> "Bardzo silne hasło"; case 4 -> "Silne hasło"; @@ -36,4 +55,31 @@ public class PasswordController { default -> "Bardzo słabe hasło"; }; } + + // ------------------------------------------ + // Stopniowe podpowiedzi + // ------------------------------------------ + private List generateTips(String password) { + List tips = new ArrayList<>(); + + if (password == null || password.isEmpty()) { + tips.add("Wpisz hasło, aby rozpocząć analizę."); + return tips; + } + + + if (!password.matches(".*[A-Z].*")) + tips.add("Dodaj co najmniej jedną dużą litere."); + if (!password.matches(".*[0-9].*")) + tips.add("Dodaj co najmniej jedną cyfre."); + if (!password.matches(".*[@$!%*?&#].*")) + tips.add("Dodaj co najmniej jeden znnak specjalny (np. ! @ # $)."); + if (password.length() < 12) + tips.add("Wydłuż hasło do co najmniej 12 znaków."); + + if (tips.isEmpty()) + tips.add("Świetnie! Twoje hasło jest bardzo silne."); + + return tips; + } } diff --git a/src/main/resources/templates/password.html b/src/main/resources/templates/password.html index f1e99b9..49acbb8 100644 --- a/src/main/resources/templates/password.html +++ b/src/main/resources/templates/password.html @@ -6,28 +6,30 @@ @@ -38,80 +40,36 @@ - -
-
-
-
-
-
+ +
+
+

-

+ + +