本文介绍了使用Oltu传递给OAuthTokenRequest时无法使用@ Context HttpServletRequest检索发布数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Oltu作为Oauth2。

I'm using Oltu for Oauth2.

当使用@Context HttpServletRequest请求时,我无法检索发布数据

When using @Context HttpServletRequest request I am unable to retrieve post data

当我使用@FormParam时,我能够检索帖子数据。

When I am using @FormParam I am able to retrieve post data.

将请求传递给OAuthTokenRequest

On passing request to OAuthTokenRequest

OAuthTokenRequest oauthRequest = new OAuthTokenRequest(request);

我收到以下错误

调试oltu OAuthTokenRequest类以下代码用于检索参数值

When debugging on the oltu OAuthTokenRequest class following code is used to retrive param value

public String getParam(String name) {
        return this.request.getParameter(name); // from request it is unable to get post data.As i am getting request object using  @Context HttpServletRequest request .
    }

据说使用@Context HttpServletRequest请求无法获得帖子使用@Context的数据HttpServletRequest请求
所以,我的问题是

It is said that using @Context HttpServletRequest request it is not possible to get post data for using @Context HttpServletRequest requestSo, my question is

如何在jax-ws
中获取包含帖子数据的HttpServletRequest请求这样我就可以将HttpServletRequest请求传递给OAuthTokenRequest

这是我的代码

How to get HttpServletRequest request with post data in jax-ws so that I can pass HttpServletRequest request to OAuthTokenRequest This is my code

@Path("/token")
public class TokenEndpoint {

 @POST
    @Consumes("application/x-www-form-urlencoded")
    @Produces("application/json")
    public Response authorize(@FormParam("state") String state,@Context HttpServletRequest request) throws OAuthSystemException {
        try {
        // here I am unable to get value of request.getParameter("state") 
       // but using (@FormParam("state") I am able to get value of post parameter state
            request.getParameter("state");
            // exception is thrown from following code 
            OAuthTokenRequest oauthRequest = new OAuthTokenRequest(request);


推荐答案

找到解决方法(阅读评论)。

Found a workaround (read the comments).

Jersey正在消耗POST数据。

解决方案是包装HttpServletRequest并覆盖 getParameters()

这是包装器:

Jersey is consuming the POST data.
The solution is to wrap the HttpServletRequest and override getParameters().
This is the wrapper:

public class OAuthRequestWrapper extends HttpServletRequestWrapper {

    private MultivaluedMap<String, String> form;

    public OAuthRequestWrapper(HttpServletRequest request, MultivaluedMap<String, String> form)
    { super(request); this.form = form; }

    @Override
    public String getParameter(String name) {
        String value = super.getParameter(name);
        if (value == null)
        { value = form.getFirst(name); }
        return value;
    }
}

这是实现令牌POST方法的方法:

And this is how to implement the token POST method:

@POST
@Path("/token")
@Consumes("application/x-www-form-urlencoded")
@Produces("application/json")
public Response token(@Context HttpServletRequest request, MultivaluedMap<String, String> form) {   

    [...]

    OAuthTokenRequest oauthRequest = new OAuthTokenRequest(new OAuthRequestWrapper(request, form));

    [...]

}

这篇关于使用Oltu传递给OAuthTokenRequest时无法使用@ Context HttpServletRequest检索发布数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 00:07