-
-
Save leandrogilvalle/d5227d50651815179a80a12ac4112aa6 to your computer and use it in GitHub Desktop.
JAX-RS AutorizadorRequestFilter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.IOException; | |
| import javax.annotation.Priority; | |
| import javax.ws.rs.Priorities; | |
| import javax.ws.rs.container.ContainerRequestContext; | |
| import javax.ws.rs.container.ContainerRequestFilter; | |
| import javax.ws.rs.core.HttpHeaders; | |
| import javax.ws.rs.core.Response; | |
| import javax.ws.rs.core.Response.Status; | |
| import javax.ws.rs.ext.Provider; | |
| @Priority(Priorities.AUTHORIZATION) | |
| @Provider | |
| public class AutorizadorRequestFilter implements ContainerRequestFilter { | |
| @Override | |
| public void filter(ContainerRequestContext req) throws IOException { | |
| String auth = req.getHeaderString(HttpHeaders.AUTHORIZATION); | |
| if (auth == null || "".equals(auth.trim())) { | |
| sendUnauthorizedResponse(req); | |
| return; | |
| } | |
| if (!isTokenDeAutorizacaoValido(auth)) { | |
| sendUnauthorizedResponse(req); | |
| } | |
| } | |
| private boolean isTokenDeAutorizacaoValido(String auth) { | |
| //Logica de validar o token de autorizacao aqui... | |
| return true; | |
| } | |
| private void sendUnauthorizedResponse(ContainerRequestContext req) { | |
| req.abortWith(Response.status(Status.UNAUTHORIZED).build()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment