使用Spring Security和LDAP保护应用程序安全

时间:2019-12-13 09:47:55

标签: spring spring-boot spring-security ldap

我对Spring Security非常陌生。我想在我的LDAP的Spring Boot应用程序中实现它。每当我尝试理解安全性的概念时,我最终都会陷入困惑。有人可以给我建议或指导我春季安全的工作原理吗?在我的项目中,仅使用spring安全性和LDAP。我观察到的是,spring boot创建了它自己的登录页面,并且在用户通过身份验证后,它设置了一个名为JSESSIONID的cookie,并且对于进一步的请求,它仅使用该会话ID。我们可以在注销期间清除该会话ID。但是我也听到了基于令牌的身份验证的概念,因此不确定是否要使用它。从外部的角度应用程序调用受保护的URL。有人可以帮忙..

1 个答案:

答案 0 :(得分:0)

您可以使用Spring Security LDAP。

将这些依赖项添加到您的pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.ldap</groupId>
    <artifactId>spring-ldap-core</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-ldap</artifactId>
</dependency>
<dependency>
    <groupId>com.unboundid</groupId>
    <artifactId>unboundid-ldapsdk</artifactId>
</dependency>

然后您必须创建一个配置类:

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
      .authorizeRequests()
        .anyRequest().fullyAuthenticated()
        .and()
      .formLogin();
  }

  @Override
  public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth
      .ldapAuthentication()
        .userDnPatterns("uid={0},ou=people")
        .groupSearchBase("ou=groups")
        .contextSource()
          .url("ldap://localhost:8389/dc=springframework,dc=org")
          .and()
        .passwordCompare()
          .passwordEncoder(new LdapShaPasswordEncoder())
          .passwordAttribute("userPassword");
  }

}

请在此处找到整个指南:

https://spring.io/guides/gs/authenticating-ldap/