Spring security switch to Ldap authentication and database authorities

后端 未结 4 432
灰色年华
灰色年华 2020-12-08 01:34

I implemented database authentication for my web page and web service. It work well for both, now I have to add Ldap authentication. I have to authenticate through remote L

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-08 01:54

    Spring Security already supports LDAP out-of-the-box. It actually has a whole chapter on this.

    To use and configure LDAP add the spring-security-ldap dependency and next use the AuthenticationManagerBuilder.ldapAuthentication to configure it. The LdapAuthenticationProviderConfigurer allows you to set the needed things up.

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.ldapAuthentication()
          .contextSource()
            .url(...)
            .port(...)
            .managerDn(...)
            .managerPassword(...)
          .and()
            .passwordEncoder(passwordEncoder())
            .userSearchBase(...)        
            .ldapAuthoritiesPopulator(new UserServiceLdapAuthoritiesPopulater(this.userService));      
    }
    

    Something like that (it should give you at least an idea on what/how to configure things) there are more options but check the javadocs for that. If you cannot use the UserService as is to retrieve the roles (because only the roles are in the database) then implement your own LdapAuthoritiesPopulator for that.

提交回复
热议问题