2012-05-16 31 views
8

Tôi đã từng chứng thực LDAP Xuân An được xác định trong tập tin xml và nó làm việc tốt: LdapAuthenticationProvider ném NullPointerException tại AbstractContextSource.getReadOnlyContext

<security:authentication-manager> 
    <security:ldap-authentication-provider 
     user-search-filter="(uid={0})" 
     user-search-base="dc=company,dc=com"> 
    </security:ldap-authentication-provider> 
</security:authentication-manager> 

<security:ldap-server url="ldap://mail.company.com" /> 

tôi cần để chèn một số logic vào nhà cung cấp xác thực (đăng nhập vào cơ sở dữ liệu để đặt tên một) vì vậy tôi thực hiện DaoAuthenticationProvider sử dụng LDAP:

cấu hình xml:

<security:authentication-manager> 
    <security:authentication-provider ref="appAuthenticationProvider" /> 
</security:authentication-manager> 
thực hiện

lớp:

@Service("appAuthenticationProvider") 
public class AppAuthenticationProvider extends DaoAuthenticationProvider { 

    private LdapAuthenticationProvider ldapProvider; 

    public AppAuthenticationProvider(){ 
     DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource("ldap://mail.company.com"); 
     BindAuthenticator authenticator = new BindAuthenticator(contextSource); 
     authenticator.setUserSearch(new FilterBasedLdapUserSearch("dc=company,dc=com", "(uid={0})", contextSource)); 
     ldapProvider = new LdapAuthenticationProvider(authenticator); 
    } 

    public Authentication authenticate(Authentication authRequest) throws AuthenticationException { 
     return ldapProvider.authenticate(authRequest); 
    } 

} 

Có vẻ Qute gì bạn mong muốn từ việc thực hiện đầu tiên nhưng xác thực phương pháp ném sau ngoại lệ:

java.lang.NullPointerException 
org.springframework.ldap.core.support.AbstractContextSource.getReadOnlyContext(AbstractContextSource.java:125) 
org.springframework.ldap.core.LdapTemplate.executeReadOnly(LdapTemplate.java:792) 
org.springframework.security.ldap.SpringSecurityLdapTemplate.searchForSingleEntry(SpringSecurityLdapTemplate.java:196) 
org.springframework.security.ldap.search.FilterBasedLdapUserSearch.searchForUser(FilterBasedLdapUserSearch.java:116) 
org.springframework.security.ldap.authentication.BindAuthenticator.authenticate(BindAuthenticator.java:90) 
org.springframework.security.ldap.authentication.LdapAuthenticationProvider.doAuthentication(LdapAuthenticationProvider.java:178) 
org.springframework.security.ldap.authentication.AbstractLdapAuthenticationProvider.authenticate(AbstractLdapAuthenticationProvider.java:61) 
myapp.security.AppAuthenticationProvider.authenticate(AppAuthenticationProvider.java:69) 

Logs trong trường hợp đầu tiên trông như thế này:

[myapp] 2012-05-16 11:38:44,339 INFO org.springframework.security.ldap.DefaultSpringSecurityContextSource - URL 'ldap://mail.company.com', root DN is '' 
[myapp] 2012-05-16 11:38:44,364 INFO org.springframework.security.ldap.DefaultSpringSecurityContextSource - URL 'ldap://mail.company.com', root DN is '' 
[myapp] 2012-05-16 11:38:44,365 DEBUG org.springframework.ldap.core.support.AbstractContextSource - AuthenticationSource not set - using default implementation 
[myapp] 2012-05-16 11:38:44,365 INFO org.springframework.ldap.core.support.AbstractContextSource - Property 'userDn' not set - anonymous context will be used for read-write operations 
[myapp] 2012-05-16 11:38:44,365 DEBUG org.springframework.ldap.core.support.AbstractContextSource - Using LDAP pooling. 
[myapp] 2012-05-16 11:38:44,365 DEBUG org.springframework.ldap.core.support.AbstractContextSource - Trying provider Urls: ldap://mail.company.com 
[myapp] 2012-05-16 11:38:44,369 INFO org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator - groupSearchBase is empty. Searches will be performed from the context source base 
[myapp] 2012-05-16 11:39:33,956 DEBUG org.springframework.security.authentication.ProviderManager - Authentication attempt using org.springframework.security.ldap.authentication.LdapAuthenticationProvider 
[myapp] 2012-05-16 11:39:33,957 DEBUG org.springframework.security.ldap.authentication.LdapAuthenticationProvider - Processing authentication request for user: JohnDoe 
[myapp] 2012-05-16 11:39:33,960 DEBUG org.springframework.security.ldap.search.FilterBasedLdapUserSearch - Searching for user 'JohnDoe', with user search [ searchFilter: '(uid={0})', searchBase: 'dc=company,dc=com', scope: subtree, searchTimeLimit: 0, derefLinkFlag: false ] 
[myapp] 2012-05-16 11:39:34,812 DEBUG org.springframework.ldap.core.support.AbstractContextSource - Got Ldap context on server 'ldap://mail.company.com' 
[myapp] 2012-05-16 11:39:35,025 DEBUG org.springframework.security.ldap.SpringSecurityLdapTemplate - Searching for entry under DN '', base = 'dc=company,dc=com', filter = '(uid={0})' 
[myapp] 2012-05-16 11:39:35,060 DEBUG org.springframework.security.ldap.SpringSecurityLdapTemplate - Found DN: cn=JohnDoe,cn=users,dc=company,dc=com 
[myapp] 2012-05-16 11:39:35,082 DEBUG org.springframework.security.ldap.authentication.BindAuthenticator - Attempting to bind as cn=JohnDoe,cn=users,dc=company,dc=com 

