![](/img/trans.png)
[英]Authentication with Spring-Security via Active Directory LDAP
[英]Spring Security Active directory LDAP Authentication error
當我使用 Spring Security Active Directory LDAP 身份驗證時,當用戶通過身份驗證時,我收到一條錯誤消息 PartialResultException 。 我還創建了一個測試運行程序文件,它可以在沒有錯誤的情況下對用戶進行身份驗證,但是當我針對活動目錄進行身份驗證時,我無法進行身份驗證。 感謝您的寶貴幫助。
測試 Runner2.java 文件
package com.company.test;
import java.util.Hashtable;
import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.InitialDirContext;
public class Runner2 {
private static String providerUrl = "ldap://ADHO.COMPANY.NET:389";
private static String principle = "@company.ad";
public static final String SEARCH_BY_SAM_ACCOUNT_NAME = "(sAMAccountName=%s)";
public static boolean authenticateAD(String user , String password) throws Exception {
InitialDirContext context=null;
Hashtable<String, String> env = new Hashtable<String, String>();
String securityPrinciple = user + principle;
System.out.println("Security principal to search ->"+securityPrinciple);
// Configure our directory context environment.
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, providerUrl);
env.put(Context.SECURITY_PRINCIPAL, securityPrinciple);
env.put(Context.SECURITY_CREDENTIALS, password);
try {
context = new InitialDirContext(env);
return true;
}catch(AuthenticationException e) {
throw new AuthenticationException();
}catch(Exception e) {
throw new Exception();
}
finally {
try {
if (context != null) {
context.close();
}
} catch (NamingException e) {
}
}
}
public static void main(String[] args) throws Exception {
String r1 = "USRXXXX;
String r2 = "Pass#word102";
authenticateAD(r1,r2);
}
}
ldap.properties 文件
ad.domain=company.ad
ad.url=ldap://ADHO.COMPANY.NET:389/
SpringSecurityConfig.java 文件
package com.company.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.ProviderManager;
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.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider;
import com.onezero.config.AuthenticationEntryPoint;
import java.util.Arrays;
@Configuration
@EnableWebSecurity
@PropertySource("classpath:ldap.properties")
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private Environment env;
@Bean
public AuthenticationEntryPoint customAuthenticationEntry() throws Exception {
return new AuthenticationEntryPoint();
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers(HttpMethod.OPTIONS, "/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/welcome","/atm/**","/atm2/**","/**","/survey/**","/usrauth/**").authenticated()
.and()
.httpBasic()
.authenticationEntryPoint(customAuthenticationEntry())
.and()
.exceptionHandling()
.and()
.csrf().disable()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider());
}
@Bean
public AuthenticationManager authenticationManager() {
return new ProviderManager(Arrays.asList(activeDirectoryLdapAuthenticationProvider()));
}
@Bean
public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider(
env.getProperty("ad.domain"), env.getProperty("ad.url"));
provider.setConvertSubErrorCodesToExceptions(true);
provider.setUseAuthenticationRequestCredentials(true);
return provider;
}
}
當服務器返回引用時,可以拋出PartialResultException
。 這是服務器說“我不知道你在說什么,但我知道誰知道”的方式。 在這個答案中有一個很好的選項解釋。 一種是設置:
env.put(Context.REFERRAL, "follow");
這將告訴它將請求發送到服務器告訴我們去的任何地方。 但我認為這不是一個好的解決方案。 當只有一個請求時,您最終會發出兩個網絡請求。 更改配置以指向正確的位置是更好的選擇。
找出它在抱怨的一種方法是檢查resolvedName
的財產PartialResultException
對象。 這應該告訴您它發送給您的位置,您可以更改配置以首先指向那里。
我不是 Java 開發人員,所以有很多猜測,但我知道 AD 是如何工作的。 還是試試吧。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.