Symfony中remember_me的token_provider配置语法咨询
Hey there! Great job getting your custom CustomTokenService set up extending the DoctrineTokenProvider—let's get it properly configured in your security.yml so Symfony uses it instead of the default cookie-based storage.
Step 1: Confirm Your Custom Service is Registered
First, double-check your app/config/services.yml has the service registered correctly (you mentioned you did this, but just to be thorough):
# app/config/services.yml services: # Replace the class path with wherever your CustomTokenService lives app.custom_remember_me_token_provider: class: App\Security\RememberMe\CustomTokenService arguments: ['@doctrine.orm.entity_manager']
Make sure the service ID (app.custom_remember_me_token_provider) is something you can reference easily—this is what we'll use in the security config.
Step 2: Configure the token_provider in security.yml
Now head to your app/config/security.yml and update the remember_me section under your firewall (usually main) to include the token_provider option pointing to your service ID. Here's a complete example:
# app/config/security.yml security: # ... other security configs (like providers, encoders, etc.) ... firewalls: main: # ... your existing firewall settings (form_login, logout, etc.) ... remember_me: secret: '%kernel.secret%' # Required, uses your app's secret lifetime: 604800 # 1 week in seconds, adjust as needed path: / # Apply remember-me to all routes domain: '%kernel.domain%' # Optional, set your app's domain if needed # Here's the key line—point to your custom token provider service token_provider: app.custom_remember_me_token_provider
Important Note: Database Table Requirement
Since you're using the DoctrineTokenProvider (and your custom service extends it), you need a database table to store the remember-me tokens. The default table name is rememberme_token with this schema:
CREATE TABLE rememberme_token ( series VARCHAR(88) PRIMARY KEY, value VARCHAR(88) NOT NULL, last_used DATETIME NOT NULL, class VARCHAR(100) NOT NULL, username VARCHAR(200) NOT NULL );
You can create this table manually, or use a Doctrine migration to generate it if you prefer.
Quick Check
After setting this up, clear your Symfony cache to make sure the new config is loaded:
php bin/console cache:clear
That should do it! Symfony will now use your custom token provider to store remember-me tokens in the database instead of relying solely on cookies.
内容的提问来源于stack exchange,提问作者Dhananjay

