-
-
Notifications
You must be signed in to change notification settings - Fork 575
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
169 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...n/java/com/github/monkeywie/proxyee/server/auth/BasicHttpProxyAuthenticationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.github.monkeywie.proxyee.server.auth; | ||
|
||
import java.util.Base64; | ||
|
||
/** | ||
* @Author LiWei | ||
* @Description | ||
* @Date 2021/1/15 14:12 | ||
*/ | ||
public abstract class BasicHttpProxyAuthenticationProvider implements HttpProxyAuthenticationProvider { | ||
|
||
public static final String AUTH_TYPE_BASIC = "Basic"; | ||
public static final String AUTH_REALM_BASIC = "Access to the staging site"; | ||
|
||
public String authType() { | ||
return AUTH_TYPE_BASIC; | ||
} | ||
|
||
public String authRealm() { | ||
return AUTH_REALM_BASIC; | ||
} | ||
|
||
protected abstract boolean authenticate(String usr, String pwd); | ||
|
||
public boolean authenticate(String authorization) { | ||
String usr = ""; | ||
String pwd = ""; | ||
if (authorization != null && authorization.length() > 0) { | ||
String token = authorization.substring(AUTH_TYPE_BASIC.length() + 1); | ||
String decode = new String(Base64.getDecoder().decode(token)); | ||
String[] arr = decode.split(":"); | ||
usr = arr[0]; | ||
if (arr.length >= 2) { | ||
pwd = arr[1]; | ||
} | ||
} | ||
return authenticate(usr, pwd); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/github/monkeywie/proxyee/server/auth/HttpProxyAuthenticationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.github.monkeywie.proxyee.server.auth; | ||
|
||
/** | ||
* @Author LiWei | ||
* @Description | ||
* @Date 2021/1/15 14:12 | ||
*/ | ||
public interface HttpProxyAuthenticationProvider { | ||
String authType(); | ||
|
||
String authRealm(); | ||
|
||
boolean authenticate(String authorization); | ||
} |
22 changes: 22 additions & 0 deletions
22
src/test/java/com/github/monkeywie/proxyee/AuthHttpProxyServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.github.monkeywie.proxyee; | ||
|
||
import com.github.monkeywie.proxyee.server.HttpProxyServer; | ||
import com.github.monkeywie.proxyee.server.HttpProxyServerConfig; | ||
import com.github.monkeywie.proxyee.server.auth.BasicHttpProxyAuthenticationProvider; | ||
|
||
public class AuthHttpProxyServer { | ||
|
||
// curl -i -x 127.0.0.1:9999 -U admin:123456 https://www.baidu.com | ||
public static void main(String[] args) throws Exception { | ||
HttpProxyServerConfig config = new HttpProxyServerConfig(); | ||
config.setAuthenticationProvider(new BasicHttpProxyAuthenticationProvider() { | ||
@Override | ||
protected boolean authenticate(String usr, String pwd) { | ||
return "admin".equals(usr) && "123456".equals(pwd); | ||
} | ||
}); | ||
new HttpProxyServer() | ||
.serverConfig(config) | ||
.start(9999); | ||
} | ||
} |