簡體   English   中英

Spring Data ldap配置

[英]Spring data ldap configuration

我在網絡應用中配置spring-data-ldap遇到麻煩。 我總結一下我所做的事情:我創建了一個maven項目(稱為ldap-model),具有spring-data-ldap依賴關系,創建了User.java(Entry)類和相應的存儲庫:UserRepository。 然后,我創建了一個新的spring mvc Web項目,其中包含以前創建的解放依賴項(ldap-model)。 我想使用xml配置,因此:

root-context.xml

<?xml version="1.0" encoding="UTF-8"?> 
 <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:ldap="http://www.springframework.org/schema/ldap"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd">

<!-- Root Context: defines shared resources visible to all other web components -->


<context:property-placeholder location="classpath:ldap.properties" />
<context:annotation-config />

<ldap:context-source id="contextSource"
    password="${ldap.contextSource.password}" url="${ldap.contextSource.url}"
    username="${ldap.contextSource.userDn}" base="${ldap.contextSource.base}"></ldap:context-source>

<ldap:repositories base-package="eu.test.ldap.repository">
</ldap:repositories>

<ldap:ldap-template id="ldapTemplate"
    context-source-ref="contextSource"></ldap:ldap-template>

<bean class="eu.test.webapp.UserService">
</bean>

在這里,我遇到了標簽ldap:repositories的第一個問題,向我建議使用Ctrl +空格鍵,但它不會編譯,並出現以下錯誤:

Multiple annotations found at this line:
- Configuration problem: Cannot locate BeanDefinitionParser for element [repositories] Offending resource: file [C:/Progetti/
 Workspace di test/ldap-webapp/src/main/webapp/WEB-INF/spring/root-context.xml]
- Cannot locate BeanDefinitionParser for element [repositories]

遵循其余的代碼:

UserService.java

public class UserService implements BaseLdapNameAware {
private final UserRepository userRepo;
private LdapName baseLdapPath;

@Autowired
public UserService(UserRepository userRepo) {
    this.userRepo = userRepo;
}

public void find()
{
    User u = userRepo.findByUid("test.test");
    System.out.println(u.getUid());
}

@Override
public void setBaseLdapPath(LdapName baseLdapPath) {
    this.baseLdapPath = baseLdapPath;

}}

ldap.properties

ldap.contextSource.url=*********
ldap.contextSource.userDn=cn=*********
ldap.contextSource.password=*********
ldap.contextSource.base=*********

servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:component-scan base-package="eu.test.webapp" />

HomeController.java

@Controller
 public class HomeController {

private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

@Autowired
private UserService userService;


@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
    logger.info("Welcome home! The client locale is {}.", locale);

     userService.find();


    return "home";
}

}

除了給出的錯誤之外,還有其他問題嗎?

我還嘗試創建Java配置,而不是xml。我創建了一個新的spring mvc Web項目,並在其之前創建了解放依賴項(ldap-model)。

@Configuration
@PropertySource("classpath:ldap.properties")
 @ComponentScan(basePackages = {"eu.test.*"})
 @Profile("default")
 @EnableLdapRepositories(basePackages = "eu.test.ldap.repository.**")
  public class AppConfig {

@Autowired
private Environment env;

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

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

}

像上面的ldap.properties

UserService.java

@Service

公共類UserService {

@Autowired
private UserRepository userRepository;

public String getUsernameByUid(String uid)
{
    return userRepository.findByUid(uid).getUsername();
}

}

HomeController.java

@Controller
public class HomeController {

private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

@Autowired
private UserService userService;

@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {

    String username = userService.getUsernameByUid("nome.cognome");

    return "home";
}

}

我確定缺少某些內容,userService為null

有人有具有這些功能的示例Web項目嗎?

對不起英語,謝謝。

從spring-ldap-core 2.3開始,解析xml配置似乎有所不同。 如果看一下LdapNamespaceHandler類,可以看到沒有為標簽“ repositories”定義處理程序。 這聽起來很奇怪,因為spring-ldap.xsd中沒有等效的更改。 無論如何,如果您想使用xml配置方法,只需修改pom.xml即可解決使用spring-ldap-core 2.2而不是spring-ldap-core 2.3的問題。

再見。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM