📄 mbasicauthenticationmanager.java
字号:
package net.jumperz.app.MGuardian.plugin;
import java.net.*;
import java.io.*;
import java.util.*;
import net.jumperz.net.*;
import net.jumperz.util.*;
import net.jumperz.app.MGuardian.*;
public class MBasicAuthenticationManager
extends MGuardianPlugin
{
private boolean permanent;
private String blackListFileName;
private Set blackList;
private Map ipCountMap = new HashMap();
private int maxDenial;
private boolean logFailure;
private boolean logPassword;
private static final String DEFAULT_MAX_DENIAL = "0";
private static final String DEFAULT_PERMANENT = "false";
private static final String DEFAULT_LOG_FAILURE = "true";
private static final String DEFAULT_LOG_PASSWORD = "false";
//--------------------------------------------------------------------------------
public void startup()
throws IOException
{
permanent = control.getProperty( "basicAuthenticationManager.permanent", DEFAULT_PERMANENT ).equals( "true" );
logFailure = control.getProperty( "basicAuthenticationManager.logFailure", DEFAULT_LOG_FAILURE ).equals( "true" );
logPassword = control.getProperty( "basicAuthenticationManager.logPassword", DEFAULT_LOG_PASSWORD ).equals( "true" );
maxDenial = Integer.parseInt( control.getProperty( "basicAuthenticationManager.maxDenial", DEFAULT_MAX_DENIAL ) );
if( permanent )
{
blackListFileName = control.getProperty( "basicAuthenticationManager.blackListFileName" );
blackList = new HashSet( MStringUtil.loadListFromFile( blackListFileName ) );
}
else
{
blackList = new HashSet();
}
}
//--------------------------------------------------------------------------------
public Map execute( Map sessionInfo )
{
if( sessionInfo.get( "response" ) == null )
{
return processRequest( sessionInfo );
}
else
{
return processResponse( sessionInfo );
}
}
//--------------------------------------------------------------------------------
private Map processRequest( Map sessionInfo )
{
Map pluginResult = new HashMap();
Socket clientSideSocket = ( Socket )sessionInfo.get( "clientSideSocket" );
String clientIp = clientSideSocket.getInetAddress().getHostAddress();
if( blackList.contains( clientIp ) )
{
pluginResult.put( "block", new Boolean( true ) );
}
String authValue = getAuthValue( sessionInfo );
if( logFailure && MStringUtil.isPrintable( authValue ) )
{
pluginResult.put( "message", authValue );
}
return pluginResult;
}
//--------------------------------------------------------------------------------
private synchronized Map processResponse( Map sessionInfo )
{
Socket clientSideSocket = ( Socket )sessionInfo.get( "clientSideSocket" );
String clientIp = clientSideSocket.getInetAddress().getHostAddress();
if( ipCountMap.containsKey( clientIp ) )
{
MCount count = ( MCount )ipCountMap.get( clientIp );
count.inc();
if( maxDenial != 0
&& count.getValue() > maxDenial
)
{
blackList.add( clientIp );
ipCountMap.remove( clientIp );
}
}
else
{
MCount count = new MCount();
count.inc();
ipCountMap.put( clientIp, count );
}
String authValue = getAuthValue( sessionInfo );
Map pluginResult = new HashMap();
if( logFailure && MStringUtil.isPrintable( authValue ) )
{
pluginResult.put( "message", authValue );
pluginResult.put( "log", new Boolean( true ) );
}
return pluginResult;
}
//--------------------------------------------------------------------------------
private String getAuthValue( Map sessionInfo )
{
MHttpRequest request = ( MHttpRequest )sessionInfo.get( "request" );
String authValue = request.getHeaderValue( "Authorization" );
authValue = Base64.decodeToString( authValue.split( " " )[ 1 ] );
if( !logPassword )
{
authValue = authValue.split( ":" )[ 0 ];
}
return authValue;
}
//--------------------------------------------------------------------------------
public void shutdown()
{
if( permanent )
{
try
{
MStringUtil.saveCollectionToFile( blackList, blackListFileName );
}
catch( IOException e )
{
e.printStackTrace();
}
}
}
//--------------------------------------------------------------------------------
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -