Add client authorization
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
package httpsServer.httpServer.src;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import common.common.src.html.HtmlManager;
|
||||
import common.common.src.logger.Logger;
|
||||
import httpsServer.httpServer.src.authorization.AuthorizedClients;
|
||||
import httpsServer.httpServer.src.authorization.Client;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.InetSocketAddress;
|
||||
@@ -10,13 +14,19 @@ import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) throws IOException {
|
||||
|
||||
final int port = 8043;
|
||||
final HtmlManager htmlManager = new HtmlManager();
|
||||
final AuthorizedClients authorizedClients = new AuthorizedClients();
|
||||
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress(port), 5);
|
||||
|
||||
System.out.println("Server started on port " + port);
|
||||
|
||||
server.createContext("/", exchange -> {
|
||||
if(isUnauthorizedVerb(exchange, "GET")){
|
||||
exchange.sendResponseHeaders(405, -1);
|
||||
return;
|
||||
}
|
||||
|
||||
String content = htmlManager.serveFile("./assets/pages/index.html");
|
||||
byte[] data = content.getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
@@ -26,7 +36,7 @@ public class Main {
|
||||
});
|
||||
|
||||
server.createContext("/login", exchange -> {
|
||||
if (!"POST".equalsIgnoreCase(exchange.getRequestMethod())) {
|
||||
if(isUnauthorizedVerb(exchange, "POST")){
|
||||
exchange.sendResponseHeaders(405, -1);
|
||||
return;
|
||||
}
|
||||
@@ -36,14 +46,26 @@ public class Main {
|
||||
.lines()
|
||||
.reduce("", (acc, line) -> acc + line + "\n");
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
Client client = mapper.readValue(body, Client.class);
|
||||
|
||||
System.out.println("Received POST data:\n" + body);
|
||||
|
||||
String response = "Data received!";
|
||||
String response = "Unauthorized";
|
||||
if(authorizedClients.isAuthorized(client)){
|
||||
response = "Authorized";
|
||||
}
|
||||
|
||||
exchange.sendResponseHeaders(200, response.getBytes().length);
|
||||
send(exchange, response.getBytes());
|
||||
});
|
||||
|
||||
server.start();
|
||||
Logger.displayInfo("Server started on port " + port);
|
||||
}
|
||||
|
||||
private static boolean isUnauthorizedVerb(HttpExchange exchange, String verb) throws IOException {
|
||||
return !verb.equalsIgnoreCase(exchange.getRequestMethod());
|
||||
}
|
||||
|
||||
private static void send(HttpExchange exchange, byte[] data) throws IOException {
|
||||
@@ -51,5 +73,4 @@ public class Main {
|
||||
os.write(data);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package httpsServer.httpServer.src.authorization;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class AuthorizedClients {
|
||||
|
||||
private final Set<Client> clients;
|
||||
|
||||
public AuthorizedClients() {
|
||||
clients = new HashSet<>();
|
||||
populateClients();
|
||||
}
|
||||
|
||||
private void populateClients() {
|
||||
registerClient("Aude Vaiselle", "password1");
|
||||
registerClient("Tony Truand", "password2");
|
||||
registerClient("Jean Porte", "password3");
|
||||
registerClient("Ruby Niole", "password4");
|
||||
registerClient("Nat Assion", "password5");
|
||||
registerClient("hepl", "hepl");
|
||||
}
|
||||
|
||||
private void registerClient(String username, String password) {
|
||||
Client client = new Client(username, password);
|
||||
clients.add(client);
|
||||
}
|
||||
|
||||
public boolean isAuthorized(Client client) {
|
||||
return clients.contains(client);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
package httpsServer.httpServer.src.authorization;
|
||||
|
||||
public record Client(String username, String password) {}
|
||||
Reference in New Issue
Block a user