I am using 1.3.9 and working on moving to the latest dropwizard 2.x.
@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 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 ?