Skip to content
Snippets Groups Projects
Commit 497f3057 authored by Julian Horner's avatar Julian Horner
Browse files

Add login page and controller

parent 544f669b
No related branches found
No related tags found
No related merge requests found
/*
* Copyright 2019 (C) by Julian Horner.
* All Rights Reserved.
*/
package de.rtuni.ms.apig;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Class that handles all kind of requests.
*
* @author Julian
*/
@Controller
public class LoginController {
//----------------------------------------------------------------------------------------------
/**
* Catch the request for the login page and returns the name of the corresponding template.
*
* @return The name of the template
*/
@RequestMapping("/login")
public String login() {
return "login";
}
//----------------------------------------------------------------------------------------------
}
\ No newline at end of file
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h1>Login</h1>
<form name="loginForm" action="securedPage/" method="POST">
<input id="access_token" type="hidden" name="access_token" value="">
<fieldset>
<legend>Please Login</legend>
<div th:if="${param.error}" class="alert alert-error">Invalid username and password.</div>
<div th:if="${param.logout}" class="alert alert-success">You have been logged out.</div>
<label for="username">Username</label>
<input type="text" id="username" name="username"/>
<label for="password">Password</label>
<input type="password" id="password" name="password"/>
<div>
<button type="button" class="btn" onclick="requestToken()">Log in</button>
</div>
</fieldset>
</form>
</body>
<script type="text/javascript">
//-------------------------------------------------------------------------------------------
function requestToken() {
const usernameElem = document.getElementById("username");
const passwordElem = document.getElementById("password");
const httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = () => { saveToken(httpRequest); };
httpRequest.open("POST", "/auth");
httpRequest.setRequestHeader("Cache-Control", "no-cache");
httpRequest.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
let params = {};
params.username = usernameElem.value;
params.password = passwordElem.value;
httpRequest.send(JSON.stringify(params));
}
//-------------------------------------------------------------------------------------------
function saveToken(httpRequest) {
try {
if (httpRequest.readyState === XMLHttpRequest.DONE) {
if (httpRequest.status === 204) {
let token = httpRequest.getResponseHeader("Authorization");
const tokenElem = document.getElementById("access_token");
tokenElem.value = token;
document.loginForm.submit();
//let tokenCookie = "authToken=" + token;
//tokenCookie += ";path=/";
//const date = new Date();
//date.setTime(date.getTime() + (1000 * 60 * 60 * 24));
//tokenCookie += ";expires=" + date.toGMTString();
//document.cookie = tokenCookie;
//forward();
} else if (httpRequest.status === 401) {
alert("Either username or password was wrong.");
} else {
alert("Something went wrong try again.");
}
}
} catch (e) { alert("Caught Exception: " + e.description); }
}
//-------------------------------------------------------------------------------------------
function forward() {
const cookies = document.cookie;
const cookieArray = cookies.split("; ")
let authToken;
cookieArray.forEach((elem) => {
if (elem.startsWith("authToken")) {
authToken = elem.slice("authToken=".length);
}
});
const httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = (httpRequest) => {
try {
if (httpRequest.readyState === XMLHttpRequest.DONE) {
console.log(httpRequest.status);
if (httpRequest.status === 200) {
console.log(httpRequest.responseText);
var newWindow = window.open(httpRequest);
} else {
alert("Something went wrong try again.");
}
}
} catch (e) { alert("Caught Exception: " + e.description); }
}
httpRequest.open("GET", "/securedPage", false);
httpRequest.setRequestHeader("Authorization", "Bearer " + authToken);
httpRequest.setRequestHeader("Cache-Control", "no-cache");
httpRequest.setRequestHeader("Content-Type", "text/html;");
httpRequest.send();
}
//-------------------------------------------------------------------------------------------
</script>
</html>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment