OWL ITS + 탐지시스템(인터넷 진흥원)
jhjang
2021-10-14 3052936fed9166521b0557a36df83eb11a5e51ee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use strict';
 
define(['app', 'angular'],
    function (app, angular) {
        app.factory('authInterceptor', function ($rootScope, $q, $injector, $log) {
            return {
                request: function (config) {
                    if (config.method === "POST") {
                        //  통신시 테이블 컬럼 정렬 초기화
                        var $tableProvider = $injector.get('$tableProvider');
                        $tableProvider.setOrderByColumn();
                        $tableProvider.reverse = false;
 
                        if (angular.isDefined(config.data)) {
                            $log.debug(config.url + " : ", config.data);
                        }
                        else {
                            $log.debug("파일 전송 : ", config);
                        }
                    }
 
                    return config;
                },
                response: function (result) {
                    return result;
                },
                responseError: function (response) {
                    $rootScope.spinner = false;
 
                    // If we have an unauthorized request we redirect to the login page
                    // Don't do this check on the account API to avoid infinite loop
                    if (response.status === 401) {
                        var Auth = $injector.get('Auth');
                        var $state = $injector.get('$state');
                        var to = $rootScope.toState;
                        var params = $rootScope.toStateParams;
 
                        $log.debug("$rootScope.toState : ", $rootScope.toState);
                        $log.debug("$rootScope.previousStateName : ", $rootScope.previousStateName);
 
                        Auth.logout();
 
                        //  이전 화면 url, parameter 정보 저장.
                        if (angular.isDefined(to)) {
                            $rootScope.previousStateName = to.name;
                        }
 
                        if (angular.isDefined(params)) {
                            $rootScope.previousStateParams = params;
                        }
 
                        //  로그인 화면으로 이동
                        $state.go('login');
 
                    }
                    else if (response.status === 403 && response.config.method !== 'GET' && getCSRF() === '') {
                        // If the CSRF token expired, then try to get a new CSRF token and retry the old request
                        var $http = $injector.get('$http');
                        return $http.get('/').finally(function () {
                            return afterCSRFRenewed(response);
                        });
                    }
                    return $q.reject(response);
                }
            };
 
            function afterCSRFRenewed(oldResponse) {
                if (getCSRF() !== '') {
                    // retry the old request after the new CSRF-TOKEN is obtained
                    var $http = $injector.get('$http');
                    return $http(oldResponse.config);
                }
                else {
                    // unlikely get here but reject with the old response any way and avoid infinite loop
                    return $q.reject(oldResponse);
                }
            }
 
            function getCSRF() {
                var name = 'CSRF-TOKEN=';
                var ca = document.cookie.split(';');
                for (var i = 0; i < ca.length; i++) {
                    var c = ca[i];
                    while (c.charAt(0) === ' ') c = c.substring(1);
                    if (c.indexOf(name) !== -1) {
                        return c.substring(name.length, c.length);
                    }
                }
                return '';
            }
        });
    });