CAS 身份验证 Symfony2.



我正在寻找一个在Symfony 2.3上集成CAS身份验证的捆绑包。我找到了这些选项,事实是我不相信任何选项,因为几乎所有捆绑包似乎都在没有更新的情况下被放弃了。

1.- 感官实验室/CasBundle: https://github.com/sensiolabs/CasBundle文档稀疏且不完整。我没有找到任何如何使用它的例子。

2.- BeSimple/BeSimpleSsoAuthBundle: https://github.com/BeSimple/BeSimpleSsoAuthBundle有了这个,我正在测试,我遇到了一些问题。我想我已经解决了第 4 个问题,我落后于另一个问题。

3.- Symfony CAS 客户端:https://wiki.jasig.org/display/CASC/Symfony+CAS+Client完全过时

真的,在symfony中使用CAS进行身份验证的选项很少吗?

我之前有同样的问题,我使用 BeSimpleSsoAuthBundle 解决了它,但您必须进行一些更改:假设您的用户实体已经在您的用户捆绑包中实现,您必须使用唯一的属性 sgid 覆盖:1- BeSimple\SsoAuthBundle\Security\Core\User :

<?php
namespace ApplicationUserBundleSecurityBeSimpleSpawnedUserProvider;
use BeSimpleSsoAuthBundleSecurityCoreUserSpawnedUserProvider;
use SymfonyComponentSecurityCoreUserUserInterface;
use SymfonyComponentSecurityCoreUserUser;
use SymfonyComponentHttpFoundationRedirectResponse;

