正在显示
3 个修改的文件
包含
37 行增加
和
6 行删除
... | ... | @@ -50,6 +50,7 @@ exports = module.exports = function () { |
50 | 50 | proxy: { |
51 | 51 | target: 'http://47.110.158.110:20000', |
52 | 52 | apiRouteRegexp: '/api', |
53 | + ignoreRouters: ['/api/uaa/v1/auth/tokens'], | |
53 | 54 | authUrl: '/uaa/v1/auth/tokens', |
54 | 55 | refreshUrl: '/uaa/v1/auth/tokens', |
55 | 56 | signOutUrl: '/signOut', | ... | ... |
1 | 1 | //代理RESTful API |
2 | 2 | var express = require('express'); |
3 | -exports = module.exports = function (settings, proxy) { | |
3 | +exports = module.exports = function (settings, proxy, logger) { | |
4 | 4 | |
5 | 5 | var app = this; |
6 | 6 | var router = express.Router(); |
7 | + var checkAuth = function (req) { | |
8 | + var flag = true, ignore = false; | |
9 | + if (settings.proxy && settings.proxy.ignoreRouters) { | |
10 | + settings.proxy.ignoreRouters.map(function (ignoreR, i) { | |
11 | + ignore = req.originalUrl.indexOf(ignoreR) != -1; | |
12 | + }); | |
13 | + } | |
14 | + if (ignore) { | |
15 | + flag = true | |
16 | + } else if (req.session && req.session.token) { | |
17 | + var token = {}; | |
18 | + try { | |
19 | + token = JSON.parse(req.session.token); | |
20 | + } catch (error) { | |
21 | + flag = false; | |
22 | + logger.error("token 字符串不能被解析", error); | |
23 | + } | |
24 | + if (token && token.access_token && token.refresh_token) | |
25 | + flag = true; | |
26 | + } else { | |
27 | + logger.debug("token 不存在"); | |
28 | + flag = false; | |
29 | + } | |
30 | + return flag; | |
31 | + } | |
32 | + | |
7 | 33 | if (settings.proxy) { |
8 | 34 | app.use(settings.proxy.apiRouteRegexp, router); |
9 | 35 | router.all('*', function (req, res, next) { |
10 | - proxy.web(req, res, next); | |
36 | + if (checkAuth(req)) | |
37 | + proxy.web(req, res, next); | |
38 | + else | |
39 | + res.redirect(settings.proxy.signInUrl); | |
11 | 40 | }); |
12 | 41 | } |
13 | 42 | }; |
14 | 43 | |
15 | -exports['@require'] = ['igloo/settings', 'igloo/restler']; | |
44 | +exports['@require'] = ['igloo/settings', 'igloo/restler', 'igloo/logger']; | ... | ... |
... | ... | @@ -77,7 +77,6 @@ exports = module.exports = function (logger, settings) { |
77 | 77 | if (err) |
78 | 78 | logger.error(err); |
79 | 79 | else { |
80 | - | |
81 | 80 | res.send(data); |
82 | 81 | } |
83 | 82 | }) |
... | ... | @@ -90,6 +89,8 @@ exports = module.exports = function (logger, settings) { |
90 | 89 | if (response.statusCode === 401) { |
91 | 90 | refreshToken(req, res, next, reqOption); |
92 | 91 | } else { |
92 | + var options = reqOption.options || {}; | |
93 | + logger.error({ url: reqOption.url, method: options.method, headers: options.headers, query: options.query, data: JSON.parse(options.data), backdata: data }); | |
93 | 94 | res.send(data); |
94 | 95 | } |
95 | 96 | } |
... | ... | @@ -123,11 +124,11 @@ exports = module.exports = function (logger, settings) { |
123 | 124 | handleFail(data, response, req, res, next, { url, options }); |
124 | 125 | }) |
125 | 126 | .on('error', function (err, response) { |
126 | - logger.error(err); | |
127 | + logger.error({ 'message': '发生错误', url: url, method: options.method, headers: options.headers, query: options.query, data: JSON.parse(options.data), error: err }); | |
127 | 128 | res.send(err); |
128 | 129 | }) |
129 | 130 | .on('timeout', function (ms) { |
130 | - logger.error('timeout', ms); | |
131 | + logger.error({ 'message': '请求超时', url: url, method: options.method, headers: options.headers, query: options.query, data: JSON.parse(options.data), timeout: ms }); | |
131 | 132 | res.send({ 'message': '请求超时' }); |
132 | 133 | }); |
133 | 134 | } | ... | ... |
请
注册
或
登录
后发表评论