Security Setup for Symfony2 Without login

Active3 hr before
Viewed126 times

9 Answers


Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,Thanks for contributing an answer to Stack Overflow!, Stack Overflow Public questions & answers , Kris, thanks for your reply. I am finally getting around to this and I have an additional question. Is the credentials part of the returned array a comma separated string of roles or an array of roles? Thanks for your helpful response. I don't think the PreAuthenicatedListener is mentioned anywhere in the Symfony2 documentation (besides the API). – Patrick James McDougle Jan 26 '12 at 16:32

If you are using your Gatekeeper class for authentication before the request hits the firewall you can create a "pre-authenticated" (i.e. authentication happens before the firewall) listener by extending AbstractPreAuthenticatedListener and implementing getPreAuthenticatedData():

class GatekeeperListener extends AbstractPreAuthenticatedListener {
   function getPreAuthenticatedData(Request $request) {
      return array(
         Gatekeeper::getCurrentUsername(), // username
         '', // credentials
load more v

Conferences , Reference ,Most websites have a login form where users authenticate using an identifier (e.g. email address or username) and a password. This functionality is provided by the form login authenticator.,The firewall initiates the authentication process by redirecting the user to the login form (/login);

$ composer require symfony / security - bundle
load more v

Move into the newly created project and install a web server:,Symfony utilizes Composer to manage its dependencies. So, before using Symfony, make sure you have Composer installed on your machine. We can install Symfony Framework by issuing the composer create-project command in our terminal or using the symfony installer.,Lastly, generate a new controller that will handle the login process for a user:,We are making use of the Symfony built-in form methods in this template.

To create our application for this tutorial, run the following command to create a new web application named top-tech-companies:

composer create - project symfony / website - skeleton top - tech - companies
load more v

Test the new links and pages - the Login link is not yet functional, of course, and you will have free access to the ‘Personal’ page without authorization.,Symfony Flex requires you to explicitly install the Security bundle to use it in your application. Additionally, no matter what kind of authentication method you choose, you must create your User entity class, and it must implement UserInterface.,Security in Symfony starts with the User entity class. There is also a helpful Maker bundle which allows you to create entities quickly from the command line, let’s make use of it:,I’ll review the Symfony security system in more detail (and in a more practical way) during the tutorial.

mysql - uroot - p
CREATE DATABASE symfony_auth CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
GRANT ALL on symfony_auth.*to 'symfony_auth'
@ ''
identified by 'symfony_auth';
load more v

Symfony Security: This will help with authentication and access control in the application.,Maker: This will help us create controllers, entities, and the like, as well as to set up authentication.,Doctrine: The Doctrine ORM will help with managing the application's database.,With the questions answered, update the App\Security\LoginAuthenticator's onAuthenticationSuccess() method to match the following.

symfony new sms_otp_generator
cd sms_otp_generator
load more v

There are a lot of ways that you can allow your users to log in... one way being a login form that loads users from the database. That's what we're going to build first.,Nothing too fancy here. Let's customize this to be a login page: set the URL to /login, call the route app_login and rename the method to login():,Before we start thinking about authenticating the user, we first need to build a login page, which... if you think about it... has nothing to do with security! It's just a normal Symfony route, controller & template that renders a form. Let's cheat a little to make this. Run:, 18. AbstractLoginFormAuthenticator & Redirecting to Previous URL 5:13

symfony console make: controller
load more v

The Symfony Security Component allows you to set up security features like authentication, role-based authorization, CSRF tokens and more very easily. In fact, it's further divided into four sub-components which you can choose from according to your needs.,Firstly, let's go through the usual authentication flow provided by the Symfony Security component.,In this article, we are going to explore the authentication feature provided by the symfony/security-core component.,The Security component has the following sub-components:

So go ahead and install the Security component using the following command.

$composer require symfony / security
load more v

This will update the config/packages/security.yaml file by adding a logout route and create authenticator, controller and login form files.,Select [1] Login form authenticator, call it LoginFormAuthenticator, confirm the controller name: SecurityController and accept adding the logout route. ,We want to shorten that constructor in the EmailVerifier class and also add proper user roles after email verification: ,Add role hierarchy and access rules to config/packages/security.yaml to achieve what's explained above:

composer create - project symfony / website - skeleton my_new_app
load more v

Warning: This implementation upgrades password hashes to Bcrypt and is therefore NOT up-to-date. Consider modifying it to upgrade hashes to Argon2id instead.,Bcrypt is assumed to be an up-to-date algorithm in the following code. Let's say you have an user database with SHA-1 hashed passwords.,See to get the picture.,config/packages/security.yaml

   # Tweak these two values according to server hardware.It is recommended that server response doesn 't take more than
# 1 second during "normal"
# Feel free to ignore the 1 second limit
for important passwords(e.g.admin account), you would then have to create a
# dedicated encoder
for admin users, with higher values
for these two settings.
# See https: //
   # Bear in mind that login will take longer
if OnAuthPasswordRehashIfAutoEncoderSettingsChange is triggered and has to rehash the
# password.This situation should therefore not be considered as a "normal"
login and could take more than 1 second.
app.argon2id_memory_cost: 128000 # Default is 65536
app.argon2id_time_cost: 10 # Default is 4
load more v

Other "security-setup" queries related to "Security Setup for Symfony2 Without login"