简体   繁体   English

如何使用Jersey和Java EE 5检索ServletRequest

[英]How to retrieve ServletRequest using Jersey and Java EE 5

I'm working on creating a security library that will be used by several RESTful clients. 我正在创建一个安全库,该库将由多个RESTful客户端使用。 I'm using Java EE 5, Jersey 1.17 and Maven. 我正在使用Java EE 5,Jersey 1.17和Maven。 The clients will use my library to call a third party app using a token. 客户将使用我的图书馆使用令牌来调用第三方应用程序。 The third party app will then return all the information it has on that token, like expiration, scope and userId. 然后,第三方应用程序将返回其在该令牌上拥有的所有信息,例如到期,范围和用户ID。 My idea is to make a filter that will check if there is an Authorization header, and if that's so, it calls the third party app. 我的想法是制作一个过滤器,该过滤器将检查是否存在“授权”标头,如果是,它将调用第三方应用程序。 If the third party app validates the token and returns the token's info, I need to return that information, stored in a TokenInformation object, back to the resources. 如果第三方应用程序验证了令牌并返回了令牌的信息,那么我需要将该信息(存储在TokenInformation对象中)返回到资源中。 In a previous post, someone said that I could do this: 在上一篇文章中,有人说我可以这样做:

public class MyFilter implements Filter{

    @Override
    public void doFilter(final ServletRequest request,
            final ServletResponse response,
            final FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;        
        String header = req.getHeader("Authorization");
        TokenInformation info = new TokenInformation();
        info = buildInfo(info);
        if (true) {
            request.setAttribute("auth", info);
            chain.doFilter(request, response);
        } else {
            handleError(response);
        }
    }
}

So, by sending the TokenInformation object to the request as an additional attribute, I would be able to retrieve it later in the resource classes. 因此,通过将TokenInformation对象作为附加属性发送到请求,我将能够稍后在资源类中检索它。 The thing is that I'm using Java EE 5, and I didn't realize that I couldn't use the @Context annotation to inject the ServletRequest object. 事实是,我使用的是Java EE 5,但我没有意识到我无法使用@Context注释注入ServletRequest对象。 How can I access the ServletRequest object again from a resource class, so that I can access the TokenInformation object in, for example, the DAO? 如何从资源类再次访问ServletRequest对象,以便可以在例如DAO中访问TokenInformation对象?

The way I'm using jersey is by doing this in my web.xml : 我使用运动衫的方式是在web.xml执行以下操作:

<servlet>
    <servlet-name>Security API</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.ni.apps.engineering.securitylibrary.resources.SecurityResource</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Security API</servlet-name>
    <url-pattern>/1/*</url-pattern>
</servlet-mapping>

The SecurityResource class has this: SecurityResource类具有以下内容:

public class SecurityResource extends Application{

    public static final String SUPPORTED_REPRESENTATIONS = MediaType.APPLICATION_XML
            + "," + MediaType.APPLICATION_JSON;

    @Override
    public Set<Class<?>> getClasses() {
        HashSet<Class<?>> set = new HashSet<Class<?>>();
        set.add(Security.class);                
        return set;
    }

}

The Security class has this: Security类具有以下功能:

@Path("")
public class Security implements ISecurity{


    @Override
    public Response get(String upId) {
        String test = "";
        try{

            TokenInformation tI = (TokenInformation) HttpServletRequestWrapper.
            test = "test1";
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
        return null;
    }

}

You don't have to access ServletRequest at Dao layer. 您不必在Dao层访问ServletRequest。 In Servlet you can get ServletRequest object and you can pass the value to Dao layer. 在Servlet中,您可以获取ServletRequest对象,并将该值传递到Dao层。 If you really want to access then pass ServletRequest object to Dao layer by reference. 如果您确实要访问,则通过引用将ServletRequest对象传递给Dao层。

<servlet-name>Security API</servlet-name>
<servlet-class>com.packagename.MyServlet</servlet-class>

public MyServlet extends com.sun.jersey.spi.container.servlet.ServletContainer{}

You can extend jersey servlet and you can initialize Servlet with Application Class by programatically. 您可以扩展jersey Servlet,也可以通过应用程序类通过应用程序初始化Servlet。

In MyServlet you can reach the request object. 在MyServlet中,您可以到达请求对象。

Servlet Information : https://jersey.java.net/apidocs/1.17/jersey/com/sun/jersey/spi/container/servlet/ServletContainer.html Servlet信息: https : //jersey.java.net/apidocs/1.17/jersey/com/sun/jersey/spi/container/servlet/ServletContainer.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM