Advertisement
Guest User

Untitled

a guest
Feb 16th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. package com.howtodoinjava.jersey.provider;
  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. import java.util.StringTokenizer;
  9.  
  10. import javax.annotation.security.DenyAll;
  11. import javax.annotation.security.PermitAll;
  12. import javax.annotation.security.RolesAllowed;
  13. import javax.ws.rs.container.ContainerRequestContext;
  14. import javax.ws.rs.container.ResourceInfo;
  15. import javax.ws.rs.core.Context;
  16. import javax.ws.rs.core.MultivaluedMap;
  17. import javax.ws.rs.core.Response;
  18. import javax.ws.rs.ext.Provider;
  19.  
  20. import org.glassfish.jersey.internal.util.Base64;
  21.  
  22. /**
  23. * This filter verify the access permissions for a user
  24. * based on username and passowrd provided in request
  25. * */
  26. @Provider
  27. public class AuthenticationFilter implements javax.ws.rs.container.ContainerRequestFilter
  28. {
  29.  
  30. @Context
  31. private ResourceInfo resourceInfo;
  32.  
  33. private static final String AUTHORIZATION_PROPERTY = "Authorization";
  34. private static final String AUTHENTICATION_SCHEME = "Basic";
  35. private static final Response ACCESS_DENIED = Response.status(Response.Status.UNAUTHORIZED)
  36. .entity("You cannot access this resource").build();
  37. private static final Response ACCESS_FORBIDDEN = Response.status(Response.Status.FORBIDDEN)
  38. .entity("Access blocked for all users !!").build();
  39.  
  40. @Override
  41. public void filter(ContainerRequestContext requestContext)
  42. {
  43. Method method = resourceInfo.getResourceMethod();
  44. //Access allowed for all
  45. if( ! method.isAnnotationPresent(PermitAll.class))
  46. {
  47. //Access denied for all
  48. if(method.isAnnotationPresent(DenyAll.class))
  49. {
  50. requestContext.abortWith(ACCESS_FORBIDDEN);
  51. return;
  52. }
  53.  
  54. //Get request headers
  55. final MultivaluedMap<String, String> headers = requestContext.getHeaders();
  56.  
  57. //Fetch authorization header
  58. final List<String> authorization = headers.get(AUTHORIZATION_PROPERTY);
  59.  
  60. //If no authorization information present; block access
  61. if(authorization == null || authorization.isEmpty())
  62. {
  63. requestContext.abortWith(ACCESS_DENIED);
  64. return;
  65. }
  66.  
  67. //Get encoded username and password
  68. final String encodedUserPassword = authorization.get(0).replaceFirst(AUTHENTICATION_SCHEME + " ", "");
  69.  
  70. //Decode username and password
  71. String usernameAndPassword = new String(Base64.decode(encodedUserPassword.getBytes()));;
  72.  
  73. //Split username and password tokens
  74. final StringTokenizer tokenizer = new StringTokenizer(usernameAndPassword, ":");
  75. final String username = tokenizer.nextToken();
  76. final String password = tokenizer.nextToken();
  77.  
  78. //Verifying Username and password
  79. System.out.println(username);
  80. System.out.println(password);
  81.  
  82. //Verify user access
  83. if(method.isAnnotationPresent(RolesAllowed.class))
  84. {
  85. RolesAllowed rolesAnnotation = method.getAnnotation(RolesAllowed.class);
  86. Set<String> rolesSet = new HashSet<String>(Arrays.asList(rolesAnnotation.value()));
  87.  
  88. //Is user valid?
  89. if( ! isUserAllowed(username, password, rolesSet))
  90. {
  91. requestContext.abortWith(ACCESS_DENIED);
  92. return;
  93. }
  94. }
  95. }
  96. }
  97. private boolean isUserAllowed(final String username, final String password, final Set<String> rolesSet)
  98. {
  99. boolean isAllowed = false;
  100.  
  101. //Step 1. Fetch password from database and match with password in argument
  102. //If both match then get the defined role for user from database and continue; else return isAllowed [false]
  103. //Access the database and do this part yourself
  104. //String userRole = userMgr.getUserRole(username);
  105.  
  106. if(username.equals("howtodoinjava") && password.equals("password"))
  107. {
  108. String userRole = "ADMIN";
  109.  
  110. //Step 2. Verify user role
  111. if(rolesSet.contains(userRole))
  112. {
  113. isAllowed = true;
  114. }
  115. }
  116. return isAllowed;
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement