jersey 2: How to create custom HTTP param binding

后端 未结 4 2135
醉梦人生
醉梦人生 2020-12-05 15:08

I am trying to create a custom http param binding for my restful service. Please see the example below.

@POST
@Path(\"/user/{userId}/orders\")
@Produces(Medi         


        
4条回答
  •  天涯浪人
    2020-12-05 15:39

    I don't know how to resolve your exception. However, may I propose you a different way to do the same thing. I hope it helps.

    I've faced exactly the same problem: I need extra parameters in the http header (btw, also related to authentication). Besides, I need to send them in every call, since I want to do a "typical" rest implementation, without maintaining a session.

    I'm using Jersey 2.7 - but I'd say it should work in 2.0. I've followed their documentation https://jersey.java.net/documentation/2.0/filters-and-interceptors.html

    It's quite clear there, but anyway I copy-paste my implementation below. It works fine. True there are some other ways to secure a rest service, for example this is a good one: http://www.objecthunter.net/tinybo/blog/articles/89

    But they depend on the application server implementation and the database you use. The filter, in my opinion, is more flexible and easier to implement.

    The copy-paste: I've defined a filter for authentication, which applies to every call and it is executed before the service (thanks to @PreMatching).

    @PreMatching
    public class AuthenticationRequestFilter implements ContainerRequestFilter {
    
        @Override
        public void filter(final ContainerRequestContext requestContext) throws IOException {
            final MultivaluedMap headers = requestContext.getHeaders();
            if (headers == null) {
                throw new...
            }
    
            // here I get parameters from the header, via headers.get("parameter_name")
            // In particular, I get the profile, which I plan to use as a Jersey role
            // then I authenticate
            // finally, I inform the Principal and the role in the SecurityContext object, so that I can use @RolesAllowed later
            requestContext.setSecurityContext(new SecurityContext() {
    
                @Override
                public boolean isUserInRole(final String arg0) {
                    //...
                }
    
                @Override
                public boolean isSecure() {
                    //...
                }
    
                @Override
                public Principal getUserPrincipal() {
                    //...
                }
    
                @Override
                public String getAuthenticationScheme() {
                    //...
                }
            });
    
        }
    
    }
    

    You have to include this filter class in your implementation of ResourceConfig,

    public class MyResourceConfig extends ResourceConfig {
    
        public MyResourceConfig() {
    
            // my init
            // my packages
            register(AuthenticationRequestFilter.class); // filtro de autenticación
            // other register
    
        }
    
    }
    

    Hope it helps!

提交回复
热议问题