Guest User

Untitled

a guest
Sep 7th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. Basic Authentication Required Dialog
  2. <?php
  3. if (!isset($_SERVER['PHP_AUTH_USER'])) {
  4. header('WWW-Authenticate: Basic realm="My Realm"');
  5. header('HTTP/1.0 401 Unauthorized');
  6. echo 'Text to send if user hits Cancel button';
  7. exit;
  8. } else {
  9. echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
  10. echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>";
  11. }
  12. ?>
  13.  
  14. <Directory /usr/local/apache/htdocs/secret>
  15. AuthType Basic
  16. AuthName "Restricted Files"
  17. # (Following line optional)
  18. AuthBasicProvider file
  19. AuthUserFile /usr/local/apache/passwd/passwords
  20. Require user rbowen
  21. </Directory>
  22.  
  23. HttpServletResponse httpResponse = (HttpServletResponse) response;
  24. httpResponse.setHeader("WWW-Authenticate", "Basic realm="My Realm"");
  25. httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, "");
  26.  
  27. private boolean authenticateRequestOk(HttpServletRequest request)
  28. {
  29. String authorizationHeader = request.getHeader("Authorization");
  30.  
  31. if (authorizationHeader != null)
  32. {
  33. byte[] decodedUsernamePassword;
  34. try
  35. {
  36. decodedUsernamePassword = Base64.decode(authorizationHeader.substring("Basic ".length()));
  37. }
  38. catch (IOException e)
  39. {
  40. log.error("Error decoding authorization header "" + authorizationHeader + """, e);
  41. return false;
  42. }
  43.  
  44. String usernameAndPassword = new String(decodedUsernamePassword);
  45.  
  46. String username = StringUtils.substringBefore(usernameAndPassword, ":");
  47. String password = StringUtils.substringAfter(usernameAndPassword, ":");
  48.  
  49. if (USERNAME.equalsIgnoreCase(username) && PASSWORD.equalsIgnoreCase(password))
  50. {
  51. return true;
  52. }
  53. }
  54.  
  55. return false;
  56. }
Add Comment
Please, Sign In to add comment