I'm trying to remake this solution found here Basic HTTP authentication with Jersey / Grizzly
i've included these imports so far
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response.Status;
and after some search i included this one
import org.glassfish.jersey.server.ContainerRequest;
My problem is now that i get these errors
... AuthFilter is not abstract and does not override abstract method filter(ContainerRequestContext) in ContainerRequestFilter
... method does not override or implement a method from a supertype
... cannot find symbol
[ERROR] symbol: method getHeaderValue(String)
[ERROR] location: variable containerRequest of type ContainerRequest
and the code if you don't want to switch tabs is here
@Override
public ContainerRequest filter(ContainerRequest containerRequest)
throws WebApplicationException {
// Automatically allow certain requests.
String method = containerRequest.getMethod();
String path = containerRequest.getPath(true);
if (method.equals("GET") && path.equals("application.wadl"))
return containerRequest;
// Get the authentication passed in HTTP headers parameters
String auth = containerRequest.getHeaderValue("authorization");
if (auth == null)
throw unauthorized;
Unfortunately i don't have the needed reputation to ask the OP so any help is appreciated.
Edit: Apparently com.sun.jersey.spi.container.ContainerRequest does not include getHeadervalue and the one who does cannot be found by jersey 2.0 Any way around it?
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ContainerRequestContext;
import org.glassfish.jersey.internal.util.Base64;
import java.io.IOException;
public class AuthFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext)
throws IOException {
String method = requestContext.getMethod();
String path = requestContext.getUriInfo().getPath();
String auth = requestContext.getHeaderString("authorization");
if (auth == null)
throw new AuthenticationException("Too bad");
//how to import that ^
auth = auth.replaceFirst("[Bb]asic ", "");
String entry = new String(Base64.decode(auth));
if (!entry.equals("admin:password"))
throw new AuthenticationException("Too bad");
}
}