编程语言
首页 > 编程语言> > 如何在Java Restlet中访问请求的标头值?

如何在Java Restlet中访问请求的标头值?

作者:互联网

我正在使用Restlet Java开发Web服务.

为此,我想保护一些Web服务免受未经授权的客户端的攻击.所以我写了Filter类.在那个Filter类中,我想得到Request的头文件.但我收到以下错误 –

java.lang.ClassCastException: org.restlet.engine.http.HttpRequest cannot be cast to javax.servlet.http.HttpServletRequest

编码是 –

public class MyFilter extends Filter {

    @Override
    protected int beforeHandle(Request request, Response response) {

        int result = STOP;

        HttpServletRequest httpReq = (HttpServletRequest) request;
        String user_token = httpReq.getHeader("auth");

        if(user_token.equals("xyz")) {
            result = CONTINUE;
        }

        return result;
    }
}

请建议我访问Java Restlet中Request的标头值?

解决方法:

我解决了我的问题

Form headers = (Form) request.getAttributes().get("org.restlet.http.headers");
String user_token = headers.getFirstValue("Location");

我发现这个http://blog.yudongli.com/2009/12/get-request-header-in-restlet_13.html链接很有用.

标签:java,rest,request,web-services,restlet
来源: https://codeday.me/bug/20190529/1177501.html