- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
如何拦截请求、对 ID 执行 ping 不同的路由、将 ID 存储在 session 中,然后使用我刚刚获得的 ID 继续原始请求(尤其是带有负载的 PUT/POST)?
我正在使用 HapiJS (8) 将客户端的请求代理到现有的 API(我无法控制流程/逻辑)。 API 要求每个请求在查询字符串或有效负载中包含“ session ID”(取决于 http 方法)。为了获得 session ID,我所要做的就是索要一个...不需要用户名/密码(它在 header 中使用基本身份验证)。如果不续订, session ID 每 24 小时就会过期。每个客户端都有自己的 session ID。
我目前使用 hapi-auth-cookie 来存储 session ID 的值,当需要该 ID 时会查询该值。如果 ID 已过期或为空,我需要请求一个新 ID,然后客户端的请求才能成功代理到 API。
当客户端的请求方法为“GET”时,我利用 hapi-auth-cookie docs 中描述的 appendNext
配置非常优雅地处理此挑战。该请求被 hapi-auth-cookie 拦截,如果需要新的 session ID,则会向该特定路由发送请求以获取它,API 返回该 ID,然后将其分配给 Hapi session ,然后(使用 Wreck )reply.redirect
返回到完成时的原始 GET 请求。无缝且优雅。
但是,我无法弄清楚如何使用包含数据有效负载的不同 http 方法来完成相同的流程。
除了reply.redirect
之外,还有什么东西可以在保持原始有效负载和方法的同时实现相同的目标吗?还是有更好的方法来做到这一点?
主应用程序文件(hapi-auth-cookie 配置)
# register plugins
server.register require('./plugins')
, (err) ->
throw err if err
# set authentication (NLS) configs
server.auth.strategy 'session', 'cookie',
password: 'session_pwd'
cookie: 'ghsid'
redirectTo: '/api/SessionID' #get new session ID
isSecure: config.get 'ssl'
appendNext: true
ttl: config.get 'session_length'
利用 session 身份验证并调用 hapi-auth-cookie 插件的 Controller :
simpleRequest:
auth: 'session'
handler: (request, reply) ->
qs = Qs.stringify request.query
request.papi_url = "/api/route/sample?#{qs}"
reply.proxy
mapUri: (request, reply) ->
auth = config.get 'basic_auth'
api_host = config.get 'api_host'
papi_url = request.papi_url
path = api_host + papi_url
next null, path, {authorization: auth}
获取新 session ID的路由
module.exports = [
{
path: '/api/SessionID'
method: 'GET'
config: SessionController.session
}
]
session Controller
Wreck = require 'wreck'
config = require 'config'
module.exports =
session:
description: 'Get new session ID'
auth:
mode: 'try'
strategy: 'session'
plugins:
'hapi-auth-cookie':
redirectTo: false
handler: (request, reply) ->
# request configs
papi_url = "/Session"
api_host = config.get 'api_host'
url = api_host + papi_url
opts =
headers:
'Authorization': config.get 'basic_auth'
'content-type': 'application/json;charset=UTF-8'
# make request to PAPI
Wreck.post url, opts, (err, res, body) ->
throw new Error err if err
try
bdy = JSON.parse body
sess =
nls: bdy.SessionId
if bdy.SessionId
# authenticate user with NLS
request.auth.session.set sess
# redirect to initial route
reply.redirect request.url.query.next
else
return throw new Error
catch err
throw new Error err
根据马特·哈里森的回答,我创建了一个自定义插件,该插件注册为身份验证方案,以便我可以按路由控制它。
这是插件代码:
Wreck = require 'wreck'
config = require 'config'
exports.register = (server, options, next) ->
server.auth.scheme 'cookie', internals.implementation
next()
exports.register.attributes =
name: 'Hapi Session Interceptor'
version: '1.0.0'
internals = {}
internals.implementation = (server, options, next) ->
scheme = authenticate: (request, reply) ->
validate = ->
session = request.state.sessionID
unless session
return unauthenticated()
reply.continue(credentials: {'session': session})
unauthenticated = ->
api_url = "/SessionID"
api_host = config.get 'api_host'
url = api_host + api_url
opts =
headers:
'Authorization': config.get 'basic_auth'
'content-type': 'application/json;charset=UTF-8'
# make request to API
Wreck.post url, opts, (err, res, body) ->
throw new Error err if err
bdy = JSON.parse body
sess =
session: bdy.SessionId
if bdy.SessionId
reply.state 'sessionID', bdy.SessionId
reply.continue(credentials: sess)
else
return throw new Error
validate()
return scheme
最佳答案
虽然并不完全忠实于您的代码,但我已经整理了一个示例,其中包含我认为您正在使用的所有部分。
我制作了一个service
插件来代表您的 API。 upstream
插件代表您要代理的实际上游 API。
所有请求都通过服务
并代理到上游
,后者仅打印出收到的所有 header 和有效负载。
如果原始请求不包含带有 sessionId 的 cookie,则会在上游
上点击路由以获取 cookie。当响应返回下游时,将使用该值设置 cookie。
代码在这里:https://github.com/mtharrison/hapijs-proxy-trouble
使用curl 和浏览器尝试一下。
获取: curl http://localhost:4000
POST W/PAYLOAD: curl -X POST -H "content-type: application/json"-d '{"example":"payload"}' http://本地主机:4000
index.js
var Hapi = require('hapi');
var server = new Hapi.Server();
server.connection({ port: 4000, labels: ['service'] }); // Your service
server.connection({ port: 5000, labels: ['upstream']}); // Pretend upstream API
server.state('session', {
ttl: 24 * 60 * 60 * 1000,
isSecure: false,
path: '/',
encoding: 'base64json'
});
server.register([{
register: require('./service')
}, {
register: require('./upstream')
}],
function (err) {
if (err) {
throw err;
}
server.start(function () {
console.log('Started!');
});
});
service.js
var Wreck = require('wreck');
exports.register = function (server, options, next) {
// This is where the magic happens!
server.select('service').ext('onPreHandler', function (request, reply) {
var sessionId = request.state.session;
var _done = function () {
// Set the cookie and proceed to the route
request.headers['X-Session-Id'] = sessionId;
reply.state('session', sessionId);
reply.continue();
}
if (typeof sessionId !== 'undefined')
return _done();
// We don't have a sessionId, let's get one
Wreck.get('http://localhost:5000/sessionId', {json: true}, function (err, res, payload) {
if(err) {
throw err;
}
sessionId = payload.id;
_done();
});
});
server.select('service').route({
method: '*',
path: '/{p*}', // Proxies all routes and methods
handler: {
proxy: {
host: 'localhost',
port: 5000,
protocol: 'http',
passThrough: true
}
}
});
next();
};
exports.register.attributes = {
name: 'your-service'
};
upstream.js
exports.register = function (server, options, next) {
server.select('upstream').route([{
method: '*',
path: '/{p*}',
handler: function (request, reply) {
// Just prints out what it received for headers and payload
// To prove we got send the original payload and the sessionID header
reply({
originalHeaders: request.headers,
originalPayload: request.payload,
})
}
}, {
method: 'GET',
path: '/sessionId',
handler: function (request, reply) {
// Returns a random session id
reply({ id: (Math.floor(Math.random() * 1000)) });
}
}]);
next();
};
exports.register.attributes = {
name: 'upstream'
};
关于node.js - HapiJS 代理问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28949559/
我完全不同意配置代理来检查我正在开发的应用程序的 HTTP(S) 流量。我试过运行 Fiddler2 和 Charles Web Proxy,它们都在 127.0.0.1:888 上运行,并使用以下参
我有一个 AWS 环境,其中有几个实例没有预安装 SSM 代理,也没有 key 对,有没有办法在不使用 SSH 登录我的实例的情况下安装 SSM 代理? 在此先感谢您的帮助! 最佳答案 没有。这是一个
在本教程中,您将借助示例了解 JavaScript 代理。 在 JavaScript 中,proxy(代理对象)用于包装对象并将各种操作重新定义到对象中,例如读取、插入、验证等。代理允许您向对
我有一个基于 Martini 的小型应用程序,但遇到了一个我无法解决的问题。 我想添加一个应用程序功能,允许用户从第三个服务器获取文件,并在 HTTP header 中进行一些更改。某种代理。这些文件
结构对比 讲实话,博主当初学习完整设计模式时,这三种设计模式单独摘哪一种都是十分清晰和明确的,但是随着模式种类的增加,在实际使用的时候竟然会出现恍惚,例如读开源代码时,遇到不以模式命名规范的代码时,
我正在尝试代理运行 ELK 的后端服务器。这是我的环境信息: root@proxy:~# root@proxy:~# cat /etc/*release DISTRIB_ID=Ubuntu DISTR
我需要为我的 java 应用程序编写一个代理,它在每个数组创建时执行一些特定的操作。到目前为止,我无法找到在此事件上运行我的代码的任何方法。 java.lang.instrument.ClassFil
PHP 代理如何工作? 我希望制作一个类似于其他 php 代理的小脚本 但是它实际上是如何工作的呢? 最佳答案 我正在考虑一个 PHP 代理,用于绕过 AJAX Sane Origin 策略。如果您需
我有一个 Electron 应用程序,试图通过该应用程序从同一网络调用url,但是出于安全考虑,我考虑了使用代理的想法。 function createWindow () { const mai
我有 1 台计算机,安装了 1 个网卡。网卡有 10 个 IP 地址分配给它。我在那里运行了一个 Windows 桌面应用程序。该应用程序基本上是一个调用 1 个特定网站的网络浏览器。 我想要实现的是
我想将 Burp 配置为我的 java 代码的代理,以查看请求和响应。Burp 作为 Web 浏览器之间的代理可以很好地工作,但它不适用于 Java 应用程序。 我已经在代码中添加了这样的行: Web
据我所知,在Spring AOP中,当我们想要拦截某些方法调用时,我们会配置一个具有与所需方法调用相匹配的切入点配置的Aspect。也就是说,我们在Aspect端配置拦截。 有没有一种方法可以完全从相
这可能是一个常见问题,但是:我有一个正在向 发出请求的应用程序elldmess.cz/api/... 但是这个api已经没有了。 现在我想要“东西”,即 catch 对 elldmess.cz/api
我正在尝试在 Android 中创建代理,但我必须使用套接字。我已经阅读了很多教程并提出了以下代码。不幸的是,浏览器似乎没有获得任何数据,一段时间后它显示标准网页,说网页不可用。可能是什么原因?感谢您
我在使用此代码时遇到了一些问题,具体取决于我使用的浏览器,有些 URL 在 IE 中显示正确,但在 Firefox 中显示为纯文本(例如 www.microsoft.es 在 IE 上看起来不错,但在
我正在尝试通过 urllib 获取一些 url 并通过我的代理进行 Mechanize 。 使用 mechanize 我尝试以下操作: from mechanize import Browser im
我安装了一个嵌入式设备(光伏转换器),它提供了一个正常的 http Web 界面(信息和设置)。该转换器具有用户身份验证,但只能通过 http 进行。出于安全考虑,我不想将服务器直接发布到互联网上。在
我正在搜索有关如何使用支持 HTTPS 的 Ruby 编写代理的一些示例。我有一个使用 Webricks HTTPProxyServer 实现的简单代理,但我注意到,HTTPS 流量只是隧道传输(它应
我的一位客户刚收到他选择的开发商订购的软件,让我看一下并准备托管程序。 这是一个 Java (jar) 应用程序,到目前为止一切顺利......但我看到了一些可疑的东西,软件每隔 60 分钟左右连接到
我试图在 C# 中创建一个 HTTPS 代理服务器。这里有人发布了解决方案: string host = "encrypted.google.com"; string
我是一名优秀的程序员,十分优秀!