Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
api-gateway
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Julian Horner
api-gateway
Commits
497f3057
Commit
497f3057
authored
5 years ago
by
Julian Horner
Browse files
Options
Downloads
Patches
Plain Diff
Add login page and controller
parent
544f669b
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/de/rtuni/ms/apig/LoginController.java
+31
-0
31 additions, 0 deletions
src/main/java/de/rtuni/ms/apig/LoginController.java
src/main/resources/templates/login.html
+121
-0
121 additions, 0 deletions
src/main/resources/templates/login.html
with
152 additions
and
0 deletions
src/main/java/de/rtuni/ms/apig/LoginController.java
0 → 100644
+
31
−
0
View file @
497f3057
/*
* 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
This diff is collapsed.
Click to expand it.
src/main/resources/templates/login.html
0 → 100644
+
121
−
0
View file @
497f3057
<!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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment