mise en place d'un token d'authentification lors de la connexion
This commit is contained in:
parent
d144ff9fe9
commit
7395063bf2
BIN
Back-end/keystore.jceks
Normal file
BIN
Back-end/keystore.jceks
Normal file
Binary file not shown.
BIN
Back-end/keystore.jceks.old
Normal file
BIN
Back-end/keystore.jceks.old
Normal file
Binary file not shown.
@ -87,6 +87,12 @@
|
|||||||
<artifactId>bcrypt</artifactId>
|
<artifactId>bcrypt</artifactId>
|
||||||
<version>0.9.0</version>
|
<version>0.9.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.vertx</groupId>
|
||||||
|
<artifactId>vertx-auth-jwt</artifactId>
|
||||||
|
<version>4.5.13</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|||||||
@ -13,15 +13,31 @@ import io.vertx.core.Promise;
|
|||||||
import io.vertx.ext.web.Router;
|
import io.vertx.ext.web.Router;
|
||||||
import io.vertx.ext.web.RoutingContext;
|
import io.vertx.ext.web.RoutingContext;
|
||||||
import at.favre.lib.crypto.bcrypt.BCrypt;
|
import at.favre.lib.crypto.bcrypt.BCrypt;
|
||||||
|
import io.vertx.ext.auth.jwt.JWTAuth;
|
||||||
|
import io.vertx.ext.auth.jwt.JWTAuthOptions;
|
||||||
|
import io.vertx.ext.auth.KeyStoreOptions;
|
||||||
|
import io.vertx.ext.auth.authentication.TokenCredentials;
|
||||||
|
import io.vertx.ext.web.handler.JWTAuthHandler;
|
||||||
|
|
||||||
|
|
||||||
public class MainVerticle extends AbstractVerticle {
|
public class MainVerticle extends AbstractVerticle {
|
||||||
private DatabaseService databaseService;
|
private DatabaseService databaseService;
|
||||||
private Router router; // Déclaration du router en variable de classe
|
private Router router; // Déclaration du router en variable de classe
|
||||||
|
private JWTAuth jwtAuth; // Déclaration au niveau de la classe
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(Promise<Void> startPromise) throws Exception {
|
public void start(Promise<Void> startPromise) throws Exception {
|
||||||
databaseService = new DatabaseService(vertx);
|
databaseService = new DatabaseService(vertx);
|
||||||
|
|
||||||
|
|
||||||
|
this.jwtAuth = JWTAuth.create(vertx, new JWTAuthOptions()
|
||||||
|
.setKeyStore(new KeyStoreOptions()
|
||||||
|
.setPath("keystore.jceks")
|
||||||
|
.setPassword("secret")));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Initialisation du router
|
// Initialisation du router
|
||||||
router = Router.router(vertx);
|
router = Router.router(vertx);
|
||||||
|
|
||||||
@ -41,6 +57,8 @@ public class MainVerticle extends AbstractVerticle {
|
|||||||
router.get("/objet").handler(this::getParticularObject);
|
router.get("/objet").handler(this::getParticularObject);
|
||||||
router.post("/signup").handler(this::handleSignup); // Route pour l'inscription
|
router.post("/signup").handler(this::handleSignup); // Route pour l'inscription
|
||||||
router.post("/login").handler(this::handleLogin); // Route pour la connexion
|
router.post("/login").handler(this::handleLogin); // Route pour la connexion
|
||||||
|
// Protéger toutes les routes commençant par "/api/"
|
||||||
|
router.route("/api/*").handler(JWTAuthHandler.create(jwtAuth));
|
||||||
|
|
||||||
// Création du serveur HTTP
|
// Création du serveur HTTP
|
||||||
vertx.createHttpServer()
|
vertx.createHttpServer()
|
||||||
@ -54,6 +72,8 @@ public class MainVerticle extends AbstractVerticle {
|
|||||||
throwable.printStackTrace();
|
throwable.printStackTrace();
|
||||||
startPromise.fail(throwable);
|
startPromise.fail(throwable);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Récupération des objets
|
// Récupération des objets
|
||||||
@ -173,6 +193,7 @@ public class MainVerticle extends AbstractVerticle {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Méthode de ocnnexion
|
||||||
private void handleLogin(RoutingContext context) {
|
private void handleLogin(RoutingContext context) {
|
||||||
JsonObject body = context.body().asJsonObject();
|
JsonObject body = context.body().asJsonObject();
|
||||||
|
|
||||||
@ -213,9 +234,15 @@ public class MainVerticle extends AbstractVerticle {
|
|||||||
BCrypt.Result verification = BCrypt.verifyer().verify(password.toCharArray(), storedHashedPassword);
|
BCrypt.Result verification = BCrypt.verifyer().verify(password.toCharArray(), storedHashedPassword);
|
||||||
|
|
||||||
if (verification.verified) {
|
if (verification.verified) {
|
||||||
context.response()
|
System.out.println("Connexion réussi");
|
||||||
|
//Génération du token JWT
|
||||||
|
JsonObject claims = new JsonObject().put("sub",email).put("role", "user");
|
||||||
|
String token = jwtAuth.generateToken(claims);
|
||||||
|
|
||||||
|
context.response()
|
||||||
.setStatusCode(200)
|
.setStatusCode(200)
|
||||||
.end(new JsonObject().put("message", "Connexion réussie").encode());
|
.end(new JsonObject().put("token", token).encode());
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
context.response()
|
context.response()
|
||||||
.setStatusCode(401)
|
.setStatusCode(401)
|
||||||
|
|||||||
@ -35,10 +35,9 @@ function Login() {
|
|||||||
throw new Error(data.error || "Erreur lors de la connexion");
|
throw new Error(data.error || "Erreur lors de la connexion");
|
||||||
}
|
}
|
||||||
|
|
||||||
alert("Connexion réussie !");
|
localStorage.setItem("token", data.token);
|
||||||
|
navigate("/");
|
||||||
|
|
||||||
// Redirection vers la page d'accueil après une connexion réussie
|
|
||||||
navigate("/home"); // Remplace "/home" par l'URL de ta page d'accueil
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
alert(error.message);
|
alert(error.message);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user