在jersey中为自己的服务编写代理/包装器类 [英] Write proxy/wrapper class for own service in jersey
问题描述
我想在基本的HTTP身份验证运行的情况下访问完整的REST服务. 但是,当
I want to access a full rest service with basic http auth running. However there is no way to for the javascript browser client to suppress the authenticate box when a wrong credential is provided.
我考虑了解决这个问题的不同方法
I thought about different methods to solve this problem
- 有人建议使用过滤器删除WWW-Authenticate标头(我不认为这是一种干净的方法)
- 我可以重写我的应用程序以完全不使用基本Http身份验证(我认为这很麻烦)
- 我可以编写一个与我的常规服务对话的代理
我最喜欢最后一种方法. 我保留了常规的Rest接口,但也可以选择将此接口与不太灵活的客户端一起使用. 此外,我以后可以代理某些浏览器不支持的Http请求.
I like the last approach the best. I keep my regular Rest Interface, but also have the option to use this interface with clients that are not that flexible. Furthermore I can later proxy Http Requests unsupported by some browsers.
这个想法是要有一个/api/proxy/{request}路径,该路径代理到/api/{request}并返回类似Facebook图形的JSON查询{数据:{data},错误:{error}}
The idea is to have a /api/proxy/{request} path that proxies to /api/{request} and returns a Facebook-Graph-like JSON query { data: {data}, error: {error}}
这是Proxy类的存根
This is the stub of the Proxy class
@Path("proxy")
public class ProxyResource {
@GET()
@Path("{url: [a-zA-Z/]*}")
public String get(@Context Request request, @PathParam("url") String url) {
// remove proxy/ from path
// resend request
// verify result
}
} 我可以访问该请求(似乎是一个ContainerRequest).如何修改请求,而无需从头开始重新发送.
} I can access the Request (which seems to be a ContainerRequest). How can I modify the request without building it from scratch to resend it.
:当有人知道更好的方法时,我很高兴听到它.
when somebody knows a better approach i am delighted to hear about it.
推荐答案
当我开始更深入地研究这个问题时,我发现不是401是问题所在.从服务器发回的www-authenticate标头导致浏览器打开登录框.
As I started to digg deeper into this, i found out that not the 401 was the problem. The www-authenticate header sent back from the server caused the browser to open the login box.
如果有人感兴趣,我已经编写了一个小小的nodejs代理,以从所有服务器请求中删除www-authenticate.
If somebody is interested I've written a little nodejs proxy to remove a www-authenticate from all server requests.
https://gist.github.com/ebb9a5052575b0a3f41f
由于这不是我最初的问题的答案,因此我将其保留为开放状态.
As this is not the answer to my original question I will leave it open.
这篇关于在jersey中为自己的服务编写代理/包装器类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!