Installation:
composer require crocos/security-bundle:dev-master
Add to AppKernel.php:
new Crocos\SecurityBundle\CrocosSecurityBundle(),
First Use Case:
@Secure to enforce authentication:
use Crocos\SecurityBundle\Annotation\Secure;
class DashboardController extends Controller
{
/**
* @Secure()
*/
public function indexAction()
{
// Only accessible to authenticated users
}
}
Configuration:
app/config/security.yml (minimal example):
security:
firewalls:
main:
anonymous: ~
Role-Based Access:
/**
* @Secure(roles={"ROLE_ADMIN"})
*/
public function adminAction()
{
// Only accessible to ADMIN users
}
Method-Level Security:
@Secure to individual methods (no global firewall needed for simple cases).Voter interfaces for custom logic.Login/Logout Handling:
$this->get('security.context')->setToken($authToken); // Login
$this->get('security.context')->setToken(null); // Logout
Symfony Forms: Use CrocosSecurityBundle's SecurityContext to validate roles in form type classes:
public function buildForm(FormBuilderInterface $builder, array $options)
{
if (!$this->getSecurityContext()->isGranted('ROLE_ADMIN')) {
throw new \RuntimeException('Unauthorized');
}
}
Event Listeners: Extend security logic via security.interactive_login/security.authentication_success events.
No Built-in CSRF Protection:
SecurityBundle, this bundle does not include CSRF tokens. Use Symfony’s CsrfTokenManager separately.Deprecated Symfony 2.x:
Symfony\SecurityBundle or api-platform/security.No Automatic Logout:
SecurityBundle, this bundle does not handle session expiration or automatic logout. Implement manually:
$this->get('session')->invalidate();
Annotation Parsing Errors:
AnnotationReader. Clear cache (php bin/console cache:clear) if issues arise.Token Context:
$this->get('security.context')->getToken()->getRoles();
Custom Annotations:
Crocos\SecurityBundle\Annotation\Secure to add metadata (e.g., @Secure(ip="192.168.1.0/24")).Voters:
Crocos\SecurityBundle\Security\Voter\VoterInterface for custom access logic.Authentication Providers:
Crocos\SecurityBundle\Security\Authentication\Provider\AuthenticationProviderInterface for custom auth backends.How can I help you explore Laravel packages today?