Advertisement
Guest User

Untitled

a guest
Jan 11th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. package br.com.audilog.logpaper.jersey;
  2.  
  3. import java.lang.reflect.Method;
  4. import java.util.Arrays;
  5. import java.util.HashSet;
  6. import java.util.List;
  7. import java.util.Set;
  8.  
  9. import javax.annotation.security.DenyAll;
  10. import javax.annotation.security.PermitAll;
  11. import javax.annotation.security.RolesAllowed;
  12. import javax.inject.Singleton;
  13. import javax.ws.rs.container.ContainerRequestContext;
  14. import javax.ws.rs.container.ContainerRequestFilter;
  15. import javax.ws.rs.container.ResourceInfo;
  16. import javax.ws.rs.core.Context;
  17. import javax.ws.rs.core.MultivaluedMap;
  18. import javax.ws.rs.core.Response;
  19. import javax.ws.rs.ext.Provider;
  20.  
  21. /**
  22. * This filter verify the access permissions for a user
  23. * based on username and passowrd provided in request
  24. * */
  25. @Singleton
  26. @Provider
  27. public class AuthenticationFilter implements ContainerRequestFilter {
  28.  
  29. @Context
  30. private ResourceInfo resourceInfo;
  31.  
  32. private static final String AUTHORIZATION_PROPERTY = "Authorization";
  33. private static final String AUTHENTICATION_SCHEME = "Basic";
  34. private static final Response ACCESS_DENIED = Response.status(Response.Status.UNAUTHORIZED)
  35. .entity("Usuário não tem permissão para acessar este recurso.").build();
  36. private static final Response ACCESS_FORBIDDEN = Response.status(Response.Status.FORBIDDEN)
  37. .entity("Acesso bloqueado a todos os usuários.").build();
  38.  
  39. /* (non-Javadoc)
  40. * @see javax.ws.rs.container.ContainerRequestFilter#filter(javax.ws.rs.container.ContainerRequestContext)
  41. */
  42. @Override
  43. public void filter(ContainerRequestContext requestContext)
  44. {
  45.  
  46. Method method = resourceInfo.getResourceMethod();
  47.  
  48. /*
  49. * Verifica se o serviço tem acesso publico,
  50. * se tiver nao é necessario a validacao da senha.
  51. */
  52. if(!method.isAnnotationPresent(PermitAll.class))
  53. {
  54. /*
  55. * Verifica se o acesso esta negado para todos.
  56. * Se estiver retorna acesso bloqueado e para a validacao.
  57. */
  58. if(method.isAnnotationPresent(DenyAll.class))
  59. {
  60. requestContext.abortWith(ACCESS_FORBIDDEN);
  61. return;
  62. }
  63.  
  64. //Recupera os dados do header da requisicao
  65. final MultivaluedMap<String, String> headers = requestContext.getHeaders();
  66.  
  67. //Recupera a autorizacao do header da requisicao.
  68. final List<String> authorization = headers.get(AUTHORIZATION_PROPERTY);
  69. System.out.println("Authorization: " + authorization);
  70.  
  71. /*
  72. * Se nao existir uma propriedade de autorizacao
  73. * bloqueia acesso ao servico.
  74. */
  75. if(authorization == null || authorization.isEmpty())
  76. {
  77. requestContext.abortWith(ACCESS_DENIED);
  78. return;
  79. }
  80.  
  81. //Recupera o token de autorizacao Base64
  82. // final String encodedUserPassword = authorization.get(0).replaceFirst(AUTHENTICATION_SCHEME + " ", "");
  83.  
  84. //Decodifica usuario e senha
  85. // String usernameAndPassword = new String(Base64.decode(encodedUserPassword.getBytes()));;
  86.  
  87. //Recupera usuario e senha do token
  88. // final StringTokenizer tokenizer = new StringTokenizer(usernameAndPassword, ":");
  89. // final String username = tokenizer.nextToken();
  90. // final String password = tokenizer.nextToken();
  91.  
  92. /*
  93. * Verifica se o servico tem regras de acesso definidas.
  94. */
  95. if(method.isAnnotationPresent(RolesAllowed.class))
  96. {
  97. RolesAllowed rolesAnnotation = method.getAnnotation(RolesAllowed.class);
  98. Set<String> rolesSet = new HashSet<String>(Arrays.asList(rolesAnnotation.value()));
  99.  
  100. /*
  101. * Verifica se as informacoes de usuarios sao validas.
  102. */
  103. // if(!isUserAllowed(username, password, rolesSet))
  104. if(!isUserAllowed())
  105. {
  106. requestContext.abortWith(ACCESS_DENIED);
  107. return;
  108. }
  109. }
  110. }
  111. }
  112.  
  113. /**
  114. * Verifica se as informacoes do usuario sao validas.
  115. *
  116. * @param username
  117. * @param password
  118. * @param rolesSet
  119. * @return
  120. */
  121. // private boolean isUserAllowed(final String username, final String password, final Set<String> rolesSet)
  122. // {
  123. // boolean isAllowed = false;
  124. //
  125. // /*
  126. // * TODO Recuperar informacoes de usuario de senha da base de dados
  127. // * bem como politicas de acesso.
  128. // */
  129. // if(username.equals("1") && password.equals("c4ca4238a0b923820dcc509a6f75849b"))
  130. // {
  131. // String userRole = "ADMIN";
  132. //
  133. // //Step 2. Verify user role
  134. // if(rolesSet.contains(userRole))
  135. // {
  136. // isAllowed = true;
  137. // }
  138. // }
  139. // return isAllowed;
  140. // }
  141.  
  142. /**
  143. * Verifica se as informacoes do usuario sao validas.
  144. *
  145. * @param username
  146. * @param password
  147. * @param rolesSet
  148. * @return
  149. */
  150. private boolean isUserAllowed()
  151. {
  152. return true;
  153. }
  154.  
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement