1 30 31 package org.apache.commons.httpclient; 32 33 import java.io.*; 34 import javax.servlet.*; 35 import javax.servlet.http.*; 36 import java.util.*; 37 38 public class BasicAuthServlet extends MultiMethodServlet { 39 private static final HashMap creds = new HashMap(); 42 static { 43 creds.put("dW5hbWU6cGFzc3dk","uname:passwd"); 44 creds.put("amFrYXJ0YTpjb21tb25z","jakarta:commons"); 45 creds.put("amFrYXJ0YS5hcGFjaGUub3JnL2NvbW1vbnM6aHR0cGNsaWVudA==","jakarta.apache.org/commons:httpclient"); 46 } 47 48 protected void genericService(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 49 String auth = request.getHeader("authorization"); 50 if(null == auth) { 51 sendUnauthenticated(request, response); 52 } else { 53 String role = (String )(creds.get(auth.substring("basic ".length(),auth.length()))); 54 if(null == role) { 55 sendUnauthorized(request, response, auth); 56 } else { 57 sendAuthorized(request, response, role); 58 } 59 } 60 } 61 62 protected void doHead(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 63 String auth = request.getHeader("authorization"); 64 if(null == auth) { 65 sendUnauthenticated(request, response); 66 } else { 67 String role = (String )(creds.get(auth.substring("basic ".length(),auth.length()))); 68 if(null == role) { 69 sendUnauthorized(request, response, auth); 70 } 71 } 72 } 73 74 private void sendUnauthenticated(HttpServletRequest request, HttpServletResponse response) throws IOException { 75 response.setStatus(response.SC_UNAUTHORIZED); 76 response.addHeader("www-authenticate","Basic realm=\"BasicAuthServlet\""); 77 PrintWriter out = response.getWriter(); 78 response.setContentType("text/html"); 79 out.println("<html>"); 80 out.println("<head><title>BasicAuth Servlet: " + request.getMethod() + "</title></head>"); 81 out.println("<body>"); 82 out.println("<p>This is a response to an HTTP " + request.getMethod() + " request.</p>"); 83 out.println("<p>Not authorized.</p>"); 84 out.println("</body>"); 85 out.println("</html>"); 86 } 87 88 private void sendUnauthorized(HttpServletRequest request, HttpServletResponse response, String auth) throws IOException { 89 response.setStatus(response.SC_UNAUTHORIZED); 90 response.addHeader("www-authenticate","Basic realm=\"BasicAuthServlet\""); 91 PrintWriter out = response.getWriter(); 92 response.setContentType("text/html"); 93 out.println("<html>"); 94 out.println("<head><title>BasicAuth Servlet: " + request.getMethod() + "</title></head>"); 95 out.println("<body>"); 96 out.println("<p>This is a response to an HTTP " + request.getMethod() + " request.</p>"); 97 out.println("<p>Not authorized. \"" + auth + "\" not recognized.</p>"); 98 out.println("</body>"); 99 out.println("</html>"); 100 } 101 102 private void sendAuthorized(HttpServletRequest request, HttpServletResponse response, String role) throws IOException { 103 response.setContentType("text/html"); 104 PrintWriter out = response.getWriter(); 105 out.println("<html>"); 106 out.println("<head><title>BasicAuth Servlet: " + request.getMethod() + "</title></head>"); 107 out.println("<body>"); 108 out.println("<p>This is a response to an HTTP " + request.getMethod() + " request.</p>"); 109 out.println("<p>You have authenticated as \"" + role + "\"</p>"); 110 out.println("</body>"); 111 out.println("</html>"); 112 } 113 } 114 115 | Popular Tags |