【问题标题】:Spring data ldap configurationSpring数据ldap配置
【发布时间】:2018-05-22 16:45:05
【问题描述】:

我在 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]

接下来的代码:

用户服务.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

用户服务.java

@Service

公共类用户服务{

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

有人有具有这些功能的示例网络项目吗?

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

【问题讨论】:

    标签: java spring spring-mvc ldap


    【解决方案1】:

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

    Ciao.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-20
      • 1970-01-01
      • 1970-01-01
      • 2014-12-30
      • 2019-04-03
      • 2020-02-15
      • 1970-01-01
      • 2012-06-04
      相关资源
      最近更新 更多