Guest User

Untitled

a guest
Jan 8th, 2018
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. Method method = resourceInfo.getResourceMethod();
  2.  
  3. //Access allowed for all
  4. if( ! method.isAnnotationPresent(PermitAll.class))
  5. {
  6. //Access denied for all
  7. if(method.isAnnotationPresent(DenyAll.class))
  8. {
  9. requestContext.abortWith(ACCESS_FORBIDDEN);
  10. return;
  11. }
  12.  
  13. //Get request headers
  14. final MultivaluedMap<String, String> headers = requestContext.getHeaders();
  15.  
  16. //Fetch authorization header
  17. final List<String> authorization = headers.get(AUTHORIZATION_PROPERTY);
  18.  
  19. //If no authorization information present; block access
  20. if(authorization == null || authorization.isEmpty())
  21. {
  22. requestContext.abortWith(ACCESS_DENIED);
  23. return;
  24. }
  25.  
  26. //Get encoded username and password
  27. final String encodedUserPassword = authorization.get(0).replaceFirst(AUTHENTICATION_SCHEME + " ", "");
  28.  
  29. //Decode username and password
  30. String usernameAndPassword;
  31. // usernameAndPassword = new String(Base64.decode(encodedUserPassword.getBytes()));
  32. usernameAndPassword = new String(encodedUserPassword.getBytes());
  33. System.out.println("usernameAndPassword: " + usernameAndPassword);
  34. //Split username and password tokens
  35. final StringTokenizer tokenizer = new StringTokenizer(usernameAndPassword, ":");
  36. final String username = tokenizer.nextToken();
  37. final String password = tokenizer.nextToken();
  38.  
  39. //Verifying Username and password
  40. System.out.println(username);
  41. System.out.println(password);
  42.  
  43. //Verify user access
  44. if(method.isAnnotationPresent(RolesAllowed.class))
  45. {
  46. System.out.println("errorcito1");
  47. RolesAllowed rolesAnnotation = method.getAnnotation(RolesAllowed.class);
  48. System.out.println("errorcito2");
  49. Set<String> rolesSet = new HashSet<>(Arrays.asList(rolesAnnotation.value()));
  50. System.out.println("errorcito3");
  51. //Is user valid?
  52. if( ! isUserAllowed(username, password, rolesSet))
  53. {
  54. System.out.println("errorcito4");
  55. requestContext.abortWith(ACCESS_DENIED);
  56. System.out.println("errorcito5");
  57. return;
  58. }
  59. System.out.println("errorcito6");
  60. }
  61. }
Add Comment
Please, Sign In to add comment