Thanks it is working fine. I just got a bit confused with that documentation. So wanted to get it confirmed.
On Sun, 19 Apr 2020, 07:06 Dimas Guardado, <[email protected]> wrote: > Hm, the migration guide talks about resource instances but not > ContainerRequestFilters. For what it's worth I tried this out in 2.0.8 with > a field-injected context and instance registration like you have written > above and it seems to work for me. HttpServletRequest is injected and > available when the filter is executed. What happens when you try it? > > On Thursday, April 9, 2020 at 7:51:54 AM UTC-7, tuk wrote: >> >> I have posted the same in StackOverflow >> <https://stackoverflow.com/questions/61117569/what-is-the-recommended-way-of-registering-containerrequestfilter-in-dropwizard> >> also. But did not get any reply. Anyone any thoughts on this one? >> >> On Thursday, 9 April 2020 13:51:10 UTC+5:30, tuk wrote: >>> >>> I am using 1.3.9 and working on moving to the latest dropwizard 2.x. >>> >>> Right now I have a ContainerRequestFilter like below >>> >>> @Authenticate >>> public class BasicAuthenticator implements ContainerRequestFilter { >>> @Context >>> private HttpServletRequest servletRequest; >>> private final CollectorChannel collectorChannel ; >>> private final ConfigStore configStore; >>> >>> public BasicAuthenticator(final CollectorChannel collectorChannel, >>> final ConfigStore configStore) >>> >>> >>> @Override >>> public void filter(ContainerRequestContext requestContext) throws >>> IOException { >>> >>> String requestIpAddress = servletRequest.getRemoteAddr(); >>> String requestHost = servletRequest.getRemoteHost(); >>> logger.info("Request originates from IP {} Host {}", >>> requestIpAddress, requestHost); >>> >>> String authHeader = >>> requestContext.getHeaderString("Authorization"); >>> >>> Optional<User> user = AuthUtils.getUserBasicAuth(authHeader); >>> if (!user.isPresent()) { >>> requestContext.abortWith(responseUnAuthenticated()); >>> return; >>> } >>> >>> if (!isAuthentic(user.get())) { >>> requestContext.abortWith(responseUnAuthenticated()); >>> } >>> >>> >>> if (!isValidLiSource(requestIpAddress) && >>> !isValidLiSource(requestHost)) { >>> requestContext.abortWith(responseUnauthorized()); >>> return; >>> } >>> } >>> >>> } >>> >>> >>> I register it like below >>> >>> >>> final BasicAuthenticator basicAuthenticator = new BasicAuthenticator( >>> collectorChannel, configStore);environment.jersey().register( >>> basicAuthenticator); >>> >>> >>> >>> In migration doc >>> <https://github.com/dropwizard/dropwizard/wiki/Upgrade-guide-1.3.x-to-2.0.x#context-injection-on-fields-in-resource-instances> >>> it is mentioned like below- >>> >>> >>> Migrating resource instances with field context injections to Dropwizard >>>> 2.0 involves pushing the field into a parameter in the desired endpoint >>> >>> >>> But filter() method does not get the context as argument. Can someone >>> let me know what is the recommended way to register a >>> ContainerRequestFilter like above in 2.x ? >>> >>> -- > You received this message because you are subscribed to a topic in the > Google Groups "dropwizard-user" group. > To unsubscribe from this topic, visit > https://groups.google.com/d/topic/dropwizard-user/uW1aeIO9nLg/unsubscribe. > To unsubscribe from this group and all its topics, send an email to > [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/dropwizard-user/9d4dfbab-16a7-4d49-bafd-2038e92e8850%40googlegroups.com > <https://groups.google.com/d/msgid/dropwizard-user/9d4dfbab-16a7-4d49-bafd-2038e92e8850%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "dropwizard-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/dropwizard-user/CAF6DVKMhuLn8n%2BvyGyZKYryNZVdw%3DqOmxbhuC4GF3TkV6FGrZg%40mail.gmail.com.
