सिम्फनी 4.0
यह प्रक्रिया 3 से 4 के सिम्फनी से परिवर्तित नहीं हुई है, लेकिन यहां नए अनुशंसित AbstractController का उपयोग करके एक उदाहरण दिया गया है। दोनों सेवाओं security.token_storage
और session
सेवाओं को मूल getSubscribedServices
विधि में पंजीकृत किया गया है ताकि आपको अपने नियंत्रक में शामिल न करना पड़े।
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use YourNameSpace\UserBundle\Entity\User;
class LoginController extends AbstractController{
public function registerAction()
{
$user = //Handle getting or creating the user entity likely with a posted form
$token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
$this->container->get('security.token_storage')->setToken($token);
$this->container->get('session')->set('_security_main', serialize($token));
// The user is now logged in, you can redirect or do whatever.
}
}
सिम्फनी 2.6.x - सिम्फनी 3.0.x
सिम्फनी के रूप में 2.6 के security.context
पक्ष में पदावनत किया जाता है security.token_storage
। नियंत्रक अब बस हो सकता है:
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use YourNameSpace\UserBundle\Entity\User;
class LoginController extends Controller{
public function registerAction()
{
$user = //Handle getting or creating the user entity likely with a posted form
$token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
$this->get('security.token_storage')->setToken($token);
$this->get('session')->set('_security_main', serialize($token));
}
}
जबकि यह पदावनत है आप अभी भी उपयोग कर सकते हैं security.context
क्योंकि इसे पिछड़े संगत बनाया गया है। बस इसे सिम्फनी 3 के लिए अपडेट करने के लिए तैयार रहें
आप यहां सुरक्षा के लिए 2.6 बदलावों के बारे में अधिक पढ़ सकते हैं: https://github.com/symfony/symfony/blob/2.6/UPGRADE-2.6.md
सिम्फनी 2.3.x
इसे सिम्फनी 2.3 में पूरा करने के लिए आप अब केवल सुरक्षा संदर्भ में टोकन सेट नहीं कर सकते। आपको सत्र में टोकन को बचाने की भी आवश्यकता है।
एक फ़ायरवॉल के साथ एक सुरक्षा फ़ाइल मानकर:
// app/config/security.yml
security:
firewalls:
main:
//firewall settings here
और एक नियंत्रक क्रिया भी समान है:
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use YourNameSpace\UserBundle\Entity\User;
class LoginController extends Controller{
public function registerAction()
{
$user = //Handle getting or creating the user entity likely with a posted form
$token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
$this->get('security.context')->setToken($token);
$this->get('session')->set('_security_main',serialize($token));
//Now you can redirect where ever you need and the user will be logged in
}
}
टोकन निर्माण के लिए आप एक बनाना चाहेंगे UsernamePasswordToken
, यह 4 मापदंडों को स्वीकार करता है: उपयोगकर्ता इकाई, उपयोगकर्ता क्रेडेंशियल्स, फ़ायरवॉल नाम, उपयोगकर्ता भूमिकाएं। टोकन के मान्य होने के लिए आपको उपयोगकर्ता क्रेडेंशियल्स प्रदान करने की आवश्यकता नहीं है।
Im 100% निश्चित नहीं security.context
है कि यदि आप अभी तुरंत पुनर्निर्देशित करने जा रहे हैं तो टोकन सेट करना आवश्यक है। लेकिन यह चोट नहीं लगती है इसलिए मैंने इसे छोड़ दिया है।
फिर महत्वपूर्ण हिस्सा, सत्र चर की स्थापना। _security_
इस केस main
मेकिंग में आपके फायरवॉल नाम के बाद वैरिएबल नामकरण कन्वेंशन है_security_main