繁体   English   中英

如何使用spring security(spring boot)实现Ldap身份验证

[英]how to achieve Ldap Authentication using spring security(spring boot)

我有以下代码与我我试图实现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;
    }
}

我的控制器

    @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 sername和密码将来自浏览器,虽然我也尝试过使用硬编码的用户名和密码。

如果用户是真实的,那么过滤器将通过检查令牌来检查授权

如果这是第一次请求,则将生成并发送新令牌 如果未找到 ,则会发送禁止HTTP状态

我有以下问题:

  1. 当我第一次从浏览器运行它返回禁止但它也在控制台中打印“第1行和第2行”,虽然它不会返回你好但是禁止。

  2. 我的htpSecurity和ldap配置好吗?

  3. 从第二个请求它总是返回你好,我试图打开新的选项卡,新的请求但仍然工作正常。如果我重新启动服务器然后只生成令牌并将其与cookie 令牌进行比较。如果两个人使用相同的系统(不同次)。

  4. 我怎么能测试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");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM