Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Basic Authentication Required Dialog
- <?php
- if (!isset($_SERVER['PHP_AUTH_USER'])) {
- header('WWW-Authenticate: Basic realm="My Realm"');
- header('HTTP/1.0 401 Unauthorized');
- echo 'Text to send if user hits Cancel button';
- exit;
- } else {
- echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
- echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>";
- }
- ?>
- <Directory /usr/local/apache/htdocs/secret>
- AuthType Basic
- AuthName "Restricted Files"
- # (Following line optional)
- AuthBasicProvider file
- AuthUserFile /usr/local/apache/passwd/passwords
- Require user rbowen
- </Directory>
- HttpServletResponse httpResponse = (HttpServletResponse) response;
- httpResponse.setHeader("WWW-Authenticate", "Basic realm="My Realm"");
- httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, "");
- private boolean authenticateRequestOk(HttpServletRequest request)
- {
- String authorizationHeader = request.getHeader("Authorization");
- if (authorizationHeader != null)
- {
- byte[] decodedUsernamePassword;
- try
- {
- decodedUsernamePassword = Base64.decode(authorizationHeader.substring("Basic ".length()));
- }
- catch (IOException e)
- {
- log.error("Error decoding authorization header "" + authorizationHeader + """, e);
- return false;
- }
- String usernameAndPassword = new String(decodedUsernamePassword);
- String username = StringUtils.substringBefore(usernameAndPassword, ":");
- String password = StringUtils.substringAfter(usernameAndPassword, ":");
- if (USERNAME.equalsIgnoreCase(username) && PASSWORD.equalsIgnoreCase(password))
- {
- return true;
- }
- }
- return false;
- }
Add Comment
Please, Sign In to add comment