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 :

recuperation des données ldap avec spring security


Sujet :

Spring Java

  1. #1
    Membre à l'essai
    Femme Profil pro
    Étudiant
    Inscrit en
    Mars 2017
    Messages
    18
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2017
    Messages : 18
    Points : 13
    Points
    13
    Par défaut recuperation des données ldap avec spring security
    Slt, j'ai besoin de récupérer des données (mail,nom,prenom...) après authentification.
    Je ne sais pas comment faire ça

    voila le code qui permet de faire l'authentification:

    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
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
     
     
     
    @Configuration 
    //@Order(SecurityProperties.BASIC_AUTH_ORDER) 
    public class SecurityConfig extends WebSecurityConfigurerAdapter{
     
     
     
     
             @Value("${ldap.urls}")
    	private String ldapUrls;
     
    	@Value("${ldap.base.dn}")
    	private String ldapBaseDn;
     
    	@Value("${ldap.user.dn.pattern}")
    	private String ldapUserDnPattern;
     
    	@Value("${ldap.enabled}")
    	private String ldapEnabled;
     
     
     
            @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        // ALTHOUGH THIS SEEMS LIKE USELESS CODE,
        // ITS REQUIRED TO PREVENT SPRING BOOT AUTO-CONFIGURATION
        return super.authenticationManagerBean();
    }
     
     
     
            @Override
    	protected void configure(HttpSecurity http) throws Exception {
            http
            	.authorizeRequests()
    				.anyRequest().authenticated()
    				.and()
    			.formLogin().and().httpBasic().disable();
    	}
     
        @Bean
    public InetOrgPersonContextMapper userContextMapper() {
        return new InetOrgPersonContextMapper();
    }
     
           @Override
    	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
     
                 GrantedAuthoritiesMapper grantedAuthoritiesMapper = null;
     
     
    			auth
    				.ldapAuthentication().userDnPatterns("uid={0},ou=users")
     
    				.contextSource()
    					.url("ldap://localhost:10389/" + ldapBaseDn)
     
    					.and()
     
                                    .passwordCompare()
                        .passwordAttribute("userPassword")
                                    .and()
                                    .passwordEncoder(new LdapShaPasswordEncoder()).authoritiesMapper(grantedAuthoritiesMapper);
                            }
    }

  2. #2
    Membre averti
    Inscrit en
    Octobre 2005
    Messages
    135
    Détails du profil
    Informations forums :
    Inscription : Octobre 2005
    Messages : 135
    Points : 391
    Points
    391
    Par défaut
    A tu regardé ce que te donnais ton controller si tu lui passais le org.springframework.security.core.Authentication en paramètres ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
        @RequestMapping("/mon_path")
        @ResponseBody
        public Health health(final Authentication user){
     
           return user.toString();
        }

  3. #3
    Membre à l'essai
    Femme Profil pro
    Étudiant
    Inscrit en
    Mars 2017
    Messages
    18
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2017
    Messages : 18
    Points : 13
    Points
    13
    Par défaut
    oui, il me donne le uid , baseDn, url

  4. #4
    Membre averti
    Inscrit en
    Octobre 2005
    Messages
    135
    Détails du profil
    Informations forums :
    Inscription : Octobre 2005
    Messages : 135
    Points : 391
    Points
    391
    Par défaut
    tu peut passer par le LdapTemplate

    voici un code viala méthode R.A.C.H.E.
    je te laisse adapter.

    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
     
    public class LdapUtilisateurServiceImpl  {
     
        @Autowired
        protected LdapTemplate ldapTemplate;
     
        public Utilisateur findByUid(String uid) throws Exception {
                return  ldapTemplate.lookup("uid="+ uid, new UtilisateurContextMapper());
        }
     
        /**
         * Mappe un objet utilisateur depuis le DirContextOperations spring
         */
        private static class UtilisateurContextMapper extends AbstractContextMapper<Utilisateur > {
     
            @Override
            public Utilisateur doMapFromContext(final DirContextOperations context) {
                Utilisateur utilisateur = new Utilisateur();
                utilisateur.setUdi(context.getStringAttribute("uid"));
                utilisateur.setNom(context.getStringAttribute("sn"));
                utilisateur.setPrenom(context.getStringAttribute("givenName"));
                utilisateur.setEmail(context.getStringAttribute("mail"));
     
                return utilisateur;
            }
        }
     
    }

Discussions similaires

  1. recupérer des données ldap avec spring.
    Par moman dans le forum Spring
    Réponses: 1
    Dernier message: 05/12/2013, 04h50
  2. [Security] Authentification LDAP avec Spring Security
    Par tiamo dans le forum Spring
    Réponses: 3
    Dernier message: 24/08/2012, 14h48
  3. Champs Ldap avec Spring-security
    Par thierryler dans le forum Frameworks Web
    Réponses: 1
    Dernier message: 18/06/2012, 13h21
  4. Réponses: 5
    Dernier message: 16/03/2010, 13h48
  5. Réponses: 3
    Dernier message: 08/03/2007, 08h51

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