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

weixin_38075910 2019-09-12 01:01:12
我跟随代码我正在尝试实现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身份验证.用户名和密码将来自浏览器,虽然我也尝试过使用硬编码的用户名和密码. 如果用户是真实的,那么过滤器将通过检查令牌来检查授权. 如果这是第一次请求,则将生成并发送新令牌.如果未找到,则会发送禁止的HTTP状态. 我有以下问题: >当我第一次从浏览器运行它返回禁止但它也在控制台中打印“第1行和第2行”虽然它不会返回你好但是禁止.>我的htpSecurity和ldap配置好吗?>从第二个请求它总是返回你好,我试图打开新选项卡,新请求,但它仍然工作正常.如果我重新启动服务器,然后只生成令牌并将其与cookie令牌进行比较.如果两个人使用相同的系统(如果不同时期).>我究竟如何测试ldap身份验证?我使用POSTMAN作为客户端. 如果我遗漏了一些信息,请告诉我.我会感谢你的答案.
...全文
421 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38076382 2019-09-12
  • 打赏
  • 举报
回复
首先,我认为你的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");

473

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