Skip to content

Instantly share code, notes, and snippets.

@schatterjee4
Forked from itzg/app.js
Created July 17, 2017 08:59
Show Gist options
  • Save schatterjee4/e3fb6b60ac3d382e747bf3135d1421ef to your computer and use it in GitHub Desktop.
Save schatterjee4/e3fb6b60ac3d382e747bf3135d1421ef to your computer and use it in GitHub Desktop.
Coordinating session timeout from Spring MVC (embedded Tomcat with Spring Boot) to AngularJS REST invocations
angular.module('App', [])
.factory('sessionTimeoutInterceptor', function($log, $window) {
return {
response: function(response){
if (response.headers('x-login') === 'true') {
$log.debug('Intercepted login redirect', response);
$window.location.reload();
}
return response;
}
}
})
.config(function($httpProvider){
$httpProvider.interceptors.push('sessionTimeoutInterceptor');
})
;
@Controller
public class ViewController {
@RequestMapping("/")
String index() {
return "index";
}
@RequestMapping("/login")
ModelAndView loginPage(HttpServletRequest request, HttpServletResponse response, Model model) {
response.addHeader("x-login", "true");
CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class
.getName());
final ModelAndView modelAndView = new ModelAndView()
.addObject("csrf", csrf);
modelAndView.setViewName("login");
return modelAndView;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment