2
2
3
3
import lombok .extern .apachecommons .CommonsLog ;
4
4
import org .springframework .beans .factory .annotation .Autowired ;
5
+ import org .springframework .http .HttpHeaders ;
5
6
import org .springframework .http .MediaType ;
7
+ import org .springframework .http .ResponseCookie ;
8
+ import org .springframework .http .ResponseEntity ;
6
9
import org .springframework .security .access .prepost .PreAuthorize ;
7
10
import org .springframework .stereotype .Controller ;
8
11
import org .springframework .web .bind .annotation .GetMapping ;
9
12
import org .springframework .web .bind .annotation .ResponseBody ;
10
- import org .springframework .web .servlet .ModelAndView ;
11
13
import ubc .pavlab .rdp .services .UserService ;
12
14
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 ;
17
18
18
19
@ Controller
19
20
@ CommonsLog
@@ -29,16 +30,34 @@ public String index() {
29
30
}
30
31
31
32
@ GetMapping (value = { "/maintenance" })
32
- public ModelAndView maintenance () {
33
- return new ModelAndView ( "error/maintenance" ) ;
33
+ public String maintenance () {
34
+ return "error/maintenance" ;
34
35
}
35
36
36
37
@ PreAuthorize ("isAuthenticated()" )
37
38
@ GetMapping (value = "/gettimeout" , produces = MediaType .TEXT_PLAIN_VALUE )
38
39
@ 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." );
42
61
}
43
62
44
63
@ GetMapping (value = "/terms-of-service" )
@@ -50,21 +69,4 @@ public String termsOfService() {
50
69
public String privacyPolicy () {
51
70
return "privacy-policy" ;
52
71
}
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
- }
70
72
}
0 commit comments