measyauth.java

来自「httptunnel.jar httptunnel java 源码」· Java 代码 · 共 82 行

JAVA
82
字号
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 + =
减小字号Ctrl + -
显示快捷键?