|
|
@@ -0,0 +1,30 @@
|
|
|
+package io.senslog.ws.filter;
|
|
|
+
|
|
|
+import io.senslog.ws.model.WSError;
|
|
|
+
|
|
|
+import javax.ws.rs.container.ContainerRequestContext;
|
|
|
+import javax.ws.rs.container.ContainerRequestFilter;
|
|
|
+import javax.ws.rs.container.PreMatching;
|
|
|
+import javax.ws.rs.core.Response;
|
|
|
+import javax.ws.rs.ext.Provider;
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
+import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
|
|
|
+
|
|
|
+// @PreMatching @Provider
|
|
|
+public class AuthFilter implements ContainerRequestFilter {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void filter(ContainerRequestContext requestContext) throws IOException {
|
|
|
+
|
|
|
+ String xAuth = requestContext.getHeaderString("x-auth");
|
|
|
+
|
|
|
+ if (xAuth == null || xAuth.isEmpty()) {
|
|
|
+ requestContext.abortWith(Response
|
|
|
+ .status(Response.Status.UNAUTHORIZED)
|
|
|
+ .type(APPLICATION_JSON)
|
|
|
+ .entity(new WSError("Unauthorized. You have to login at first.").toJson())
|
|
|
+ .build());
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|