Mercurial Hosting > luan
comparison src/luan/modules/http/AuthenticationHandler.java @ 1000:32d4b569567c
simplify handle()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 19 Oct 2016 04:22:51 -0600 |
parents | b3176fd168bf |
children | 0d884377e923 |
comparison
equal
deleted
inserted
replaced
999:74b9daf2826c | 1000:32d4b569567c |
---|---|
1 package luan.modules.http; | 1 package luan.modules.http; |
2 | 2 |
3 import java.io.IOException; | 3 import java.io.IOException; |
4 import java.util.Base64; | 4 import java.util.Base64; |
5 import javax.servlet.http.HttpServletRequest; | |
6 import javax.servlet.http.HttpServletResponse; | 5 import javax.servlet.http.HttpServletResponse; |
7 import org.eclipse.jetty.server.Request; | 6 import org.eclipse.jetty.server.Request; |
8 import org.eclipse.jetty.server.handler.AbstractHandler; | 7 import org.eclipse.jetty.server.handler.AbstractHandler; |
9 | 8 |
10 | 9 |
18 | 17 |
19 public void setPassword(String password) { | 18 public void setPassword(String password) { |
20 this.password = password; | 19 this.password = password; |
21 } | 20 } |
22 | 21 |
23 public void handle(String target,Request baseRequest,HttpServletRequest request,HttpServletResponse response) | 22 @Override |
23 public void handle(String target,Request request,HttpServletResponse response) | |
24 throws IOException | 24 throws IOException |
25 { | 25 { |
26 if( !target.startsWith(path) ) | 26 if( !target.startsWith(path) ) |
27 return; | 27 return; |
28 String pwd = getPassword(request); | 28 String pwd = getPassword(request); |
29 if( password.equals(pwd) ) | 29 if( password.equals(pwd) ) |
30 return; | 30 return; |
31 response.setHeader("WWW-Authenticate","Basic realm=\""+path+"\""); | 31 response.setHeader("WWW-Authenticate","Basic realm=\""+path+"\""); |
32 response.sendError(HttpServletResponse.SC_UNAUTHORIZED); | 32 response.sendError(HttpServletResponse.SC_UNAUTHORIZED); |
33 baseRequest.setHandled(true); | 33 request.setHandled(true); |
34 } | 34 } |
35 | 35 |
36 private static String getPassword(HttpServletRequest request) { | 36 private static String getPassword(Request request) { |
37 String auth = request.getHeader("Authorization"); | 37 String auth = request.getHeader("Authorization"); |
38 if( auth==null ) | 38 if( auth==null ) |
39 return null; | 39 return null; |
40 String[] a = auth.split(" +"); | 40 String[] a = auth.split(" +"); |
41 if( a.length != 2 ) | 41 if( a.length != 2 ) |