📄 measyauth.java
字号:
package net.jumperz.app.MGuardian.plugin;
import java.io.IOException;
import java.util.*;
import net.jumperz.util.*;
import net.jumperz.net.*;
public class MEasyAuth
extends MGuardianPlugin
{
private Map fileAuthMap = new HashMap();
private static final String STATUS_LINE = "HTTP/1.1 401 Authorization Required";
private static final String REALM_LINE = "Basic realm=\"Authentication\"";
private static final String BODY_STRING = "Authorization Required.";
//--------------------------------------------------------------------------------
public Map execute( Map sessionInfo )
throws IOException
{
if( !sessionInfo.containsKey( "arg" ) )
{
throw new IOException( "arg not defined." );
}
String fileName = ( String )sessionInfo.get( "arg" );
Set authSet = getAuthSet( fileName );
MHttpRequest request = ( MHttpRequest )sessionInfo.get( "request" );
boolean pass = false;
if( !request.headerExists( "Authorization" ) )
{
pass = false;
}
else
{
String authValue = request.getHeaderValue( "Authorization" );
authValue = Base64.decodeToString( authValue.split( " " )[ 1 ] );
if( authSet.contains( authValue ) )
{
pass = true;
}
else
{
pass = false;
}
}
if( !pass )
{
MHttpResponse response = new MHttpResponse();
response.setStatusLine( STATUS_LINE );
response.addHeaderValue( "WWW-Authenticate", REALM_LINE );
response.setBody( BODY_STRING );
Map pluginResult = new HashMap( 1 );
pluginResult.put( "response", response );
return pluginResult;
}
else
{
return null;
}
}
//--------------------------------------------------------------------------------
private Set getAuthSet( String fileName )
throws IOException
{
if( fileAuthMap.containsKey( fileName ) )
{
return ( Set )fileAuthMap.get( fileName );
}
else
{
Set authSet = MStringUtil.loadSetFromFile( fileName );
fileAuthMap.put( fileName, authSet );
return authSet;
}
}
//--------------------------------------------------------------------------------
private void sendNotify( String eventName )
{
System.out.println( eventName );
}
//--------------------------------------------------------------------------------
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -