使用 Ldap 身份验证的 createSuccessfulAuthentication 中的空指针异常

nullpointer exception in createSuccessfulAuthentication with Ldap Authentication

我是 spring 安全认证的新手。我正在使用 AbstractUserDetailsAuthenticationProvider 进行身份验证

但我遇到异常

java.lang.NullPointerException: null
    at org.springframework.security.ldap.authentication.AbstractLdapAuthenticationProvider.createSucccessfulAuthentication(AbstractLdapAuthenticationProvider.java:117)
    at org.springframework.security.ldap.authentication.AbstractLdapAuthenticationProvider.authenticate(AbstractLdapAuthenticationProvider.java:92)
    at com.example.securitydemo.security.CustomUserDetailsAuthenticationProvider.authenticate(CustomUserDetailsAuthenticationProvider.java:36)

下面是我的CustomUserDetailsAuthenticationProvider.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider;
import org.springframework.security.ldap.userdetails.LdapUserDetailsImpl;
import org.springframework.security.ldap.userdetails.UserDetailsContextMapper;

public class CustomUserDetailsAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider {

    private ActiveDirectoryLdapAuthenticationProvider adAuthprovider;

    private UserDetailsContextMapper userDetailsContextMapper;

    @Autowired
    private AppGrantedAuthoritiesMapper mapper;

    public CustomUserDetailsAuthenticationProvider() {
    }

    public CustomUserDetailsAuthenticationProvider(ActiveDirectoryLdapAuthenticationProvider adAuthprovider) {
        this.adAuthprovider = adAuthprovider;
        if (this.adAuthprovider == null) {
            userDetailsContextMapper = new AppUserDetailsMapper();
            this.adAuthprovider.setUserDetailsContextMapper(userDetailsContextMapper);
        }
    }

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        adAuthprovider.setAuthoritiesMapper(mapper);
        Authentication auth = adAuthprovider.authenticate(authentication);
        return auth;
    }

    @Override
    protected void additionalAuthenticationChecks(UserDetails userDetails,
            UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {

    }

    @Override
    protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication)
            throws AuthenticationException {
        LdapUserDetailsImpl ud = (LdapUserDetailsImpl) authentication.getPrincipal();
        return ud;
    }

}

下面是我的ReferAuthenticationSuccessHandler.java

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.Authentication;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

public class ReferAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
            Authentication authentication) throws IOException, ServletException {
        handle(request, response, authentication);
    }

    protected void handle(final HttpServletRequest request, final HttpServletResponse response,
            final Authentication authentication) throws IOException {
        final String targetUrl = "home.html";
        redirectStrategy.sendRedirect(request, response, targetUrl);
    }

}

下面是我的SecurityConfig.java

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
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.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

@Configuration
@EnableWebSecurity
@ComponentScan("com.example.securitydemo.security")
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(getAmsAuthProvider());
    }

    @Bean
    public AuthenticationSuccessHandler myAuthenticationSuccessHandler() {
        return new ReferAuthenticationSuccessHandler();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().disable().authorizeRequests().anyRequest().authenticated().and().formLogin()
                .successHandler(myAuthenticationSuccessHandler());

    }

    private CustomUserDetailsAuthenticationProvider getAmsAuthProvider() {

        ActiveDirectoryLdapAuthenticationProvider adLdap = new ActiveDirectoryLdapAuthenticationProvider("pac.***.com",
                "ldap://**.pac.com:389");
        adLdap.setSearchFilter("(&(objectClass=user)(sAMAccountName={1}");
        adLdap.setUseAuthenticationRequestCredentials(true);
        adLdap.setConvertSubErrorCodesToExceptions(true);
        CustomUserDetailsAuthenticationProvider authenticationProvider = new CustomUserDetailsAuthenticationProvider(
                adLdap);
        return authenticationProvider;
    }

}

不确定我在这里遗漏了什么,请帮助我解决这个问题

确保用户有权限。 这是 AbstractLdapAuthenticationProvider 的源代码 https://github.com/spring-projects/spring-security/blob/master/ldap/src/main/java/org/springframework/security/ldap/authentication/AbstractLdapAuthenticationProvider.java

看看你的 Stacktrace 说的第 117 行。我建议调试特定行并检查哪个变量为空。