2012-07-04 52 views

回答

1

也許是這樣的:

Pesudocode: 

generate hashed link that is unique to user (e.g. md5(email+timestamp+salt)) 
store hash for user in db 
Send link to user in email (same email as used in hash) 

when user accesses site using link: 

fetch hash part from link 
match hash against database 
if match is found, authenticate, else fail 

注意,我不能保證,這是通過任何方式做(不知道如果它甚至有可能是一個安全的方式只有通過鏈接進行身份驗證時纔是安全的)。只是一些想法讓你走。

2

您可能想要查看FOSUserBundle,它內置了很多用於管理用戶的內容。

我們使用類似的技術來註冊用戶。下面是一些基本的代碼(你需要用錯誤處理和實際發現基於URL參數的用戶的方法填寫:

/** 
* @Route("/register/activate/{hash}/{oId}", requirements={"hash"="\w+", "oId"="\d+"}, name="register_byhash") 
* @Method({"GET"}) 
* @Template() 
*/ 
public function registerByHashAction($hash, $oId) 
{ 
    $um = $this->container->get('fos_user.user_manager'); 
    $user = $um->findUserByHash($hash, $oId); // You will need to supply a method that finds the user and checks the hash 

    // Mark the user as now active and save the user here 

    $providerKey = $this->container->getParameter('fos_user.firewall_name'); 
    $token = new UsernamePasswordToken($user, null, $providerKey, $user->getRoles()); 
    $this->container->get('security.context')->setToken($token); 


    $url = $this->container->get('router')->generate('welcome'); 
    return new RedirectResponse($url); 
} 
0

解決這個最好的辦法是實現一個自定義身份驗證提供者的身份驗證供應商負責用戶身份驗證,使用您希望幾乎一切:可能是一個cookie(記得我爲例),表單數據,或者在你的情況,查詢字符串參數

你需要:

  • 用於存儲認證參數的自定義令牌可以管理令牌和驗證用戶
  • 一些這些組件裝配到安全系統之三
  • 自定義的認證供應商(意味着你需要定義一個工廠,某些配置)。

使用身份驗證提供程序,您將有機會將記錄的用戶分配給自定義安全角色(因爲記住我的功能),因此您可以將登錄用戶與「自動登錄」用戶區分開來。如果要限制自動登錄的用戶可以執行的操作(例如,您可能希望僅對某個特定操作進行身份驗證),這可能非常有用。

有大約自定義身份驗證提供正式菜譜食譜:

http://symfony.com/doc/current/cookbook/security/custom_authentication_provider.html

我已經做了相當多,你需要什麼,在screenfony.com博客中寫道一下:

http://www.screenfony.com/blog/symfony-custom-authentication-provider

相關問題