Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring security and angular javascript redirect to login page

Hi I am facing a problem with spring security+Spring MVC+angular javascript.. When the session is invalid I want to request the (whole) page to get redirected to login page.

I tried <session-management invalid-session-url="/login" /> But its not working since its a one page app...

Or any suggestion to handle sessiontimeout in angular javascript single page app

Any help ?

like image 867
Freaky Thommi Avatar asked Jun 18 '26 04:06

Freaky Thommi


2 Answers

You can write or use HTTP interceptors like this https://github.com/witoldsz/angular-http-auth to capture timeout issues.

Basically when session expires any server request starts to return 401, which can be capture by using HTTP interceptor and necessary redirects can be performed.

like image 118
Chandermani Avatar answered Jun 20 '26 19:06

Chandermani


I made a simple code for checking session timeout and redirect to login page for spring security. It's not a smart solution, but returned status is 200 and response doesn't contain redirect information.

var app = angular.module("app");
app.factory("sessionInjector", ['$log', function($log){
    return {
        request: function(config) {return config;},
        response: function(response) {
            if (typeof response.data === "string" && response.data.indexOf("login") > -1) {
                alert("Session expired.");
                location.reload();
            }
            return response;
        }
    };
}]);
app.config(["$httpProvider", function($httpProvider){
    $httpProvider.interceptors.push("sessionInjector");
}]);
like image 39
Jonghee Park Avatar answered Jun 20 '26 17:06

Jonghee Park



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!