class SsoUserProvider extends SpawnedUserProvider
{
/**
 * @var array
 */
private $roles;
/**
 * Constructor.
 *
 * @param array $roles An array of roles
 */
private $entityManager;
private $securityContext;
public function __construct($em, $securityContext) {
    $this->em = $em; 
    $this->securityContext = $securityContext; 
}
/**
 * {@inheritdoc}
 */
public function loadUserByUsername($username)
{
    $session = $this->securityContext;
    $qb = $this->em->createQueryBuilder();
    $qb->select("u")
        ->from('ApplicationUserBundle:User', 'u')
        ->where('u.sgid = :sgid')
        ->AndWhere('u.status = 1')
        ->setParameter("sgid", $username);
   $result = $qb->getQuery()->getOneOrNullResult();
    if ($result == NULL) {
        $session->getFlashBag()->add('error', 'Vous ne pouvez pas vous connecter car votre compte est désactivé');
        return new RedirectResponse('login');
    }
   $user_name = $result->getFirstName().' '.$result->getLastName();
    $session->set('userId', $result->getId());
   if ($result->getUserType() == 1) {
       $this->roles = array('ROLE_ADMIN');
   }else if ($result->getUserType() == 0){
       $this->roles = array('ROLE_USER');
   }else{
        $session->getFlashBag()->add('error', 'Vous ne pouvez pas vous connecter car votre compte n'a pas de rôle');
        return new RedirectResponse('logout');
   }
    return $this->spawnUser($user_name);
}
/**
 * {@inheritDoc}
 */
public function refreshUser(UserInterface $user)
{
    if (!$user instanceof User) {
        throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
    }
    return $this->spawnUser($user->getUsername());
}
/**
 * {@inheritDoc}
 */
public function supportsClass($class)
{
    return $class === 'SymfonyComponentSecurityCoreUserUser';
}
/**
 * Spawns a new user with given username.
 *
 * @param string $username
 *
 * @return SymfonyComponentSecurityCoreUserUser
 */
private function spawnUser($username)
{
    //$this->roles = $this->userType;
    return new User($username, null, (array)$this->roles, true, true, true, true);
  }
}

2-覆盖也简单\SsoAuthBundle\安全性\核心\身份验证\提供程序:

<?php
namespace ApplicationUserBundleSecurityBeSimpleAuthenticationProvider;
use SymfonyComponentSecurityCoreUserUserInterface;
use SymfonyComponentSecurityCoreExceptionUsernameNotFoundException;
use SymfonyComponentSecurityCoreExceptionBadCredentialsException;
use BeSimpleSsoAuthBundleSecurityCoreUserUserFactoryInterface;
/*
 * @Override 
 */
use    BeSimpleSsoAuthBundleSecurityCoreAuthenticationProviderSsoAuthenticationPr ovider;
class AppAuthenticationProvider extends SsoAuthenticationProvider
{
/**
 * @var UserProviderInterface
 */
private $userProvider;
/**
 * @var bool
 */
private $createUsers;
/**
 * @var bool
 */
private $hideUserNotFound;
/**
 * @Override file
 * @throws SymfonyComponentSecurityCoreExceptionUsernameNotFoundException
 * @throws SymfonyComponentSecurityCoreExceptionBadCredentialsException
 *
 * @param string $username
 * @param array $attributes
 *
 * @return UserInterface
 */
protected function provideUser($username, array $attributes = array())
{
    try {
        $user = $this->retrieveUser($username);
    } catch (UsernameNotFoundException $notFound) {
        if ($this->createUsers && $this->userProvider instanceof UserFactoryInterface) {
            $user = $this->createUser($username, $attributes);
        } elseif ($this->hideUserNotFound) {
            throw new BadCredentialsException('Bad credentials', 0, $notFound);
        } else {
            throw $notFound;
        }
    }
    return $user;
  }
}

3-当用户登录到您的应用程序时,在会话中保存所需的信息:

<?php
namespace ApplicationUserBundleSecurityAuthenticationHandler;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationRedirectResponse;
use SymfonyComponentRoutingRouter;
use SymfonyComponentSecurityCoreAuthenticationTokenTokenInterface;
use SymfonyComponentSecurityCoreSecurityContext;
use SymfonyComponentSecurityHttpAuthenticationAuthenticationSuccessHandlerInterface;
use DoctrineORMEntityManager;
class LoginSuccessHandler implements AuthenticationSuccessHandlerInterface
{
protected 
    $router,
    $security,
    $entityManager;
public function __construct(Router $router, SecurityContext $security, EntityManager $entityManager)
{
    $this->router = $router;
    $this->security = $security;
    $this->entityManager = $entityManager;
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
    $session = $request->getSession();
    $attributes = $this->security->getToken()->getAttributes();
    $sgid = $attributes['sso:validation']['sgid'];
    $em = $this->entityManager;
    $qb = $em->createQueryBuilder();
    $qb->select("u")
        ->from('ApplicationUserBundle:User', 'u')
        ->where('u.sgid = :sgid')
        ->AndWhere('u.status = 1')
        ->setParameter("sgid", $sgid);
    $result = $qb->getQuery()->getOneOrNullResult();
    //en cas où utilisateur est désactivée
    //Malgre que si il arrive a cette handler ça veut dire qu'il activé car le test se fait sur le bundle BeSimple
    if ($result == NULL) {
        return new RedirectResponse($this->router->generate('login'));
    }
    $session->set('userId', $result->getId());
    $response = new RedirectResponse('admin');
    return $response;
  }
}

4-现在在Application/UserBundle/Ressources/config/security_listeners.yml中定义一个安全列表器:

parameters:
    security.authentication.provider.sso.class: ApplicationUserBundleSecurityBeSimpleAuthenticationProviderAppAuthenticationProvider
services:
    security.authentication.provider.sso:
        class: %security.authentication.provider.sso.class%
        public: false
        arguments: ['', '@security.user_checker', '', '', false]

5-BeSimple配置应该是这样的:

be_simple_sso_auth:
admin_sso:
    protocol:
        id: cas
        version: 2
    server:
        id: cas
        login_url: https://adresse ip:8443/cas-server-webapp-4.0.0/login
        logout_url: https://adresse ip:8443/cas-server-webapp-4.0.0/logout
        validation_url: https://adresse ip:8443/cas-server-webapp-4.0.0/serviceValidate
services:
    spawned_user_provider:
        class:     ApplicationUserBundleSecurityBeSimpleSpawnedUserProviderSsoUserProvider
    arguments: [@doctrine.orm.entity_manager, @session]

6-参数.yml

  be_simple.sso_auth.client.option.curlopt_ssl_verifypeer.value: false
  be_simple.sso_auth.client.option.curlopt_sslversion.value: 4 (Optionale)

7-安全.yml

  main:
        pattern: ^/admin
        context: marketshare_context
        logout:
            path:   /admin/logout
            target: /
        #provider: sso
        trusted_sso:
            manager: admin_sso
            login_action: ApplicationUserBundle:TrustedSso:login
            logout_action: false
            login_path: /admin/login
            check_path: /admin/check
            always_use_default_target_path: true
            default_target_path: /admin/potentiel
            failure_path: /admin/logout
您还可以

测试它是否看起来比BeSimpleSSoBundle更新l3-team/CasBundle更活跃,并且文档更清晰。

它似乎也支持单点注销。

最新更新