Trong trường hợp thứ hai:

[myapp] 2012-05-16 11:34:13,563 INFO org.springframework.security.ldap.DefaultSpringSecurityContextSource - URL 'ldap://mail.company.com', root DN is '' 
[myapp] 2012-05-16 11:34:28,363 INFO org.springframework.security.ldap.DefaultSpringSecurityContextSource - URL 'ldap://mail.company.com', root DN is '' 
[myapp] 2012-05-16 11:34:37,194 DEBUG org.springframework.security.authentication.ProviderManager - Authentication attempt using myapp.security.AppAuthenticationProvider 
[myapp] 2012-05-16 11:34:37,197 DEBUG org.springframework.security.ldap.authentication.LdapAuthenticationProvider - Processing authentication request for user: JohnDoe 
[myapp] 2012-05-16 11:34:37,197 DEBUG org.springframework.security.ldap.search.FilterBasedLdapUserSearch - Searching for user 'JohnDoe', with user search [ searchFilter: '(uid={0})', searchBase: 'dc=company,dc=com', scope: subtree, searchTimeLimit: 0, derefLinkFlag: false ] 

Bạn có ý tưởng gì không?

Trả lời

18

Bạn chưa hoàn toàn khởi tạo DefaultSpringSecurityContextSource (vì bạn đang tạo thủ công bằng cách sử dụng "mới").

Thêm này dưới sự sáng tạo, và bạn sẽ có tất cả các thiết lập:

contextSource.afterPropertiesSet(); 

Trong trường hợp này, dòng này là chìa khóa:

[myapp] 2012-05-16 11:38:44,365 INFO org.springframework.ldap.core.support.AbstractContextSource - Property 'userDn' not set - anonymous context will be used for read-write operations 

Khi bạn cố gắng sử dụng tay của bạn tạo ra (nhưng không được khởi tạo đúng) nguồn ngữ cảnh mà nó sẽ thực hiện theo hành vi mặc định, để sử dụng truy cập không ẩn danh đối với các hoạt động chỉ đọc. Vì bạn đang chỉ định bất kỳ người quản lý dn/mật khẩu, nó không thành công với một NPE. Khởi tạo đúng cá thể (bằng cách gọi afterPropertiesSet()) sẽ đặt nó để sử dụng truy cập ẩn danh cho chỉ đọc vì không có người dùng/pwd nào được chỉ định.

+0

Nó hoạt động! Cảm ơn rất nhiều. Tôi đã dành vài giờ bực bội, bạn đã cứu tôi thêm một vài điều nữa;) Tài liệu hoặc thông báo lỗi của Spring Secuirty có thể cung cấp thông tin thực tế hơn. –

+0

Hai ngày làm việc để tìm giải pháp này cảm ơn rất nhiều –

+0

Hoạt động trong Grails 3.2.9 với lõi bảo mật mùa xuân: 3.1.2 và spring-security-ldap: 3.0.2. Đối với một số lý do, cài đặt application.yml grails.plugin.springsecurity.ldap.context.anonymousReadOnly: true đã bị bỏ qua. afterPropertiesSet() đã sửa nó. –

Các vấn đề liên quan