HSal
HSal

Reputation: 35

Back button on browser does not send the control flow to API

I have two API's:

  1. /login (returns login jsp)
  2. /home (returns home page jsp)

Functionality:

Initially, I am on login screen and on pressing the login button, I go to home screen.

When I'm on home page screen and I logout using the log out button, I am redirected to login screen.

In /home API, following is the condition:

if (userLoggedIn()) {
  return new ModelAndView("/homePage");
} else {
  return new ModelAndView("/loginPage");
}

Issue:

When I logout and move to login page, I click on back button of the browser. I am redirected to home page. (incorrect)

However, if I open the home page directly using url, I am redirected to login page (correct)

I debugged the application and I found that on pressing the back button, the control does not even go in the /home API.

How can I ensure that on pressing back button, my screen is redirected to login page?

Upvotes: 0

Views: 45

Answers (2)

HSal
HSal

Reputation: 35

Found a solution. My issue is fixed with this.

In the servlet, add the following lines for no caching.

response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); 
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0); 

Upvotes: 1

Marian Dalalau
Marian Dalalau

Reputation: 29

Set the [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")] to the action. This way the request will be made also on back button and your functionality will make the rest of redirects.

Upvotes: 0

Related Questions