Utiliser LDAP pour authentifier la connexion de l'utilisateur et approfondir le principe

Android Martin Jr. 2021-08-19 22:06:32 阅读数:378

utiliser ldap pour authentifier la

import org.springframework.context.annotation.Configuration;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;

import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Configuration

public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override

protected void configure(HttpSecurity http) throws Exception {

http
.authorizeRequests()
.anyRequest().fullyAuthenticated()
.and()
.formLogin();

}

@Override

public void configure(AuthenticationManagerBuilder auth) throws Exception {

auth
.ldapAuthentication()
.userDnPatterns("uid={0},ou=people")
.groupSearchBase("ou=groups")
.contextSource()
.url("ldap://localhost:8389/dc=springframework,dc=org")
.and()
.passwordCompare()
.passwordEncoder(new BCryptPasswordEncoder())
.passwordAttribute("userPassword");

}

}


?Nouveau fichiersrc/main/resources/application.properties

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

spring.ldap.embedded.ldif=classpath:test-server.ldif

spring.ldap.embedded.base-dn=dc=springframework,dc=org

spring.ldap.embedded.port=8389


Nouveausrc/main/resources/test-server.ldif?

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

dn: dc=springframework,dc=org

objectclass: top

objectclass: domain

objectclass: extensibleObject

dc: springframework

dn: ou=groups,dc=springframework,dc=org

objectclass: top

objectclass: organizationalUnit

ou: groups

dn: ou=subgroups,ou=groups,dc=springframework,dc=org

objectclass: top

objectclass: organizationalUnit

ou: subgroups

dn: ou=people,dc=springframework,dc=org

objectclass: top

objectclass: organizationalUnit

ou: people

dn: ou=space cadets,dc=springframework,dc=org

objectclass: top

objectclass: organizationalUnit

ou: space cadets

dn: ou=“quoted people”,dc=springframework,dc=org

objectclass: top

objectclass: organizationalUnit

ou: “quoted people”

dn: ou=otherpeople,dc=springframework,dc=org

objectclass: top

objectclass: organizationalUnit

ou: otherpeople

dn: uid=ben,ou=people,dc=springframework,dc=org

objectclass: top

objectclass: person

objectclass: organizationalPerson

objectclass: inetOrgPerson

cn: Ben Alex

sn: Alex

uid: ben

userPassword: $2a 10 10 c6bSeWPhg06xB1lvmaWNNe4NROmZiSpYhlocU/98HNr2MhIOiSt36

dn: uid=bob,ou=people,dc=springframework,dc=org

objectclass: top

objectclass: person

objectclass: organizationalPerson

objectclass: inetOrgPerson

cn: Bob Hamilton

sn: Hamilton

uid: bob

userPassword: bobspassword

dn: uid=joe,ou=otherpeople,dc=springframework,dc=org

objectclass: top

objectclass: person

objectclass: organizationalPerson

objectclass: inetOrgPerson

cn: Joe Smeth

sn: Smeth

uid: joe

userPassword: joespassword

dn: cn=mouse, jerry,ou=people,dc=springframework,dc=org

objectclass: top

objectclass: person

objectclass: organizationalPerson

objectclass: inetOrgPerson

cn: Mouse, Jerry

sn: Mouse

uid: jerry

userPassword: jerryspassword

dn: cn=slash/guy,ou=people,dc=springframework,dc=org

objectclass: top

objectclass: person

objectclass: organizationalPerson

objectclass: inetOrgPerson

cn: slash/guy

sn: Slash

uid: slashguy

userPassword: slashguyspassword

dn: cn=quote"guy,ou=“quoted people”,dc=springframework,dc=org

objectclass: top

objectclass: person

objectclass: organizationalPerson

objectclass: inetOrgPerson

cn: quote"guy

sn: Quote

uid: quoteguy

userPassword: quoteguyspassword

dn: uid=space cadet,ou=space cadets,dc=springframework,dc=org

objectclass: top

objectclass: person

objectclass: organizationalPerson

objectclass: inetOrgPerson

cn: Space Cadet

sn: Cadet

uid: space cadet

userPassword: spacecadetspassword

dn: cn=developers,ou=groups,dc=springframework,dc=org

objectclass: top

objectclass: groupOfUniqueNames

cn: developers

ou: developer

uniqueMember: uid=ben,ou=people,dc=springframework,dc=org

uniqueMember: uid=bob,ou=people,dc=springframework,dc=org

dn: cn=managers,ou=groups,dc=springframework,dc=org

objectclass: top

objectclass: groupOfUniqueNames

cn: managers

ou: manager

uniqueMember: uid=ben,ou=people,dc=springframework,dc=org

uniqueMember: cn=mouse, jerry,ou=people,dc=springframework,dc=org

dn: cn=submanagers,ou=subgroups,ou=groups,dc=springframework,dc=org

objectclass: top

objectclass: groupOfUniqueNames

cn: submanagers

ou: submanager

uniqueMember: uid=ben,ou=people,dc=springframework,dc=org


### ?3.Tests
![](https://s2.51cto.com/images/20210819/1629380868559681.jpg)
### Enfin
Un petit avantage pour tout le monde.
![](https://s2.51cto.com/images/20210819/1629380868630236.jpg)
Les informations sont partagées gratuitement,Encéphalogramme HD joint,Tutoriel d'explication des points de connaissance HD,Et quelques questions d'entrevue et réponses.Envoyer les techniques de levage nécessaires、Préparez - vous à l'entrevue.、Amis confus dans leur planification de carrière.**[Je le prends gratuitement.](https://gitee.com/vip204888/java-p7)!!!**
![](https://s2.51cto.com/images/20210819/1629380868982668.jpg)

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
Copyright:Cet article est[Android Martin Jr.]Établi,Veuillez apporter le lien original pour réimprimer,remercier。 https://fra.fheadline.com/2021/08/20210819220630808c.html