Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package co.selim;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.UUID;
- import spark.Spark;
- public class Router {
- public static void main(String[] args) {
- Map<String, String> credentials = new HashMap<>();
- credentials.put("root", "passw0rd");
- List<String> tokens = new ArrayList<>();
- Spark.staticFileLocation("public");
- Spark.exception(Exception.class, (exception, request, response) -> {
- exception.printStackTrace();
- });
- Spark.before("/protected/*", (req, res) -> {
- if (!tokens.contains(req.cookie("token")))
- Spark.halt(401, "You can't access this page.");
- });
- Spark.post("/login", (req, res) -> {
- String username = req.queryParams("user");
- String password = req.queryParams("password");
- String storedPassword = credentials.get(username);
- if (storedPassword != null && password.equals(storedPassword)) {
- String token = UUID.randomUUID().toString();
- tokens.add(token);
- res.cookie("token", token, 3600);
- } else
- return "Username or password wrong.";
- return "";
- });
- Spark.get("/logout", (req, res) -> {
- String token = req.cookie("token");
- if (tokens.remove(token)) {
- res.removeCookie("token");
- return "Successfully logged out.";
- } else
- return "You were not logged in.";
- });
- Spark.get("/protected/controlPanel", (req, res) -> {
- return "<button>Launch nukes</button>";
- });
- }
- }
- res.cookie("token", token, 3600);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement