Mercurial Hosting > luan
comparison http/src/luan/modules/web/AuthenticationHandler.java @ 493:1d082a0812e0
move web to http
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 15 May 2015 17:29:59 -0600 |
parents | web/src/luan/modules/web/AuthenticationHandler.java@e132b7a3d94c |
children |
comparison
equal
deleted
inserted
replaced
492:b36cc406d3d2 | 493:1d082a0812e0 |
---|---|
1 package luan.modules.web; | |
2 | |
3 import java.io.IOException; | |
4 import javax.servlet.ServletException; | |
5 import javax.servlet.http.HttpServlet; | |
6 import javax.servlet.http.HttpServletRequest; | |
7 import javax.servlet.http.HttpServletResponse; | |
8 import org.eclipse.jetty.server.Request; | |
9 import org.eclipse.jetty.server.handler.AbstractHandler; | |
10 import org.eclipse.jetty.util.B64Code; | |
11 | |
12 | |
13 public class AuthenticationHandler extends AbstractHandler { | |
14 private final String path; | |
15 private String password = "password"; | |
16 | |
17 public AuthenticationHandler(String path) { | |
18 this.path = path; | |
19 } | |
20 | |
21 public void setPassword(String password) { | |
22 this.password = password; | |
23 } | |
24 | |
25 public void handle(String target,Request baseRequest,HttpServletRequest request,HttpServletResponse response) | |
26 throws IOException | |
27 { | |
28 if( !target.startsWith(path) ) | |
29 return; | |
30 String pwd = getPassword(request); | |
31 if( password.equals(pwd) ) | |
32 return; | |
33 response.setHeader("WWW-Authenticate","Basic realm=\""+path+"\""); | |
34 response.sendError(HttpServletResponse.SC_UNAUTHORIZED); | |
35 baseRequest.setHandled(true); | |
36 } | |
37 | |
38 private static String getPassword(HttpServletRequest request) { | |
39 String auth = request.getHeader("Authorization"); | |
40 if( auth==null ) | |
41 return null; | |
42 String[] a = auth.split(" +"); | |
43 if( a.length != 2 ) | |
44 throw new RuntimeException("auth = "+auth); | |
45 if( !a[0].equals("Basic") ) | |
46 throw new RuntimeException("auth = "+auth); | |
47 auth = new String(B64Code.decode(a[1])); | |
48 a = auth.split(":"); | |
49 if( a.length != 2 ) | |
50 throw new RuntimeException("auth = "+auth); | |
51 return a[1]; | |
52 } | |
53 } |