Spring框架中的Active Directory配置问题

时间:2019-03-28 19:31:12

标签: asp.net spring active-directory ldap

我有一个问题,我需要能够访问活动目录来管理Spring上的身份验证。具体来说,我有使用asp.net对其进行管理的同事的代码信息。我从spring.io中获取了项目,但我只是想不通输入哪个URL以启动身份验证连接。我已经尝试了很多次,但是已经超过4天没处理了。我在asp.net中附加了同事“伪装”的部分代码,以便能够查看将在Spring项目中输入的数据。

https://spring.io/guides/gs/authenticating-ldap/(项目Spring.io)

ASP.NET代码检索连接信息

      private string aut(string us, string psw){
        string strPath = "LDAP://DC=USERS,DC=italia, DC=it";
DirectoryEntry de = new DirectoryEntry(strPath,us,psw); 

        DirectorySearcher se = new DirectorySearcher(de);

        se.Filter = "(samaccountname=" + TextBox1.Text + ")";

        try
        {
          SearchResult sr;
          sr = se.FindOne();

          return "user e password CORRETTE:  " + sr.Properties["givenName"][0].ToString();
        }
        catch (Exception)
        {

            return "NON AUTENTICATO";
        }

我要编辑的spring.io代码;

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.password.LdapShaPasswordEncoder;

@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:///dc=USERS,dc=italia,dc=it")
                    .and()
                .passwordCompare()
                    .passwordEncoder(new LdapShaPasswordEncoder())
                    .passwordAttribute("userPassword");
    }

}

0 个答案:

没有答案