2013-10-28 4 views
8

Symfony 2.3에서 CAS 인증을 통합하기위한 번들을 찾고 있습니다. 나는이 옵션들을 발견했고 진실은 거의 모든 번들들이 업데이트되지 않고 버려진 것 같기 때문에 나는 확신하지 못한다.CAS 인증 Symfony2

1- sensiolabs/CasBundle : https://github.com/sensiolabs/CasBundle 설명서가 불완전하고 불완전합니다. 나는 그것을 사용하는 방법의 예제를 발견하지 못했습니다.

2.-BeSimple/BeSimpleSsoAuthBundle : https://github.com/BeSimple/BeSimpleSsoAuthBundle 이 글에서는 테스트 중이며 몇 가지 문제가 있습니다. 나는 네 번째 문제를 해결하고 다른 것의 뒤에 있다고 생각한다.

3 .- 심포니 CAS 클라이언트 : https://wiki.jasig.org/display/CASC/Symfony+CAS+Client

정말, 심포니에 CAS를 인증 할 수 있도록 몇 가지 옵션이 있습니다 완전히 구식?

+0

결국 우리는 ['jasig/phpcas'] (https://github.com/Jasig/phpCAS/blob/master/docs/examples/example_simple.php)를 사용하여 자체 공급자를 구현하게되었습니다. 매우 간단했습니다. – rolebi

+0

동일한 문제가 있습니다. 현재'BeSimpleSsoAuthBundle'의'login_path'를 사용하고 있습니다. 인증 후, CAS 서버는'/ login'으로 나를 포워드합니다 ... 이것에 대한 문서는 아주 적습니다 ... : –

+0

지난 1 년 이래로이 주제에 관한 뉴스가 있습니까? –

답변

2

이전에 같은 문제가 있었는데 BeSimpleSsoAuthBundle을 사용하여 해결했지만 몇 가지 사항을 변경해야합니다. 사용자 엔터티가 User Bundle에 이미 구현되어 있고 사용자가 재정의해야하는 고유 속성을 가지고 있다고 가정합니다 : 1 BeSimple \ SsoAuthBundle \ 보안 \ 코어 \ 사용자 :

<?php 

namespace Application\UserBundle\Security\BeSimple\SpawnedUserProvider; 

use BeSimple\SsoAuthBundle\Security\Core\User\SpawnedUserProvider; 
use Symfony\Component\Security\Core\User\UserInterface; 
use Symfony\Component\Security\Core\User\User; 
use Symfony\Component\HttpFoundation\RedirectResponse; 


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 === 'Symfony\Component\Security\Core\User\User'; 
} 

/** 
* Spawns a new user with given username. 
* 
* @param string $username 
* 
* @return \Symfony\Component\Security\Core\User\User 
*/ 
private function spawnUser($username) 
{ 
    //$this->roles = $this->userType; 
    return new User($username, null, (array)$this->roles, true, true, true, true); 
    } 
} 

2 오버라이드 또한 BeSimple \ SsoAuthBundle \ 보안 \ 코어 \ 인증 \ 공급자 :

<?php 

namespace Application\UserBundle\Security\BeSimple\Authentication\Provider; 

use Symfony\Component\Security\Core\User\UserInterface; 
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; 
use Symfony\Component\Security\Core\Exception\BadCredentialsException; 
use BeSimple\SsoAuthBundle\Security\Core\User\UserFactoryInterface; 

/* 
* @Override 
*/ 
use BeSimple\SsoAuthBundle\Security\Core\Authentication\Provider\SsoAuthenticationPr ovider; 

class AppAuthenticationProvider extends SsoAuthenticationProvider 
{ 
/** 
* @var UserProviderInterface 
*/ 
private $userProvider; 

/** 
* @var bool 
*/ 
private $createUsers; 

/** 
* @var bool 
*/ 
private $hideUserNotFound; 

/** 
* @Override file 
* @throws \Symfony\Component\Security\Core\Exception\UsernameNotFoundException 
* @throws \Symfony\Component\Security\Core\Exception\BadCredentialsException 
* 
* @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 때 사용자

parameters: 
    security.authentication.provider.sso.class: Application\UserBundle\Security\BeSimple\Authentication\Provider\AppAuthenticationProvider 

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

5 BeSimple 구성해야합니다

<?php 

namespace Application\UserBundle\Security\Authentication\Handler; 

use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\RedirectResponse; 
use Symfony\Component\Routing\Router; 
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; 
use Symfony\Component\Security\Core\SecurityContext; 
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface; 
use Doctrine\ORM\EntityManager; 

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- 이제 응용 프로그램/UserBundle/능숙/설정/security_listeners.yml의 보안 청취자를 정의 세션에서 필요한 정보를 저장 응용 프로그램에 로그인 그렇게 될 :

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:  Application\UserBundle\Security\BeSimple\SpawnedUserProvider\SsoUserProvider 
    arguments: [@doctrine.orm.entity_manager, @session] 

6 parameters.yml

,517,333 (210)

7- security.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 
1

당신은 또한 더 & 최근의 활성 및 BeSimpleSSoBundle보다 명확하게 문서로 보인다 l3-team/CasBundle을 테스트 할 수 있습니다.

또한 싱글 사인 아웃을 지원하는 것으로 보입니다.