Reputation: 3621
I've googled and tried but I cant get Providers to work in my project.
POM.XML:
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>9.0.0.M1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
My Filter:
package xxxxxxx
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.DatatypeConverter;
@Provider
public class BasicAuthFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext)
throws IOException {
String authorization = requestContext.getHeaderString("Authorization");
if (!auth(authorization)) {
requestContext.abortWith(Response
.status(Response.Status.UNAUTHORIZED)
.entity("User cannot access the resource.")
.build());
}
}
private Boolean auth(String authorization)
throws UnsupportedEncodingException {
if (authorization != null && authorization.startsWith("Basic")) {
String base64Credentials = authorization
.substring("Basic".length()).trim();
byte[] decoded = DatatypeConverter
.parseBase64Binary(base64Credentials);
final String[] values = new String(decoded, "UTF-8").split(":", 2);
if (values.length == 2 && values[0].equals("usuario")
&& values[1].equals("senha")) {
return true;
}
}
return false;
}
}
My ResourceConfig class:
import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;
import xxx.providers.BasicAuthFilter;
import xxx.resource.LogParserResource;
@ApplicationPath("rest")
public class ApplicationConfig extends ResourceConfig {
public ApplicationConfig() {
register(BasicAuthFilter.class);
register(MyResource.class);
}
}
Filter is not triggered. Anything wrong in my configuration? WEB.XML is empty and the Resource is working fine. Any suggestions?? Thanks.
Upvotes: 0
Views: 1381
Reputation: 4724
i noticed, that this dependency is not the version of the other ones:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.19</version>
</dependency>
you could try to use:
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.22.1</version>
</dependency>
and then notice the documentation:
Important
Servlet 2.x API does not provide a way how to programmatically read the filter mappings. To make application deployed using filter work correctly, either Servlet 3.x container must be used (jersey-container-servlet instead of jersey-container-servlet-core), or the context path of the app needs to be defined using init parameter jersey.config.servlet.filter.contextPath.
see: https://jersey.java.net/documentation/latest/deployment.html#deployment.servlet
Upvotes: 1