java - Spring数据ldap配置

标签 java spring spring-mvc ldap

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

根上下文.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.属性

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

公共(public)类 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 项目吗?

对不起,英语不好,谢谢。

最佳答案

自 spring-ldap-core 2.3 以来,解析 xml 配置似乎有所不同。 如果您查看 LdapNamespaceHandler 类,您可以看到没有为标记“repositories”定义处理程序。 这听起来很奇怪,因为 spring-ldap.xsd 中没有等效的更改。 不管怎样,如果你想使用xml配置方式,你可以使用spring-ldap-core 2.2而不是spring-ldap-core 2.3来解决,只需修改你的pom.xml即可。

再见。

关于java - Spring数据ldap配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47642597/

相关文章:

java - quartz cron 作业未启动

java - 你在哪里初始化你的对象?

java - 从java文件中读取每个字符串文本

java - Spring Data JPA - 如何通过父对象的 id 查找嵌套对象?

java - 无法将 json 字符串绑定(bind)到 Spring MVC Controller 中的对象?

java - MySQL: org.hibernate.HibernateException: 找到超过一行的给定标识符 3

java - Spring Boot - 添加外部属性文件

java - 在现有 Spring with JSP 应用程序中使用 Spring MVC

java - Slf4j LoggerFactory.getLogger 和 sonarqube

java - 如何设置 oracle.net.ns.SQLnetDef.TCP_CONNTIMEOUT_STR