Spring数据ldap配置

时间:2017-12-04 21:53:15

标签: java spring spring-mvc ldap

我在网络应用中配置spring-data-ldap时遇到问题。 我总结一下我做了什么: 我创建了一个maven(称为ldap-model)项目,使用spring-data-ldap依赖项,我创建了User.java(Entry)类和相应的Repository:UserRepository。 然后我创建了一个新的spring mvc web项目,之前创建了解放依赖项(ldap-model)。 我想使用xml配置,所以:

根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 + Space,但它没有编译给出这个错误:

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为空

有没有人拥有具有这些功能的示例Web项目?

对不起英文,谢谢。

1 个答案:

答案 0 :(得分:0)

自spring-ldap-core 2.3以来解析xml配置似乎有所不同。 如果你看一下LdapNamespaceHandler类,你会发现没有为标记&#34; repositories&#34;定义处理程序。 这听起来很奇怪,因为spring-ldap.xsd没有相应的变化。 无论如何,如果你想使用xml配置方法,你可以使用spring-ldap-core 2.2而不是spring-ldap-core 2.3来解决,只需修改你的pom.xml。