les*_*nar 3 java spring spring-mvc 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;
}
}
Run Code Online (Sandbox Code Playgroud)
我的控制器
@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;
}
Run Code Online (Sandbox Code Playgroud)
我的目标是:
我想实现ldap身份验证.U sername和密码将来自浏览器,虽然我也尝试过使用硬编码的用户名和密码.
如果用户是真实的,那么过滤器将通过检查令牌来检查授权.
如果这是第一次请求,则将生成并发送新令牌.如果未找到,则会发送禁止的HTTP状态.
我有以下问题:
当我第一次从浏览器运行它返回禁止但它也在控制台中打印"第1行和第2行",虽然它不会返回你好但是禁止.
我的htpSecurity和ldap配置好吗?
从第二个请求它总是返回你好,我试图打开新选项卡,新请求,但它仍然工作正常.如果我重新启动服务器,然后只生成令牌并将其与cookie 令牌进行比较.如果两个人使用相同的系统(不同)次).
我怎么能测试ldap身份验证?我使用POSTMAN作为客户端.
如果我遗漏了一些信息,请告诉我.我会感谢你的答案.
首先,我认为你的HttpSecurity配置是错误的.您想要保护所有端点.不是吗?
所以将其更改为以下内容:
http.httpBasic()
.and()
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.csrf()
.csrfTokenRepository(csrfTokenRepository())
.and()
.addFilterAfter(csrfHeaderFilter(), CsrfFilter.class);
Run Code Online (Sandbox Code Playgroud)
此外,我不确定你的ldap配置是否正确.我想你可以把它减少到以下几点:
auth.ldapAuthentication()
.userSearchFilter("uid={0}")
.contextSource()
.url("ldap://192.168.11.11:1234/dc=intern,dc=xyz,dc=com");
Run Code Online (Sandbox Code Playgroud)
确保您的userSearchBase是否正确.它没有"ou".
如果您没有任何不同的组织单位,则只需删除userSearchBase即可
为了提供更好的帮助,我需要知道你的ldap的结构.
如果你想检查你的HttpSecurity配置,你可能不会首先使用ldap并使用inMemoryAuthentication代替:
auth.inMemoryAuthentication().withUser("user").password("password").authorities("ROLE_USER");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15432 次 |
| 最近记录: |