本文档介绍了如何配置Spring-Security LDAP:
http://docs.spring.io/spring-security/site/docs/3.2.4.CI-SNAPSHOT/reference/htmlsingle/#ldap
Run Code Online (Sandbox Code Playgroud)3.4.5. Spring Bean Configuration <bean id="contextSource" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource"> <constructor-arg value="ldap://monkeymachine:389/dc=springframework,dc=org"/> <property name="userDn" value="cn=manager,dc=springframework,dc=org"/> <property name="password" value="password"/> </bean> <bean id="ldapAuthProvider" class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider"> <constructor-arg> <bean class="org.springframework.security.ldap.authentication.BindAuthenticator"> <constructor-arg ref="contextSource"/> <property name="userDnPatterns"> <list><value>uid={0},ou=people</value></list> </property> </bean> </constructor-arg> <constructor-arg> <bean class="org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator"> <constructor-arg ref="contextSource"/> <constructor-arg value="ou=groups"/> <property name="groupRoleAttribute" value="ou"/> </bean> </constructor-arg> </bean>
如何在没有xml的情况下实现这一目标?这里我们有一个使用本地ldif文件的示例:https: //github.com/spring-projects/spring-security/blob/master/samples/ldap-jc/src/main/java/org/springframework/security/样品/配置/ SecurityConfig.java
我修改了SecurityConfig.java,如下所示:
public void registerGlobalAuthentication(AuthenticationManagerBuilder auth) throws Exception {
DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource(ldap_url);
contextSource.setUrl(ldap_user);
contextSource.setPassword(ldap_password);
DefaultLdapAuthoritiesPopulator ldapAuthoritiesPopulator = new DefaultLdapAuthoritiesPopulator(contextSource, "ou=groups");
ldapAuthoritiesPopulator.setGroupRoleAttribute("ou");
LdapAuthenticationProviderConfigurer<AuthenticationManagerBuilder> ldapAuthenticationProviderConfigurer = auth.ldapAuthentication();
ldapAuthenticationProviderConfigurer
.userDnPatterns("uid={0},ou=people")
.groupSearchBase("ou=groups")
.contextSource(contextSource)
.ldapAuthoritiesPopulator(ldapAuthoritiesPopulator);
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我使用网络表单登录时,我收到此错误:
java.lang.NullPointerException
at java.util.Hashtable.<init>(Hashtable.java:296)
at org.springframework.ldap.core.support.AbstractContextSource.getAuthenticatedEnv(AbstractContextSource.java:499)
at org.springframework.ldap.core.support.AbstractContextSource.doGetContext(AbstractContextSource.java:114)
at org.springframework.ldap.core.support.AbstractContextSource.getContext(AbstractContextSource.java:110)
at org.springframework.security.ldap.authentication.BindAuthenticator.bindWithDn(BindAuthenticator.java:112)
Run Code Online (Sandbox Code Playgroud)
是否有与http://docs.spring.io/spring-security/site/docs/3.2.4.CI-SNAPSHOT/reference/htmlsingle/#ldap类似的文档解释如何在没有spring xml的情况下实现此目的?
你需要打电话
contextSource.afterPropertiesSet()
Run Code Online (Sandbox Code Playgroud)
如果您在应用程序上下文之外使用该类(有关更多信息,请参阅Spring和Javadoc for Spring LDAP的AbstractContextSource).或者你可以把它变成一个@Bean,Spring将调用该方法并为你初始化它.
也
contextSource.setUrl(ldap_user);
Run Code Online (Sandbox Code Playgroud)
看起来不对劲.不应该setUserDn吗?
| 归档时间: |
|
| 查看次数: |
5552 次 |
| 最近记录: |