Java Spring数据ldap配置
在web应用程序中配置spring数据ldap时遇到问题。 我总结了我所做的工作: 我创建了一个maven(称为ldap模型)项目,使用spring数据ldap依赖项,创建了User.java(Entry)类和相应的存储库:UserRepository。 然后,我创建了一个新的SpringMVCWeb项目,其中包含以前创建的liberation依赖项(ldap模型)。 我想使用xml配置,因此: root-context.xmlJava Spring数据ldap配置,java,spring,spring-mvc,ldap,Java,Spring,Spring Mvc,Ldap,在web应用程序中配置spring数据ldap时遇到问题。 我总结了我所做的工作: 我创建了一个maven(称为ldap模型)项目,使用spring数据ldap依赖项,创建了User.java(Entry)类和相应的存储库:UserRepository。 然后,我创建了一个新的SpringMVCWeb项目,其中包含以前创建的liberation依赖项(ldap模型)。 我想使用xml配置,因此: root-context.xml <?xml version="1.0" encoding="
<?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>
<?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" />
遵循代码的其余部分:
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;
}}
@Service
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 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>
<?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" />
}
除了给出的错误之外,还有什么问题吗
我还尝试创建一个java配置,而不是xml
我创建了一个新的SpringMVCWeb项目,其中包含以前创建的liberation依赖项(ldap模型)
}
ldap.properties,如上所述
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;
}}
@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) {
logger.info("Welcome home! The client locale is {}.", locale);
userService.find();
return "home";
}
@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配置方法,您可以使用SpringLDAP核心2.2而不是SpringLDAP核心2.3来解决这个问题,只需修改pom.xml即可 再见