【发布时间】:2019-10-02 07:18:45
【问题描述】:
我有 Spring Boot 2 REST 应用程序,并且我想配置 Spring Security 以支持对相同资源的 Google 登录或 LDAP 身份验证(例如 /employees)
我已经通过 httpBasic(连接到 Apache AD LDAP 服务器)完成了身份验证。
我还设置了通过 Google OAuth2 登录的身份验证。 这两种配置都可以单独工作(我可以通过 Google 登录进行身份验证,但不能同时使用 LDAP,因为我必须重新设置 spring 安全性),现在我需要能够通过这两种方式进行身份验证同时。
我的 LDAP 身份验证的 Spring Security 配置
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.authorizeRequests()
.antMatchers("/", "/login**","/callback/", "/webjars/**", "/error**")
.permitAll()
.anyRequest()
.authenticated()
.and()
.httpBasic()
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.ldapAuthentication()
.ldapAuthoritiesPopulator(customLdapAuthoritiesPopulator)
.userDnPatterns("uid={0},ou=people")
.groupSearchBase("ou=groups")
.contextSource()
.url(env.getProperty("spring.ldap.urls") + env.getProperty("spring.ldap.base"))
.and()
.passwordCompare()
.passwordAttribute("userPassword")
.passwordEncoder(new LdapShaPasswordEncoder());
}
当我为 Google OAuth2 登录重新配置 Spring Security 时它的外观
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.authorizeRequests()
.antMatchers("/", "/login**","/callback/", "/webjars/**", "/error**")
.permitAll()
.anyRequest()
.authenticated()
.and()
.oauth2Login()
.userInfoEndpoint().oidcUserService(customOAuth2UserService);
}
我需要的结果:用户有两种选择:使用 Oauth2 进行身份验证,或者,如果他愿意,使用 httpBasic LDAP,无论哪种方式。
我认为有一种方法可以配置 Spring Security,以便 OAuth2 和 httpBasic LDAP 一起工作,但我不知道该怎么做。
【问题讨论】:
标签: java spring spring-security spring-security-oauth2 spring-security-ldap