drema2014 2014-01-10 10:49
浏览 79
已采纳

Symfony2自定义Authenticator在未经过身份验证时执行某些操作

How to manage Full authentication is required to access this resource.? I want to redirect user when he is not authenticated. I have custom uthenticater which authenticate user depending on session data, and i want to redirect user when hes not authenticatet.

My authenticator class:

/**
 * @Service("sso_authenticator")
 */
class SsoAuthenticator implements SimplePreAuthenticatorInterface
{

    /**
     * @var SsoUserProvider
     */
    protected $userProvider;

    /**
     * @InjectParams({
     *      "userProvider" = @Inject("sso_user_provider")
     * })
     */
    public function __construct(SsoUserProvider $userProvider)
    {
        $this->userProvider = $userProvider;
    }

    public function createToken(Request $request, $providerKey)
    {
        $user = $request->getSession()->get('sso_user');

        if (!$user) {
            throw new BadCredentialsException('No user found');
        }

        return new PreAuthenticatedToken(
                'anon.', $user, $providerKey
        );
    }

    public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
    {
        $user = $token->getCredentials();
        if (!is_array($user)) {
            $user = $token->getUser();
        }

        if (!$user) {
            throw new AuthenticationException('User does not exist.');
        }

        $ssoUser = $this->userProvider->loadUser($user);

        return new PreAuthenticatedToken(
                $ssoUser, $user, $providerKey, $ssoUser->getRoles()
        );
    }

    public function supportsToken(TokenInterface $token, $providerKey)
    {
        return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey;
    }

}
  • 写回答

1条回答 默认 最新

  • dongyin0628 2014-01-12 13:26
    关注

    i set the login path to logout path like this:

    secured_area:           
        form_login: 
            login_path : main_user_logout
    

    then i wrote custom logout handler:

    /**
     * @Service("sso_authentication_handler")
     */
    class SsoAuthenticationHandler implements LogoutSuccessHandlerInterface
    {
        /**
         * @var Router
         */
        private $router;
    
        /**
         * @var array
         */
        protected $ssoUrls;
    
        /**
         * @InjectParams({
         *      "ssoUrls" = @Inject("%wordpress_sso%"),
         *      "router" = @Inject("router")
         * })
         */
        public function __construct(array $ssoUrls, Router $router)
        {
            $this->ssoUrls = $ssoUrls;
            $this->router = $router;
        }
    
        public function onLogoutSuccess(Request $request)
        {
            $locale = $request->getLocale();
            if ($locale === 'pl') {
                $url = $this->ssoUrls[$locale];
            } else {
                $url = $this->ssoUrls['en'];
            }
    
            $url .= '?returnUrl=' . $this->router->generate('main');
    
            return new RedirectResponse($url);
        }
    
    }
    

    so with this combination i achive behavior like when youser is not authenticated or when he logout i will redirect him to other site to login, in my example to wordpress.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog