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

Implement dynamic forwarding after the login page

parent 4d2ec189
No related branches found
No related tags found
No related merge requests found
Pipeline #453 failed
...@@ -13,6 +13,7 @@ import org.springframework.security.config.annotation.web.configuration.WebSecur ...@@ -13,6 +13,7 @@ import org.springframework.security.config.annotation.web.configuration.WebSecur
import org.springframework.security.config.http.SessionCreationPolicy; import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import de.rtuni.ms.apig.filter.ForwardFilter;
import de.rtuni.ms.apig.filter.JWTAuthenticationFilter; import de.rtuni.ms.apig.filter.JWTAuthenticationFilter;
/** /**
...@@ -49,6 +50,8 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter { ...@@ -49,6 +50,8 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
// Use stateless sessions. // Use stateless sessions.
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
// Add filter to set passed cookie.
.addFilterBefore(new ForwardFilter(), UsernamePasswordAuthenticationFilter.class)
// Add filter to validate tokens with every request. // Add filter to validate tokens with every request.
.addFilterAfter(new JWTAuthenticationFilter(jwtConfiguration), .addFilterAfter(new JWTAuthenticationFilter(jwtConfiguration),
UsernamePasswordAuthenticationFilter.class) UsernamePasswordAuthenticationFilter.class)
......
/*
* Copyright 2019 (C) by Julian Horner.
* All Rights Reserved.
*/
package de.rtuni.ms.apig.filter;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.filter.OncePerRequestFilter;
/**
* Filter class which sets the last requested path to the response before login or auth.
*
* @author Julian
*
*/
public class ForwardFilter extends OncePerRequestFilter {
//---------------------------------------------------------------------------------------------
/** The login path. */
private static final String LOGIN_PATH = "/login";
/** The auth path. */
private static final String AUTH_PATH = "/auth";
//=============================================================================================
/**
* {@inheritDoc}
*/
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
boolean isLoginPath = LOGIN_PATH.equals(request.getServletPath());
boolean isAuthPath = AUTH_PATH.equals(request.getServletPath());
if (!(isLoginPath || isAuthPath)) {
Cookie forwardPageCookie = new Cookie("forwardPage", request.getServletPath());
// Adds the last requested path to the response before login or auth.
response.addCookie(forwardPageCookie);
}
filterChain.doFilter(request, response);
}
//---------------------------------------------------------------------------------------------
}
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
<body> <body>
<h1>Login</h1> <h1>Login</h1>
<form name="loginForm" action="securedPage/" method="POST"> <form name="loginForm" action="/" method="POST">
<input type="hidden" id="access_token" name="access_token" value=""> <input type="hidden" id="access_token" name="access_token" value="">
<fieldset> <fieldset>
...@@ -36,7 +36,7 @@ ...@@ -36,7 +36,7 @@
httpRequest.setRequestHeader("Cache-Control", "no-cache"); httpRequest.setRequestHeader("Cache-Control", "no-cache");
httpRequest.setRequestHeader("Content-Type", "application/json; charset=UTF-8"); httpRequest.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
let params = {}; const params = {};
params.username = usernameElem.value; params.username = usernameElem.value;
params.password = passwordElem.value; params.password = passwordElem.value;
...@@ -53,7 +53,16 @@ ...@@ -53,7 +53,16 @@
const tokenElem = document.getElementById("access_token"); const tokenElem = document.getElementById("access_token");
tokenElem.value = token; tokenElem.value = token;
const cookies = document.cookie;
let forwardPageCookie = "";
cookies.split("; ").forEach((elem) => {
if (elem.split("=")[0] === "forwardPage") {
forwardPageCookie = elem.split("=")[1];
}
});
document.loginForm.action = getCookie("forwardPage");
document.loginForm.submit(); document.loginForm.submit();
} else if (httpRequest.status === 401) { } else if (httpRequest.status === 401) {
alert("Authorization failed, either user or password was incorrect."); alert("Authorization failed, either user or password was incorrect.");
...@@ -65,5 +74,19 @@ ...@@ -65,5 +74,19 @@
} }
//------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------
function getCookie(name) {
const cookies = document.cookie;
let forwardPageCookie = "";
cookies.split("; ").forEach((elem) => {
if (elem.split("=")[0] === "name") {
forwardPageCookie = elem.split("=")[1];
}
});
return forwardPageCookie;
}
//-------------------------------------------------------------------------------------------
</script> </script>
</html> </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