Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在spring中测试LDAP安全配置?_Java_Spring_Spring Boot_Spring Security_Ldap - Fatal编程技术网

Java 如何在spring中测试LDAP安全配置?

Java 如何在spring中测试LDAP安全配置?,java,spring,spring-boot,spring-security,ldap,Java,Spring,Spring Boot,Spring Security,Ldap,如何在spring boot中为ldap安全配置编写测试 authentication manager首先验证ldap中是否存在用户首字母缩写,以及找到的用户是否是为用户筛选器设置的任何组的成员 问题:我怎么能模拟ldap响应呢?例如,我想返回一个memberOf=CN=Team INVALID的用户,该用户不应在测试范围内进行身份验证。 当然,我想返回一个与userSearchFilter匹配的用户 但是这次考试我必须模仿哪个班级呢 @Configuration @Order(1) @Enab

如何在
spring boot
中为
ldap
安全配置编写测试

authentication manager首先验证ldap中是否存在用户首字母缩写,以及找到的用户是否是为用户筛选器设置的任何组的成员

问题:我怎么能模拟ldap响应呢?例如,我想返回一个
memberOf=CN=Team INVALID
的用户,该用户不应在测试范围内进行身份验证。 当然,我想返回一个与
userSearchFilter
匹配的用户

但是这次考试我必须模仿哪个班级呢

@Configuration
@Order(1)
@EnableWebSecurity
public class LdapSecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.ldapAuthentication()
            .userSearchFilter("(&(initials={0})(|" +
                    "(memberOf=CN=TEAM-1,OU=Intern,DC=my-company)" +
                    "(memberOf=CN=TEAM-2,OU=Intern,DC=my-company)" +
                    "))")
            .contextSource()
            .url(ldapUrl + ldapBase)
            .managerDn(ldapUsername)
            .managerPassword(ldapPassword);
    }
}

您可以使用LDIF文件为测试定义嵌入式LDAP服务器,如下所示:

spring.ldap.embedded.ldif=classpath:test-server.ldif
spring.ldap.embedded.base dn=dc=springframework,dc=org
spring.ldap.embedded.port=8389
在测试中,您可以尝试像在正常流中一样对特定用户进行身份验证:

@SpringBootTest
@AutoConfigureMockMvc
@TestPropertySource(属性={
“spring.ldap.embedded.ldif=classpath:test server.ldif”,
“spring.ldap.embedded.base dn=${spring.ldap.base}”,
“spring.ldap.embedded.port=8389”,
“spring.ldap.embedded.url=ldap://localhost:8389/",
“spring.ldap.embedded.credential.username=uid=admin”,
“spring.ldap.embedded.credential.password=secret”,
“spring.ldap.embedded.validation.enabled=false”,
“spring.ldap.url=ldap://localhost:8389/",
“spring.ldap.username=uid=admin”,
“spring.ldap.password=secret”})
公共类身份验证应用程序测试{
@自动连线
私有MockMvc-MockMvc;
@试验
public void loginWithValidUserThenAuthenticated()引发异常{
FormLoginRequestBuilder登录名=formLogin()
.用户(“用户”)
.密码(“用户密码”);
mockMvc.perform(登录)
.andExpect(已验证(),使用用户名(“用户”));
}
@试验
public void loginWithInvalidUserThenUnauthenticated()引发异常{
FormLoginRequestBuilder登录名=formLogin()
.user(“无效”)
.密码(“无效密码”);
mockMvc.perform(登录)
.andExpect(未经验证的());
}
}
我在一本书中找到了这个例子。你可以参考它了解更多细节

所需依赖项:

<dependency>
    <groupId>com.unboundid</groupId>
    <artifactId>unboundid-ldapsdk</artifactId>
    <version>5.1.4</version>
    <scope>test</scope>
</dependency>

com.unbounded
未绑定ldapsdk
5.1.4
测试

为什么要测试spring最有可能已经测试过的东西?如果要测试配置是否有效,请测试ldapauthentication流。我不知道用户如何在您的应用程序中进行身份验证,因为您没有提供该代码。我只想测试我的自定义
userSearchFilter()
。通常不是ldap spring。那么,如果不模拟真实的ldap服务器响应,我如何测试它呢?也许可以尝试使用一个,然后测试您的身份验证流。如果用户可以进行身份验证,则您的搜索过滤器工作,如果用户不能进行身份验证,则该过滤器不工作。好吧,这不是一个解决方案。假设您想要测试使用数据库结果的工作流。而不是模拟sql结果,而是添加一个inmemory db。那是一步之遥…那不是一步之遥,绝对不是。如果您的查询有效,唯一可以肯定回答的是LDAP的正确实现。如果你嘲笑,你基本上是在估计,如果你嘲笑错误并且你的测试通过了,你的测试会给你一个假阳性,这更危险。LDAP实现不会说谎,尤其是在测试中使用与生产中使用的LDIF相同的LDIF时。您希望对LDAP集成进行单元测试。单元测试是针对单元的。集成测试用于集成。如何强制将
应用程序.properties
中的现有ldap替换为用于测试的嵌入式ldap?在测试文件夹中,您可以创建
应用程序.properties
文件,甚至创建
应用程序测试.properties
并使用
@activeaprofile(“测试”)运行测试
我必须添加
未绑定的ldapsdk
库才能工作。此外,我必须配置
spring.ldap.embedded.*
,然后将我的主
spring.ldap.*
属性路由到嵌入式连接。将使用所需的测试属性来更新您的示例。非常好,非常好的工作!