2017-05-19 11 views
1

나는 win server 2003 with AD를 사용합니다. 그리고 Spring LDAP를 통해 연결하려고합니다.스프링 LDAP에서 인증 할 수 없음

2017-05-19 22:48:46.768 ERROR 18868 --- [nio-8090-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause 

java.lang.NullPointerException: null 
    at hello.ldap.client.LdapClient.authenticate(LdapClient.java:26) ~[classes/:na] 
    at hello.HelloController.index(HelloController.java:13) ~[classes/:na] 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131] 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131] 
<...> 

코드를 baeldung.com

은 ldapclient에서

package hello.ldap.javaconfig; 

import hello.ldap.client.LdapClient; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.*; 
import org.springframework.core.env.Environment; 
import org.springframework.ldap.core.LdapTemplate; 
import org.springframework.ldap.core.support.LdapContextSource; 

@Configuration 
@PropertySource("classpath:application.properties") 
@ComponentScan(basePackages = { "hello.ldap.*" }) 
@Profile("default") 
public class AppConfig { 

    @Autowired 
    private Environment env; 

    @Bean 
    public LdapContextSource contextSource() { 
     LdapContextSource contextSource = new LdapContextSource(); 
     contextSource.setUrl(env.getRequiredProperty("ldap.url")); 
     contextSource.setBase(env.getRequiredProperty("ldap.partitionSuffix")); 
     contextSource.setUserDn(env.getRequiredProperty("ldap.principal")); 
     contextSource.setPassword(env.getRequiredProperty("ldap.password")); 
     return contextSource; 
    } 

    @Bean 
    public LdapTemplate ldapTemplate() { 
     return new LdapTemplate(contextSource()); 
    } 

    @Bean 
    public LdapClient ldapClient() { 
     return new LdapClient(); 
    } 

} 
012,351,641

package hello.ldap.client; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.core.env.Environment; 
import org.springframework.ldap.core.*; 
import org.springframework.ldap.support.LdapNameBuilder; 

import javax.naming.Name; 
import java.security.MessageDigest; 
import java.security.NoSuchAlgorithmException; 
import java.util.Base64; 
import java.util.List; 

public class LdapClient { 

    @Autowired 
    private Environment env; 

    @Autowired 
    private ContextSource contextSource; 

    @Autowired 
    private LdapTemplate ldapTemplate; 

    public void authenticate(final String username, final String password) { 
     contextSource.getContext("cn=" + username + ",cn=users," + env.getRequiredProperty("ldap.partitionSuffix"), password); 
    } 

    public List<String> search(final String username) { 
     return ldapTemplate.search(
      "cn=users", 
      "cn=" + username, 
      (AttributesMapper<String>) attrs -> (String) attrs 
      .get("cn") 
      .get()); 
    } 

    public void create(final String username, final String password) { 
     Name dn = LdapNameBuilder 
      .newInstance() 
      .add("cn", "users") 
      .add("cn", username) 
      .build(); 
     DirContextAdapter context = new DirContextAdapter(dn); 

     context.setAttributeValues("objectclass", new String[] { "top", "person", "organizationalPerson", "inetOrgPerson" }); 
     context.setAttributeValue("cn", username); 
     context.setAttributeValue("sn", username); 
     context.setAttributeValue("userPassword", digestSHA(password)); 

     ldapTemplate.bind(context); 
    } 

    public void modify(final String username, final String password) { 
     Name dn = LdapNameBuilder 
      .newInstance() 
      .add("ou", "users") 
      .add("cn", username) 
      .build(); 
     DirContextOperations context = ldapTemplate.lookupContext(dn); 

     context.setAttributeValues("objectclass", new String[] { "top", "person", "organizationalPerson", "inetOrgPerson" }); 
     context.setAttributeValue("cn", username); 
     context.setAttributeValue("sn", username); 
     context.setAttributeValue("userPassword", digestSHA(password)); 

     ldapTemplate.modifyAttributes(context); 
    } 

    private String digestSHA(final String password) { 
     String base64; 
     try { 
      MessageDigest digest = MessageDigest.getInstance("SHA"); 
      digest.update(password.getBytes()); 
      base64 = Base64 
       .getEncoder() 
       .encodeToString(digest.digest()); 
     } catch (NoSuchAlgorithmException e) { 
      throw new RuntimeException(e); 
     } 
     return "{SHA}" + base64; 
    } 
} 

appconfig가를 : 나는 http://localhost:8090/에 연결하려고 할 때 오류가 발생합니다

application.properties

server.port = 8090 
ldap.partitionSuffix=dc=GRSU,dc=local 
ldap.partition=GRSU 
ldap.principal=cn=Jack Wood,cn=users 
ldap.password=1234 
ldap.port=389 
ldap.url=ldap://192.168.56.101 

시작 지점

package hello; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 

@SpringBootApplication 
public class Application { 

    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 

} 

HelloController

package hello; 

import hello.ldap.client.LdapClient; 
import org.springframework.web.bind.annotation.RestController; 
import org.springframework.web.bind.annotation.RequestMapping; 

@RestController 
public class HelloController { 

    @RequestMapping("/") 
    public String index() { 
     LdapClient ldapClient = new LdapClient(); 
     ldapClient.authenticate("Jack Wood", "1234"); 
     return "Greetings from Spring Boot!"; 
    } 

} 

AD 구조. 내가 contextSource에서 문제를 이해로서

AD

응용 프로그램 구조

Application structure

. 어떻게 해결할 수 있습니까?

도움을 주시면 감사하겠습니다.

+0

나는 개인적으로 신뢰하지 것이다 유효한 튜토리얼 사이트로 baeldung. 그들의 습관은 종종 약간 개략적입니다. 이것은 내 자신의 의견입니다. – kkflf

+0

@kkflf Spring LDAP를위한 튜토리얼은 무엇을 조언 할 수 있습니까? – Woland

답변

1

스프링 응용 프로그램을 시작하지 않았습니다. 이것은 봄의 출발점이며, 일을 초기화하지 않는다.

SpringApplication.run(Application.class, args); 

와 (당신이 springboot를 사용하는 경우) 다음과 같은 주석을 추가 : 당신은 당신의 주요 방법 같은 것을 추가해야합니다

@SpringBootApplication 

https://spring.io/guides/gs/spring-boot/

+0

의견을 보내 주셔서 감사합니다. 나는 그것을 고치려고했지만 오류가 남아있다. 주제가 업데이트되었습니다. 제발. – Woland

+0

이 튜토리얼을 대신보아야합니다. https://spring.io/guides/gs/authenticating-ldap/ – kkflf

+0

[topic] (http://stackoverflow.com/questions/44084343/spring-news) ldap-and-spring-boot-configuration)? – Woland