我有一個自定義的用戶提供,在引導下:Symfony2的自定義用戶提供程序不工作
http://symfony.com/doc/current/cookbook/security/custom_provider.html
所有的工作沒有錯誤,但我不設法訪問受限制的區域。在我的UserProvider類中,我將$ roles var設置爲array(「ROLE_USER」),這就是我需要訪問路由應用程序/列表的權限,但是當我進入應用程序/列表時,Symfony重定向我再次登錄然後再次。
我見過的調試工具欄,它的結果:
Username anon.
Authenticated? yes
Roles { }
Token class Symfony\Component\Security\Core\Authentication\Token\AnonymousToken
我security.yml文件是:
security:
firewalls:
secured_area:
pattern: ^/
anonymous: ~
form_login: ~
http_basic:
realm: "Secured Demo Area"
form_login:
provider: webservice
login_path: login
check_path: login_check
always_use_default_target_path: true
default_target_path: listado_actas
logout:
path: logout
target: login
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
providers:
webservice:
id: webservice_user_provider
encoders:
Symfony\Component\Security\Core\User\User: plaintext
Actas\Gestion\UserBundle\Security\User\WebServiceUser:
id: my.encoder.service
我UserProvider類看起來像下面這樣。我只是打電話,讓我說,我會在我的UserClass的存儲TOKEN的XML服務:
public function loadUserByUsername($username)
{
$salt = "";
$roles = "";
// make a call to your webservice here
$password = $this->request->get('_password');
$xml_interface = new XMLInterfaceBundle();
$token = $xml_interface->requestLogin($username, $password);
if (strlen($token) > 10) {
$roles = array("ROLE_USER");
$salt = "";
return new WebserviceUser($username, $password, $salt, $roles, $token);
}
throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
}
這是我需要把DaoAuthenticationProvider UserObject :: checkAuthentication()
Actas\Gestion\UserBundle\Security\User\WebserviceUser Object
(
[username:Actas\Gestion\UserBundle\Security\User\WebserviceUser:private] => 44886706X
[password:Actas\Gestion\UserBundle\Security\User\WebserviceUser:private] => 44886706XkCrDP
[salt:Actas\Gestion\UserBundle\Security\User\WebserviceUser:private] =>
[roles:Actas\Gestion\UserBundle\Security\User\WebserviceUser:private] => Array
(
[0] => ROLE_ADMIN
)
[my_token:Actas\Gestion\UserBundle\Security\User\WebserviceUser:private] =>
)
這是我的routing.yml :
xml_interface:
resource: "@XMLInterfaceBundle/Resources/config/routing.yml"
prefix: /
actas:
resource: "@ActasBundle/Resources/config/routing.yml"
prefix: /
login:
pattern: /login
defaults: { _controller: UserBundle:Default:login }
login_check:
pattern: /login_check
logout:
pattern: /logout
你是如何在'security.yml'中設置'role_hierarchy'的? – lvarayut
我沒有設置,是否有必要? –
是的,我認爲是。我在我的評論中提到。 – lvarayut