jQuery Ajax Timeouts with Spring Security

Here’s how we handled Ajax timeouts with Spring Security.

In your login controller, set response status to 401.

	@RequestMapping(value = "login", method = RequestMethod.GET)
	public void login(HttpServletResponse response) {
		...

		response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
	}

In your Ajax error callback, check for the 401 code and redirect to a page that is not the login page. If we redirect to the login page, Spring Security might go to Ajax request right after login. You can redirect to a secure page to effectively get the login page.

	$.post(url, $('#id'))
	.success(function(result) {
		...
	})
	.error(function(xhr) {
		if (xhr.status == 401) {
			window.location = 'home';
		} else {
			...
		}
	});

No Comments


Leave a Reply

Your email address will not be published. Required fields are marked *