我有以下代码 我正在尝试实现 ldap 身份验证,但我认为它没有发生。
我的安全配置
@EnableWebSecurity
@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class Config extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic().and().authorizeRequests().antMatchers("/*")
.permitAll().anyRequest().authenticated().and().csrf()
.disable().httpBasic().and().csrf()
.csrfTokenRepository(csrfTokenRepository()).and()
.addFilterAfter(csrfHeaderFilter(), CsrfFilter.class);
}
@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.ldapAuthentication()
.userSearchFilter("(uid={0})")
.userSearchBase("dc=intern,dc=xyz,dc=com")
.contextSource()
.url("ldap://192.168.11.11:1234/dc=intern,dc=xyz,dc=com")
.managerDn("username")
.managerPassword("password!")
.and()
.groupSearchFilter("(&(objectClass=user)(sAMAccountName=" + "username" + "))");
}
private Filter csrfHeaderFilter() {
return new OncePerRequestFilter() {
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
CsrfToken csrf = (CsrfToken) request
.getAttribute(CsrfToken.class.getName());
if (csrf != null) {
Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN");
String token = csrf.getToken();
if (cookie == null || token != null
&& !token.equals(cookie.getValue())) {
cookie = new Cookie("XSRF-TOKEN", token);
cookie.setPath("/");
response.addCookie(cookie);
response.sendRedirect("/notAllowed");
}
}
filterChain.doFilter(request, response);
}
};
}
private CsrfTokenRepository csrfTokenRepository() {
HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
repository.setHeaderName("X-XSRF-TOKEN");
return repository;
}
}
我的 Controller
@RequestMapping(value = { "/test" }, method = RequestMethod.GET)
public @ResponseBody String retrieve() {
System.out.println("line 1");
System.out.println("line 2");
return "hello";
}
@RequestMapping(value = { "/notAllowed" }, method = RequestMethod.GET)
public @ResponseBody HttpStatus login() {
return HttpStatus.FORBIDDEN;
}
我的目标是:
我想实现 ldap 身份验证。 U用户名和密码将来自浏览器,尽管我也尝试过使用硬编码的用户名和密码。
如果用户是真实的,那么过滤器将通过检查 token 来检查授权。
如果这是第一次请求,那么将生成新 token 并发送。 如果它未找到,那么它将发送HTTP Status forbidden。
我有以下问题:
当我第一次从浏览器运行时,它返回 forbidden,但它也在控制台中打印“第 1 行和第 2 行”,虽然它不返回 hello,但返回 forbidden。
我的 htpSecurity 和 ldap 配置是否正常?
从第 2 个请求开始,它总是返回 hello,我尝试打开新标签页、新请求,但它仍然工作正常。如果我重新启动服务器,那么只有它生成 token 并将其与 cookie token 进行比较。如果两个人使用同一系统(不同时间)会怎样。
我该如何测试 ldap 身份验证?我正在使用 POSTMAN 作为客户端。
如果我缺少某些信息,请告诉我。 我将感谢您的回答。
最佳答案
首先,我认为您的 HttpSecurity 配置有误。您想要保护所有端点。不是吗?
所以改成如下:
http.httpBasic()
.and()
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.csrf()
.csrfTokenRepository(csrfTokenRepository())
.and()
.addFilterAfter(csrfHeaderFilter(), CsrfFilter.class);
另外,我不确定你的ldap配置是否正确。我认为您可以将其简化为以下内容:
auth.ldapAuthentication()
.userSearchFilter("uid={0}")
.contextSource()
.url("ldap://192.168.11.11:1234/dc=intern,dc=xyz,dc=com");
确保您的 userSearchBase 是否正确。它没有“ou”。
如果您没有任何不同的组织单位,您可以简单地删除 userSearchBase
为了提供更好的帮助,我需要了解您的 ldap 的结构。
如果你想检查你的 HttpSecurity 配置,你可能首先不使用 ldap,而是使用 inMemoryAuthentication:
auth.inMemoryAuthentication().withUser("user").password("password").authorities("ROLE_USER");
关于java - 如何使用spring security(spring boot)实现Ldap认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32757440/