updating a div with URL redirection instead of global redirect

I am facing an issue with redirecting my website flow to the login page when a user clicks any link on the page after the session has expired (either due to timeout or manual logout from another window).

In an attempt to solve this, I inserted the following code snippet into my main.gsp file (included in every gsp):

<script>
  $.ajaxSetup({
            statusCode: {
            401: function(){

            // Redirect the user to the login page.
            location.href = "/gra/login/auth";
            }
            }
            });
  </script>

This solution was working fine initially, but after some recent changes to our application such as Oracle migration, UI updates, and implementation of a Spring security plugin, it seems that instead of forwarding the entire page to the login page, only the content in the target container gets refreshed. This means that when a user clicks a link after the session timeout, the logout page is loaded within that specific container.

Previously, the system would automatically direct the user to the login page without any issues. How can I modify this to ensure a global page redirection?

Update: Upon inspecting with Firebug, I discovered that clicking a link post-session timeout now triggers a 302 response code (moved temporarily) instead of the expected 401 error. What steps should I take to address this discrepancy?

Answer №1

Utilizing Grails Filters for this task is highly recommended. Here is the documentation to guide you.

The provided code snippet will intercept all controllers and actions before they are executed to ensure the session is valid. If it is not, the user will be redirected to the login page.

Make sure to adjust the condition in the `if` statement to match your session validation requirements correctly.

class SecurityFilters {
   def filters = {
       sessionCheck(controller: '*', action: '*') {
           before = {
              if (!session && !actionName.equals('login')) {
                  redirect(uri: '/gra/login/auth')
                  return false
               }
           }
       }
   }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a tooltip for new list items: A step-by-step guide

I'm currently working on creating a tooltip using JQuery for incoming list items. Users will input text in a textfield, press enter, and those values will be displayed in a visible list on the website. I intend to have a tooltip associated with each i ...

The transparency level of materials in THREE.js

When using the lambert shader, I encountered an issue with setting the material. In the example provided, it sets the material as follows: this.material.uniforms.emissive.value = new THREE.Color( Math.random(), Math.random(), Math.random()); Prior ...

Navigate through the elements of an Ext.form.CheckboxGroup using Ext JS

Currently, I am working with an Ext.form.CheckboxGroup that contains multiple items of Ext.form.Checkbox. I am wondering if there is a way to iterate through each item within the Ext.form.CheckboxGroup? I attempted the code below without success: for ( ...

Is there a way to transmit custom JSON to node.js without having to refresh the page?

My main objective is to send custom JSON data to node.js when a button is clicked. I am currently familiar with sending form input only. Below is the code snippet I have used to send form data: function postForm() { $('form').submit(function(e ...

Uncovering the source of glitchy Angular Animations - could it be caused by CSS, code, or ng-directives? Plus, a bonus XKCD comic for some light-hearted humor

Just finished creating an XKCD app for a MEAN stack class I'm enrolled in, and I'm almost done with it. However, there's this annoying visual bug that's bothering me, especially with the angular animations. Here is the link to my deploy ...

Issue with child prop not being updated despite changes in parent component

I'm facing a strange issue where altering a child component's prop doesn't trigger a re-render. Here's the scenario: In the parent component, I have: <child :problemProp="proplemPropValue"></child> In the child component, ...

What mechanism does package.json use to determine whether you are currently operating in development or production mode?

What is the process for package_json to determine when to load devDependencies as opposed to regular dependencies? How does it differentiate between local development and production environments? ...

I'm having trouble assigning the data from my object to the setState in my React class component

class AreaChart extends React.Component { constructor(props) { super(props); this.state = { chartData: GRAPH_DATA, chartDataSelection: GRAPH_DATA.selection };} The GRAPH_DATA object contains all the necessary data for my Area Ch ...

Executing functions in real-time using React Native

I'm fairly new to Object-Oriented Programming (OOP) and my understanding of promises, asynchronous/synchronous function execution is quite basic. Any guidance or help from your end would be greatly appreciated! Let's take a look at an example fr ...

Is there a way to retrieve $httpProvider.defaults.xsrfCookieName within .factory?

Here's a question that might come from someone new to programming. I'm trying to avoid hard-coding the values for xsrfHeaderName and xsrfCookieName. Can anyone guide me on how to retrieve them from the $httpProvider? .factory('XSRFIntercept ...

Tips for properly formatting the sort_by parameter in Cloudinary to avoid errors

Greetings to the helpful stack overflow community, I have encountered an issue with fetching images from cloudinary via a post request. Everything works fine until I include the sort_by parameter in the URL. This results in an error related to the format ...

The Zoom-sdk functions properly on a local machine, but encounters issues when it is

Using zoom's API, jwt, and the websdk, I am able to create a meeting on button click, join as a host, and start the meeting for others to join. This process works flawlessly when running locally, but once deployed to Cloudflare, I encounter the follow ...

How can I link a Vue.js webpage with a WordPress site?

Looking to integrate a Vue.js 2 single page into an existing WordPress website? Perhaps you've already got an old WordPress site and now want to develop a new Vue tool or page that can be added seamlessly to the menu. How can this be achieved within W ...

Real-time JQuery search with instant results

While utilizing a custom script to display search results on the page, I encountered an issue when typing long sentences. Each request goes to the server, resulting in delayed responses that stack up and cause the div to change quickly. This is not the des ...

What steps should I take to ensure that the login page functions properly?

Below is the HTML code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Login Pag ...

Passing multiple parameters using jQuery ajax with the GET method can be a powerful

When using an ajax method to retrieve a partial view based on a filter criteria using GET, I encountered an issue where I couldn't pass in more than one parameter unless they were all primitive types. The parameters were being passed in as null withou ...

Faulty toggle functionality within a JavaScript-created accordion panel

HTML I'm looking to add a FAQ question within the div with the class "section-center" <body> <section class="questions"> <div class="title"> <h2>FAQ SECTION</h2> < ...

Leveraging req.files for uploading multiple files at once

When it comes to handling a multiple file upload on the client side, I have encountered an issue with my HTML code. Here is what it looks like: form(method='post', enctype='multipart/form-data')#createReportForm input(type='file ...

"Encountered a roadblock while attempting to utilize the applyMatrix

I am currently working on running an animation using a JSON file and a custom JSON loader, not the one provided with three.js. Within the JSON file, there is an object called frames that contains multiple frames, each with shape information and a simulatio ...

Having issues with a partial view not loading properly when making an ajax call in MVC5

Currently, I am developing a view that serves as a create view for customers. Within the "Create" View, I have implemented a textbox to search for items in the database and then display their details on a partial view. The partial view action is successfu ...