IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Spring Java Discussion :

Problème d'authentification avec Spring Security dû au GrantedAuthority


Sujet :

Spring Java

  1. #1
    Membre actif
    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    728
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2006
    Messages : 728
    Points : 250
    Points
    250
    Par défaut Problème d'authentification avec Spring Security dû au GrantedAuthority
    Bonjour,

    j'utilise spring security et j'ai un problème d'authentification avec les GrantedAuthority

    Voici ma classe USER

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
     
    @Entity
    @Table(name="MEMBRE")
    public class Membre  implements UserDetails, Serializable {
     
        ...................
    	private ArrayList<Role> authorities;
     
    	@ManyToMany
    	@JoinTable(
    			name="MEMBRE_ROLE",
    			joinColumns={@JoinColumn(name="id_membre", referencedColumnName="id_membre")},
    			inverseJoinColumns={@JoinColumn(name="id_role", referencedColumnName="id_role")})
    	public Collection<Role> getAuthorities() {
    		return this.authorities;
    	}
     
    	public void setAuthorities(ArrayList<Role> authorities) {
    		this.authorities = authorities;
    	}
        ................
        }
    Voici ma class Role

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
     
    @Entity
    @Table(name="ROLE")
    public class Role  implements GrantedAuthority, Serializable {
     
    	/**
             *
             */
    	private static final long serialVersionUID = 4160725609927520747L;
     
    	private Integer id;
     
    	private String role;
     
    	@Transient
    	public String getAuthority() {
    		return this.role;
    	}
     
    	@Id
    	@GeneratedValue
    	@Column(name = "id_role", unique = true, nullable = false, precision = 9, scale = 0)
    	public Integer getId() {
    		return this.id;
    	}
     
    	@Column(name = "role", nullable = false, length = 20)
    	public String getRole() {
    		return this.role;
    	}
     
    	public void setId(Integer id) {
    		this.id = id;
    	}
     
    	public void setRole(String role) {
    		this.role = role;
    	}
     
    }
    voici ma table MEMBRE_ROLE

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
     
    CREATE  TABLE IF NOT EXISTS `MEMBRE_ROLE` (
      `id_role` INT NOT NULL AUTO_INCREMENT ,
      `id_membre` INT NOT NULL),
      PRIMARY KEY (`id_role`, `id_membre`) ,
      CONSTRAINT `fk_membre_role_membre`
        FOREIGN KEY (`id_membre` )
        REFERENCES `membre` (`id_membre` )
        ON DELETE NO ACTION
        ON UPDATE NO ACTION,
      CONSTRAINT `fk_membre_role_role`
        FOREIGN KEY (`id_role` )
        REFERENCES `role` (`id_role` )
        ON DELETE NO ACTION
        ON UPDATE NO ACTION)
    ENGINE = InnoDB
    DEFAULT CHARACTER SET = utf8;
    Et quand je tente de m'authentifier, je récupère l'erreur suivante:

    org.springframework.security.authentication.InternalAuthenticationServiceException: IllegalArgumentException occurred while calling setter for property [domain.Membre.authorities (expected type = java.util.ArrayList)]; target = [domain.Membre@1807f3f2], property value = [[domain.Role@16cc0706, domain.Role@88b48]] setter of domain.Membre.authorities; nested exception is IllegalArgumentException occurred while calling setter for property [domain.Membre.authorities (expected type = java.util.ArrayList)]; target = [domain.Membre@1807f3f2], property value = [[domain.Role@16cc0706, domain.Role@88b48]]

    je comprend que le setter n'est pas bien défini, mais je n'ai pas trouvé une solution et je ne sais pas si je ma façon d'utiliser mes GrantedAuthority est correcte ou pas. Merci d'avance pour vos suggestions

  2. #2
    Membre actif
    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    728
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2006
    Messages : 728
    Points : 250
    Points
    250
    Par défaut
    Bonjour,

    j'ai debuggé en remplacant mon setter pour mes authorities par

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
      public void setAuthorities(Object authorities) {
        	this.authorities = (ArrayList<Role>)authorities;
        }
    Et je récupère un persistentBag comme argument

    Voici l'inspection de mon objet em mode debug

    Nom : Capture d’écran 2015-07-11 à 12.05.47.png
