Handle HTTP 302 response from proxy in angularjs

前端 未结 5 1554
迷失自我
迷失自我 2020-11-28 06:19

I have a reverse proxy that checks global authentication for several applications. When the user is disconnected but still trying to use my application, the proxy sends a 30

5条回答
  •  一整个雨季
    2020-11-28 06:40

    Your 302 -Redirect is being handled directly by the browser and there is nothing you can do about it directly. You can, however, use an httpInterceptor to help you along. You'll need to include $httpProvider in your app DI list, and then somewhere in your config function put a reference to it like this:

    $httpProvider.responseInterceptors.push('HttpInterceptor');
    

    A sample interceptor looks like this:

    window.angular.module('HttpInterceptor', [])
    .factory('HttpInterceptor', ['$q', '$injector',
        function($q, $injector) {
            'use strict';
    
            return function(promise) {
                return promise.then(success, error);
            };
    
            function success(response) {
                return response;
            }
    
            function error(response) {
                var isAuthRequest = (response.config.url.indexOf('/v1/rest/auth') !== -1);
    
                //if we are on the authenticating don't open the redirect dialog
                if (isAuthRequest) {
                    return $q.reject(response);
                }
    
                //open dialog and return rejected promise
                openErrorDialog(response);
                return $q.reject(response);
            }
    
            function openErrorDialog(response) {
                $injector.get('$dialog').dialog({
                    backdropFade: true,
                    dialogFade: true,
                    dialogClass: 'modal newCustomerModal',
                    resolve: {
                        errorData: function() {
                            return response.data;
                        },
                        errorStatus: function() {
                            return response.status;
                        }
                    }
                })
                .open('/views/error-dialog-partial.htm',
                    'errorDialogController')
                .then(function(response) {
                    if (response) {
                        window.location = '/';
                    }
                });
            }
        }
    ]);
    

提交回复
热议问题