2013-10-07 5 views
2

Désolé pour mon anglais. Pourquoi ne fonctionne pas la méthode isAuthenticated() dans la sécurité de printemps? J'utilise en JSF:printemps sécurité 3 isAuthenticated() ne fonctionne pas

#{loginMB.authentication.authenticated} 

<sec:authorize access="hasRole('ROLE_ADMIN')"> 
    test 
</sec:authorize> 

Cela ne fonctionne pas. Toutes les fois qu'il retourne true, si je suis authentifié ou non.

Si montrant un rôle:

#{loginMB.authentication.authorities} 

Il est de montrer droit, quand est authentifié un rôle est [ROLE_ADMIN], quand est pas authentifié un rôle est [ROLE_ANONYMOUS].

Quand est-ce un problème?

==== ==== Mise à jour

Si metod isAuthenticated() à créer des LoginBean pour l'enregistrement AnonymousAuthenticationToken comme dit Aleksandr:

public boolean isAuthenticated(){ 

    Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); 
    return authentication != null && !(authentication instanceof AnonymousAuthenticationToken) && authentication.isAuthenticated(); 

} 

Il travaille. Merci Aleksandr. Mais autoriser le tag ne fonctionne pas. Si j'ajoute dans une page JSF:

<sec:authorize access="hasRole('ROLE_ANONYMOUS')"> 
    ROLE_ANONYMOUS 
</sec:authorize> 
<sec:authorize access="hasRole('ROLE_ADMIN')"> 
    ROLE_ADMIN 
</sec:authorize> 

Il ROLE_ANONYMOUS imprimer et ROLE_ADMIN. Pourquoi?

==== Mise à jour 2 ====

applicationContext-security.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/security" 
      xmlns:beans="http://www.springframework.org/schema/beans" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:context="http://www.springframework.org/schema/context" 
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
      http://www.springframework.org/schema/security 
      http://www.springframework.org/schema/security/spring-security-3.1.xsd"> 

    <beans:import resource="applicationContext.xml"/> 


    <global-method-security jsr250-annotations="enabled" /> 

    <http auto-config="true" use-expressions="true"> 
     <form-login login-page="/pages/login.html" authentication-failure-url="/fail.html"/> 
     <intercept-url pattern="/**" access="permitAll" /> 

    </http> 

    <authentication-manager alias="authenticationManager"> 
     <authentication-provider user-service-ref="UserDAO"> 
      <password-encoder hash="plaintext" /> 
     </authentication-provider> 
    </authentication-manager> 

</beans:beans> 
+0

Cela fonctionne très bien. Vous avez juste besoin de vérifier 'AnonymousAuthenticationToken' si vous voulez utiliser cette méthode. –

+0

J'ai créé metod isAuthenticated() dans LoginBean pour vérifier AnonymousAuthenticationToken, c'est travail. Mais la sécurité de printemps autorise le tag ne fonctionne pas. – z3r9

+0

Que voulez-vous dire? –

Répondre

3

Problème résolu.

  1. Si créer metod isAuthenticated() dans LoginBean pour l'enregistrement AnonymousAuthenticationToken comme dit Aleksandr:

    public boolean isAuthenticated(){ 
    
         Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); 
         return authentication != null && !(authentication instanceof AnonymousAuthenticationToken) && authentication.isAuthenticated(); 
    
        } 
    

    Il travaille. Merci Aleksandr.

  2. Pour le travail autorisera tag dans la page JSF pour lire here. Et j'ai eu it problème.

Questions connexes