Advertisement
Guest User

Untitled

a guest
Dec 20th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. package co.selim;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.UUID;
  8.  
  9. import spark.Spark;
  10.  
  11. public class Router {
  12. public static void main(String[] args) {
  13. Map<String, String> credentials = new HashMap<>();
  14. credentials.put("root", "passw0rd");
  15.  
  16. List<String> tokens = new ArrayList<>();
  17.  
  18. Spark.staticFileLocation("public");
  19.  
  20. Spark.exception(Exception.class, (exception, request, response) -> {
  21. exception.printStackTrace();
  22. });
  23.  
  24. Spark.before("/protected/*", (req, res) -> {
  25. if (!tokens.contains(req.cookie("token")))
  26. Spark.halt(401, "You can't access this page.");
  27. });
  28.  
  29. Spark.post("/login", (req, res) -> {
  30. String username = req.queryParams("user");
  31. String password = req.queryParams("password");
  32.  
  33. String storedPassword = credentials.get(username);
  34. if (storedPassword != null && password.equals(storedPassword)) {
  35. String token = UUID.randomUUID().toString();
  36. tokens.add(token);
  37. res.cookie("token", token, 3600);
  38. } else
  39. return "Username or password wrong.";
  40. return "";
  41. });
  42.  
  43. Spark.get("/logout", (req, res) -> {
  44. String token = req.cookie("token");
  45. if (tokens.remove(token)) {
  46. res.removeCookie("token");
  47. return "Successfully logged out.";
  48. } else
  49. return "You were not logged in.";
  50. });
  51.  
  52. Spark.get("/protected/controlPanel", (req, res) -> {
  53. return "<button>Launch nukes</button>";
  54. });
  55. }
  56. }
  57.  
  58. res.cookie("token", token, 3600);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement