XML 대신 주석을 사용하여 Spring LdapTemplate를 구성하는 모범 사례
Spring Boot 어플리케이션의 경우 Spring을 정상적으로 설정했습니다.LdapTemplate
주석 사용(예:LdapContextSource
과의 의존.@Value
s는 application.properties에서 가져옵니다.(아뿔싸! 예를 찾을 수 없어서 다른 사람에게 도움이 될지도 몰라.)
아래 스니펫은 콘텍스트소스를 셋업하고 콘텍스트소스를LdapTemplate
그것을 디렉토리 서비스에 자동 접속합니다.
더 나은/깨끗한 방법으로 셋업할 수 있습니까?ContextSource
Spring Boot 앱에 있나요?
application.properties(클래스 패스):
ldap.url=ldap://server.domain.com:389
ldap.base:OU=Employees,OU=Users,DC=domain,DC=com
ldap.username:CN=myuserid,OU=employees,OU=Users,DC=domain,DC=com
ldap.password:secretthingy
MyLdapContextSource.java:
@Component
public class MyLdapContextSource extends LdapContextSource implements ContextSource {
@Value("${ldap.url}")
@Override
public void setUrl(String url) { super.setUrl(url); }
@Value("${ldap.base}")
@Override
public void setBase(String base) {super.setBase(base); }
@Value("${ldap.username}")
@Override
public void setUserDn(String userDn) {super.setUserDn(userDn); }
@Value("${ldap.password}")
@Override
public void setPassword(String password) { super.setPassword(password); }
}
MyLdapTemplate.자바:
@Component
public class MyLdapTemplate extends LdapTemplate {
@Autowired
public MyLdapTemplate(ContextSource contextSource) { super(contextSource); }
}
디렉토리 서비스자바:
@Service
public class DirectoryService {
private final LdapTemplate ldapTemplate;
@Value("${ldap.base}")
private String BASE_DN;
@Autowired
public DirectoryService(LdapTemplate ldapTemplate) { this.ldapTemplate = ldapTemplate; }
public Person lookupPerson(String username) {
return (Person) ldapTemplate.lookup("cn=" + username, new PersonAttributesMapper());
}
public List<Person> searchDirectory(String searchterm) {
SearchControls searchControls = new SearchControls();
searchControls.setCountLimit(25);
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
List<Person> people = (List<Person>) ldapTemplate.search(
BASE_DN, "cn=" + searchterm, searchControls, new PersonAttributesMapper());
return people;
}
}
왜 모든 하위 클래스가 있죠?Configuration을 사용하여 콩을 설정합니다.XML 또는 Java Config 중 하나.
@Configuration
public class LdapConfiguration {
@Autowired
Environment env;
@Bean
public LdapContextSource contextSource () {
LdapContextSource contextSource= new LdapContextSource();
contextSource.setUrl(env.getRequiredProperty("ldap.url"));
contextSource.setBase(env.getRequiredProperty("ldap.base"));
contextSource.setUserDn(env.getRequiredProperty("ldap.user"));
contextSource.setPassword(env.getRequiredProperty("ldap.password"));
return contextSource;
}
@Bean
public LdapTemplate ldapTemplate() {
return new LdapTemplate(contextSource());
}
}
당신의.DirectoryService
그대로 유지될 수 있습니다.LdapTemplate
자동 전원 공급
일반적인 경험으로 볼 때 인프라스트럭처를 확장하지 않는 것이 좋습니다(예:DataSource
또는LdapTemplate
(단, 명시적으로 설정합니다.이는 어플리케이션의 콩(서비스, 저장소 등)과 반대됩니다.
LDAP 의 명시적인 배선은, 스트레이트 케이스에서는 전혀 필요 없습니다.이것은 스프링 부츠가 애초에 독선적인 태도를 취함으로써 없애려는 것이다.
다음 기능이 있는지 확인합니다.spring-boot-starter-data-ldap
또는spring-ldap-core
예를 들어, Maven을 위해 포함된 의존관계pom:xml
:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-ldap</artifactId>
</dependency>
에서 LDAP 설정application.properties
다음 키를 사용합니다.
# Note the spring prefix for each and use just the CN for username
spring.ldap.url=ldap://server.domain.com:389
spring.ldap.base=OU=Employees,OU=Users,DC=domain,DC=com
spring.ldap.username=myuserid
spring.ldap.password=secretthingy
그런 다음 스프링에 의존하여 자동 배선(예1: 현장 주입 사용):
@Autowired
private final LdapTemplate ldapTemplate;
1 현장 주사는 일반적으로 권장되지 않지만 여기서는 조영제로 사용합니다.
언급URL : https://stackoverflow.com/questions/25515345/best-practice-for-configuring-spring-ldaptemplate-via-annotations-instead-of-xml
'programing' 카테고리의 다른 글
angularjs로 줄 바꿈 유지 (0) | 2023.03.08 |
---|---|
ui-select angular에서 선택한 옵션 지우기 (0) | 2023.03.08 |
Oracle의 RAW(16)에서 로 변환합니다.NET의 GUID (0) | 2023.03.08 |
서버와 클라이언트 간의 데이터 자동 동기화 (0) | 2023.03.08 |
C# 클래스에서 JSON 스키마를 생성하는 중 (0) | 2023.02.26 |