在Spring Boot中通过LDAP自定义登录页面
我想在我的Spring Boot应用程序中实现LDAP身份验证。我有如下配置类:在Spring Boot中通过LDAP自定义登录页面,spring,authentication,spring-boot,ldap,Spring,Authentication,Spring Boot,Ldap,我想在我的Spring Boot应用程序中实现LDAP身份验证。我有如下配置类: @Configuration public class WebSecurityConfig extends WebSecurityConfigurerAdapter { //@Value("${ad.domain}") private String AD_DOMAIN; //@Value("${ad.url}") private String AD_URL; WebSecurityConfig() {
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
//@Value("${ad.domain}")
private String AD_DOMAIN;
//@Value("${ad.url}")
private String AD_URL;
WebSecurityConfig() {
AD_DOMAIN = "domain.com";
AD_URL = "ldap://URL";
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().fullyAuthenticated().and().formLogin().permitAll().and().logout().permitAll();
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider()).userDetailsService(userDetailsService());
}
@Bean
public AuthenticationManager authenticationManager() {
return new ProviderManager(Arrays.asList(activeDirectoryLdapAuthenticationProvider()));
}
@Bean
public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider(AD_DOMAIN, AD_URL);
provider.setConvertSubErrorCodesToExceptions(true);
provider.setUseAuthenticationRequestCredentials(true);
return provider;
}
}
控制器:
@Controller
public class HomeController {
@GetMapping("/")
public String index() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String user = auth.getName();
return "Welcome to the home page "+ user;
}
和我的pom文件具有以下依赖项:
<dependencies>
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
其中/login
是我的自定义登录页面。但这是行不通的。我认为AuthenticationManager绑定到Spring的默认登录页面
有人能推荐我吗?这会有帮助的。谢谢假设您参考github上的示例。这也是一个很好的开始。很抱歉,这些对我不起作用。我有我的ldap为我工作的事情,我需要的是如何进入我的自定义登录页面。你能修复它吗?请帮忙。
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().authenticated().and().formLogin().loginPage("/login").permitAll().and().logout().permitAll();
}