0% found this document useful (0 votes)
7 views3 pages

Secure REST Authentication System

Uploaded by

bitoriessoham
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Secure REST Authentication System

Uploaded by

bitoriessoham
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Projet REST JAX-RS - Système d'authentification

Ce projet consiste à implémenter un système d'authentification sécurisé pour une application de

commerce électronique via un service web REST utilisant JAX-RS. Le système repose sur

l'utilisation de jetons d'authentification pour sécuriser les accès.

1. Classe [Link]

// Singleton [Link]
import [Link].*;

public class AuthentificationService {


private static AuthentificationService instance = null;
private static Map<String, String> comptes = new HashMap<>();
private static List<String> jetons = new ArrayList<>();

private AuthentificationService() {
// Comptes fictifs
[Link]("admin", "admin123");
[Link]("user", "user123");
}

public static AuthentificationService getInstance() {


if (instance == null) {
instance = new AuthentificationService();
}
return instance;
}

public String authentifier(String login, String motDePasse) {


if ([Link](login) && [Link](login).equals(motDePasse)) {
String jeton = [Link]().toString();
[Link](jeton);
return jeton;
}
return null;
}

public boolean isValide(String jeton) {


return [Link](jeton);
}
}

2. Classe [Link]

// Service REST [Link]


import [Link].*;
import [Link].*;
import [Link];

@Path("/auth")
public class AuthentificationResource {

private AuthentificationService service = [Link]();

@POST
@Produces(MediaType.TEXT_PLAIN)
public Response authentifier(@FormParam("login") String login, @FormParam("mdp")
String mdp) {
String jeton = [Link](login, mdp);
if (jeton != null) {
// Ici on devrait publier sur un Topic JMS (omission volontaire)
return [Link](jeton).build();
} else {
return [Link]([Link]).entity("Login ou mot de
passe incorrect").build();
}
}

@GET
@Produces(MediaType.TEXT_PLAIN)
public String verifierJeton(@HeaderParam("Authorization") String jeton) {
if ([Link](jeton)) {
return "Jeton valide - Date: " + new Date().toString();
} else {
throw new WebApplicationException("Jeton invalide",
[Link]);
}
}
}

3. Exemple de client Jersey

// Exemple de client Java (Jersey)


import [Link].*;
import [Link].*;

public class ClientTest {


public static void main(String[] args) {
Client client = [Link]();
WebTarget target = [Link]("[Link]

// Requête POST pour s'authentifier


Form form = new Form();
[Link]("login", "admin");
[Link]("mdp", "admin123");

Response response = [Link]().post([Link](form));


String jeton = [Link]([Link]);
[Link]("Jeton reçu: " + jeton);
// Requête GET avec le jeton
Response check = [Link]()
.header("Authorization", jeton)
.get();

[Link]("Réponse GET: " + [Link]([Link]));


}
}

You might also like