Affichages : 445
Taille : 45,5 Ko

    comment recuperer une arrayList ? Je suppose qu'il y a une erreur de mapping. J'ai trouvé ce post

    http://www.coderanch.com/t/431759/OR...-PersistentBag

    mais cela ne m'a pas donné de solution qui marche.

  3. #3
    Membre actif
    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    728
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2006
    Messages : 728
    Points : 250
    Points
    250
    Par défaut
    Bonjour,

    finalement j'ai trouvé une solution temporaire suivante

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    	public void setAuthorities(PersistentBag authorities) {
    		final List<Object> listObject = Arrays.asList(authorities.toArray());
    		final ArrayList<Role> roles = new ArrayList<Role>();
    		for (final Object object : listObject) {
    			if (object instanceof Role) {
    				roles.add((Role)object);
    			}
    		}
    		this.authorities = roles;
    	}
    Mais c'est lourd à gérer. Si vous avez une solution plus élégante, merci d'avance.

  4. #4
    Membre confirmé Avatar de ruscov
    Homme Profil pro
    Architecte de système d'information
    Inscrit en
    Mars 2007
    Messages
    347
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : Belgique

    Informations professionnelles :
    Activité : Architecte de système d'information

    Informations forums :
    Inscription : Mars 2007
    Messages : 347
    Points : 500
    Points
    500
    Par défaut
    Hello,
    J'ai l'impression que tu t'es compliqué la vie. Je te met ici ma config :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
     
    @Table(name = "USERS")
    public class Users implements Serializable {
    @Id
    	@Column(name = "username", unique = true, nullable = false,length = 255)
    	@NotEmpty
    	private String username;
     
    	@Column(name = "password", nullable = false,length = 100)
    	@NotEmpty
    	private String password;
     
    	@Column(name = "enabled", nullable = false)
    	private boolean enabled;
     
    	@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")	
    	private Set<Authorities> authorities;
    ...
    getters/setters
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
     
    @Entity
    @Table(name = "AUTHORITIES")
    public class Authorities implements Serializable {
     
    	private static final long serialVersionUID = 8937598868253978337L;
     
    	@Id
    	@Column(name = "USERNAME", nullable = false,length = 255)
    	@NotEmpty
    	private String username;
     
    	@Column(name = "AUTHORITY", nullable = false,length = 70)
    	@NotEmpty
    	private String authority;
     
    	@ManyToOne(fetch = FetchType.LAZY)
    	@JoinColumn(name="username", insertable=false, updatable = false)
    	private Users user;
     
    ...
    getters/setters
    }
    et dans ta config spring security dans ton authentication provider :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    <jdbc-user-service id="jdbcUserService" data-source-ref='dataSource'  
               users-by-username-query='select username,password, enabled from USERS where username=?'  
               authorities-by-username-query='select USERS.username , AUTHORITIES.AUTHORITY as authorities from USERS,AUTHORITIES  
                where USERS.username = ? AND USERS.username=AUTHORITIES.USERNAME '/>
    Mes logiciels n’ont jamais de bug. Ils développent juste certaines fonctions aléatoires.

Discussions similaires

  1. [Security] Simple authentification avec spring security
    Par rachida90 dans le forum Spring
    Réponses: 1
    Dernier message: 21/01/2014, 10h13
  2. [Security] Plusieurs types d'authentification avec spring security
    Par soumayachouchene dans le forum Spring
    Réponses: 0
    Dernier message: 10/04/2013, 18h45
  3. [Security] Authentification avec Spring Security
    Par tiamo dans le forum Spring
    Réponses: 1
    Dernier message: 11/09/2012, 14h01
  4. [Security] Utiliser l'authentification avec Spring Security
    Par wadjaawbk dans le forum Spring
    Réponses: 4
    Dernier message: 21/11/2011, 18h23
  5. Réponses: 1
    Dernier message: 19/12/2010, 12h44

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo