22
33import lombok .extern .apachecommons .CommonsLog ;
44import org .springframework .beans .factory .annotation .Autowired ;
5+ import org .springframework .http .HttpHeaders ;
56import org .springframework .http .MediaType ;
7+ import org .springframework .http .ResponseCookie ;
8+ import org .springframework .http .ResponseEntity ;
69import org .springframework .security .access .prepost .PreAuthorize ;
710import org .springframework .stereotype .Controller ;
811import org .springframework .web .bind .annotation .GetMapping ;
912import org .springframework .web .bind .annotation .ResponseBody ;
10- import org .springframework .web .servlet .ModelAndView ;
1113import ubc .pavlab .rdp .services .UserService ;
1214
13- import javax .servlet .http .Cookie ;
14- import javax .servlet .http .HttpServletRequest ;
15- import javax .servlet .http .HttpServletResponse ;
16- import java .io .IOException ;
15+ import javax .servlet .http .HttpSession ;
16+ import java .time .Duration ;
17+ import java .time .Instant ;
1718
1819@ Controller
1920@ CommonsLog
@@ -29,16 +30,34 @@ public String index() {
2930 }
3031
3132 @ GetMapping (value = { "/maintenance" })
32- public ModelAndView maintenance () {
33- return new ModelAndView ( "error/maintenance" ) ;
33+ public String maintenance () {
34+ return "error/maintenance" ;
3435 }
3536
3637 @ PreAuthorize ("isAuthenticated()" )
3738 @ GetMapping (value = "/gettimeout" , produces = MediaType .TEXT_PLAIN_VALUE )
3839 @ ResponseBody
39- public String getTimeout ( HttpServletRequest servletRequest , HttpServletResponse servletResponse ) {
40- addTimeoutCookies ( servletRequest , servletResponse );
41- return "Session timeout refreshed." ;
40+ public ResponseEntity <String > getTimeout ( HttpSession httpSession ) {
41+ // Only set timeout cookie if the user is authenticated.
42+ Instant currTime = Instant .now ();
43+ Duration timeoutInSeconds = Duration .ofSeconds ( httpSession .getMaxInactiveInterval () ).minusSeconds ( 60 ); // Subtracting by 60s to give an extra minute client-side.
44+ Instant expiryTime = currTime .plus ( timeoutInSeconds );
45+
46+ // Get cookie for server current time.
47+ ResponseCookie serverTimeCookie = ResponseCookie .from ( "serverTime" , Long .toString ( currTime .toEpochMilli () ) )
48+ .path ( "/" )
49+ .build ();
50+
51+ // Get cookie for expiration time (consistent with serverTime cookie).
52+ ResponseCookie sessionExpiryCookie = ResponseCookie .from ( "sessionExpiry" , Long .toString ( expiryTime .toEpochMilli () ) )
53+ .path ( "/" )
54+ .build ();
55+
56+ return ResponseEntity .ok ()
57+ .header ( HttpHeaders .SET_COOKIE , serverTimeCookie .toString () )
58+ .header ( HttpHeaders .SET_COOKIE , sessionExpiryCookie .toString () )
59+ .contentType ( MediaType .TEXT_PLAIN )
60+ .body ( "Session timeout refreshed." );
4261 }
4362
4463 @ GetMapping (value = "/terms-of-service" )
@@ -50,21 +69,4 @@ public String termsOfService() {
5069 public String privacyPolicy () {
5170 return "privacy-policy" ;
5271 }
53-
54- private void addTimeoutCookies ( HttpServletRequest servletRequest , HttpServletResponse servletResponse ) {
55- // Only set timeout cookie if the user is authenticated.
56- long currTime = System .currentTimeMillis ();
57- int TIMEOUT_IN_SECONDS = servletRequest .getSession ().getMaxInactiveInterval () - 60 ; // Subtracting by 60s to give an extra minute client-side.
58- long expiryTime = currTime + TIMEOUT_IN_SECONDS * 1000 ;
59-
60- // Get cookie for server current time.
61- Cookie serverTimeCookie = new Cookie ( "serverTime" , "" + currTime );
62- serverTimeCookie .setPath ( "/" );
63- servletResponse .addCookie ( serverTimeCookie );
64-
65- // Get cookie for expiration time (consistent with serverTime cookie).
66- Cookie expiryCookie = new Cookie ( "sessionExpiry" , "" + expiryTime );
67- expiryCookie .setPath ( "/" );
68- servletResponse .addCookie ( expiryCookie );
69- }
7072}
0 commit comments