【问题标题】:securing an application with spring security and LDAP使用 Spring Security 和 LDAP 保护应用程序
【发布时间】:2020-04-06 17:59:45
【问题描述】:
我对 Spring Security 很陌生。我想用 LDAP 在我的 Spring Boot 应用程序中实现它。每当我试图理解安全的概念时,我最终都会陷入困惑。有人可以给我推荐一个指南,或者给我一个关于spring security做什么的要点。在我的项目中,我只使用 spring security 和 LDAP。我观察到的是,spring boot 创建了它自己的登录页面,一旦用户通过身份验证,它就会设置一个名为 JSESSIONID 的 cookie,对于进一步的请求,它只使用该会话 ID。我们可以在注销期间清除该会话 ID。但我也听说过基于令牌的身份验证的概念,所以不确定我是否要使用它。从外部 Angular 应用程序调用受保护的 URL。有人可以帮忙吗..
【问题讨论】:
标签:
spring
spring-boot
spring-security
ldap
【解决方案1】:
您可以使用 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/