Created
April 26, 2021 01:18
-
-
Save sandipchitale/0332454b513edab146cb448f4a846d8f to your computer and use it in GitHub Desktop.
Stale Session Managerment #springboot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<link rel="shortcut icon"type="image/x-icon" href="data:image/x-icon;,"> | |
<title>Reload</title> | |
<script> | |
(function() { | |
window.addEventListener("beforeunload", function (e) { | |
let token; | |
for(let i = 0; i < sessionStorage.length; i++) { | |
const key = sessionStorage.key(i); | |
if (key.startsWith('PREFIX.')) { | |
token = sessionStorage.getItem(key); | |
break; | |
} | |
} | |
if (token) { | |
// Add maybe | |
const tokenParts = token.split('.'); | |
if (tokenParts && tokenParts.length === 2) { | |
const localStorageToken = `MAYBE_STALE_PREFIX.${tokenParts[1]}`; | |
localStorage.setItem(localStorageToken, Date.now()); | |
} | |
} | |
}); | |
// Add new Token to session storage if not present | |
const now = Date.now(); | |
let token; | |
for(let i = 0; i < sessionStorage.length; i++) { | |
const key = sessionStorage.key(i); | |
if (key.startsWith('PREFIX.')) { | |
token = sessionStorage.getItem(key); | |
break; | |
} | |
} | |
if (!token) { | |
token = `PREFIX.${now}`; | |
console.log(`New token:${token}`); | |
sessionStorage.setItem(token, token); | |
} | |
// clear self | |
const tokenParts = token.split('.'); | |
if (tokenParts && tokenParts.length === 2) { | |
// We are not stale | |
let localStorageToken = `MAYBE_STALE_PREFIX.${tokenParts[1]}`; | |
localStorage.removeItem(localStorageToken); | |
} | |
// Scan | |
setInterval(function() { | |
const now = Date.now(); | |
const candidates = []; | |
for(let i = 0; i < localStorage.length; i++) { | |
const key = localStorage.key(i); | |
if (key.startsWith('MAYBE_STALE_PREFIX.')) { | |
const when = localStorage.getItem(key); | |
if (when && (now - when) > 30000) { | |
candidates.push(key); | |
} | |
} | |
} | |
candidates.forEach(key => { | |
localStorage.removeItem(key); | |
document.cookie = `${key.substring(6)}=${Date.now()}`; | |
}) | |
if (candidates.length > 0) { | |
console.log(candidates.length) | |
fetch('clean'); | |
} | |
}, 5000); | |
})(); | |
</script> | |
</head> | |
<body> | |
<h1>Token</h1> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.properties; | |
import javax.servlet.http.Cookie; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.web.bind.annotation.GetMapping; | |
import org.springframework.web.bind.annotation.ResponseBody; | |
@SpringBootApplication | |
public class PropertiesApplication { | |
@Controller | |
public static class HeaderSize { | |
@GetMapping("/") | |
public String headerSize() { | |
return "index.html"; | |
} | |
@GetMapping("/clean") | |
@ResponseBody | |
public String cleanup(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { | |
Cookie[] cookies = httpServletRequest.getCookies(); | |
if (cookies != null && cookies.length > 0) { | |
for (int i = 0; i < cookies.length; i++) { | |
Cookie cookie = cookies[i]; | |
if (cookie.getName().startsWith("STALE_PREFIX.")) { | |
Cookie cookieToDelete = new Cookie(cookie.getName(), null); | |
cookieToDelete.setMaxAge(0); | |
cookieToDelete.setPath("/"); | |
httpServletResponse.addCookie(cookieToDelete); | |
} | |
} | |
try { | |
Thread.sleep(1000); | |
} catch (InterruptedException e) { | |
} | |
} | |
return ""; | |
} | |
} | |
public static void main(String[] args) { | |
SpringApplication.run(PropertiesApplication.class, args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment