當前位置:網站首頁>使用LDAP驗證用戶登錄,深入剖析原理

使用LDAP驗證用戶登錄,深入剖析原理

2021-08-19 22:05:30 Android小馬丁

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");

}

}




?新建文件src/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




新建src/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.測試



![](https://s2.51cto.com/images/20210819/1629380868559681.jpg)


### 最後

給大家送一個小福利

![](https://s2.51cto.com/images/20210819/1629380868630236.jpg)

資料都是免費分享的,附送高清腦圖,高清知識點講解教程,以及一些面試真題及答案解析。送給需要的提昇技術、准備面試跳槽、自身職業規劃迷茫的朋友們。**[點我免費領取](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.

版權聲明
本文為[Android小馬丁]所創,轉載請帶上原文鏈接,感謝
https://cht.chowdera.com/2021/08/20210819220529631p.html

隨機推薦