coderaf/spiderman-security-bundle
Installation Add the package via Composer:
composer require coderaf/spiderman-security-bundle
Register the bundle in config/app.php under providers:
Coderaf\SpiderManSecurityBundle\SpiderManSecurityServiceProvider::class,
Publish the config file:
php artisan vendor:publish --provider="Coderaf\SpiderManSecurityBundle\SpiderManSecurityServiceProvider" --tag=config
Basic Configuration
Edit config/spiderman-security.php to define:
SpiderManSecurityMiddleware in the stack).First Use Case: IP Whitelisting Define allowed IPs in the config:
'ip_whitelist' => [
'192.168.1.1',
'127.0.0.1',
],
Place the middleware in app/Http/Kernel.php:
protected $middleware = [
\Coderaf\SpiderManSecurityBundle\Http\Middleware\SpiderManSecurityMiddleware::class,
// ... other middleware
];
Test by accessing a route from a non-whitelisted IP—it should be blocked.
Dynamic Rules: Extend the bundle by creating custom rules (e.g., user-agent blocking, geolocation restrictions).
// app/Rules/CustomRule.php
namespace App\Rules;
use Coderaf\SpiderManSecurityBundle\Contracts\SecurityRuleInterface;
class CustomRule implements SecurityRuleInterface {
public function check($request) {
return $request->userAgent() !== 'BadBot/1.0';
}
}
Register in spiderman-security.php:
'custom_rules' => [
\App\Rules\CustomRule::class,
],
Priority Handling: Rules are evaluated in order. Place stricter rules (e.g., IP whitelist) before looser ones (e.g., rate limiting).
Configure in spiderman-security.php:
'rate_limits' => [
'login_attempts' => [
'max_attempts' => 5,
'time_window' => '10 minutes',
'key' => 'login_attempts',
],
],
Use the RateLimit trait in controllers:
use Coderaf\SpiderManSecurityBundle\Traits\RateLimit;
class LoginController extends Controller {
use RateLimit;
public function attemptLogin() {
$this->checkRateLimit('login_attempts');
// ... login logic
}
}
Route::middleware(['web', 'spiderman.security'])->group(function () {
// Admin routes
});
namespace App\Http\Middleware;
use Coderaf\SpiderManSecurityBundle\Http\Middleware\SpiderManSecurityMiddleware;
class CustomSecurityMiddleware extends SpiderManSecurityMiddleware {
protected function handleBlockedRequest($request, $response) {
// Custom response (e.g., redirect to CAPTCHA)
return redirect()->route('captcha');
}
}
'logging' => [
'enabled' => true,
'path' => storage_path('logs/spiderman-security.log'),
],
spiderman_security_logs table for patterns:
$blockedIps = \DB::table('spiderman_security_logs')
->where('action', 'blocked')
->groupBy('ip')
->orderBy('count', 'desc')
->take(10)
->get();
Middleware Order Matters
SpiderManSecurityMiddleware before authentication middleware (e.g., auth). Otherwise, blocked requests may bypass checks.app/Http/Kernel.php):
protected $middlewareGroups = [
'web' => [
\Coderaf\SpiderManSecurityBundle\Http\Middleware\SpiderManSecurityMiddleware::class,
\App\Http\Middleware\TrustProxies::class,
// ... other middleware
],
];
Rate Limit Key Collisions
key in rate_limits is unique per route/action to avoid shared counters.user_id or ip as part of the key:
'key' => 'login_attempts_{$request->ip()}_{$request->user()->id}',
Config Overrides
php artisan config:clear
Performance Impact
ip_whitelist) in a static array.Enable Verbose Logging
Temporarily set 'debug' => true in config to log rule evaluations:
'logging' => [
'debug' => true,
],
Check Middleware Execution Add a debug endpoint to test rules:
Route::get('/debug-security', function () {
$rules = app('spiderman.security.rules');
foreach ($rules as $rule) {
dd($rule->check(request()));
}
});
Database Locks If using database-backed rate limiting, ensure transactions are short to avoid locks:
\DB::transaction(function () use ($request) {
$this->checkRateLimit('login_attempts');
});
Custom Rule Storage Override the default rule storage (e.g., switch from database to Redis):
// app/Providers/SpiderManSecurityServiceProvider.php
public function register() {
$this->app->bind(
\Coderaf\SpiderManSecurityBundle\Contracts\RuleStorageInterface::class,
\App\Services\RedisRuleStorage::class
);
}
Event Listeners
Listen for security events (e.g., SpiderManSecurity.Blocked):
// app/Providers/EventServiceProvider.php
protected $listen = [
\Coderaf\SpiderManSecurityBundle\Events\SecurityBlocked::class => [
\App\Listeners\LogBlockedAttempt::class,
],
];
API-Specific Rules
For APIs, combine with Laravel's throttle middleware:
Route::middleware(['throttle:60,1', 'spiderman.security'])->group(function () {
// API routes
});
How can I help you explore Laravel packages today?