Spring Security OAuth2和LDAP身份验证具有同一资源



我有Spring Boot 2 Rest应用程序,我想配置Spring Security以支持Google登录或LDAP身份验证(例如/雇员(

我已经通过HTTPBASIC(连接到Apache AD LDAP服务器(进行了身份验证。

另外,我已经通过Google oauth2登录设置了身份验证。这两种配置都可以分开正确(我可以通过Google登录进行身份验证,但不能同时使用LDAP,因为我必须重新启动Spring Security(,现在我需要能够通过这两种方式进行身份验证同时。

我针对LDAP auth

的弹簧安全配置
@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登录

的弹簧安全性时,它的外观
@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,无论哪种方式。

我认为有一种配置弹簧安全性的方法,因此OAuth2和httpbasic ldap一起工作,但我不知道Ho去做。

这是可能的。

基本身份验证使用基本,其中OAuth使用Bearer作为标头授权标题的一部分。

我们可以使用自定义请求匹配器检测基本身份验证并使用LDAP进行身份验证。如果没有,它将流过Oauth。

首先,订购比OAuth Authentication Server高的WebsecurityConfigurerAdapter

@Configuration
@Order(2)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

使用我们的自定义请求映射器

http
            .csrf()
            .disable()
            .requestMatcher(new BasicRequestMatcher())
            .authorizeRequests()
            .antMatchers("/", "/login**","/callback/", "/webjars/**", "/error**")
            .permitAll()
            .anyRequest()
            .authenticated()
            .and()
            .httpBasic()
            .and()
            .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS);

自定义请求匹配器,

 private static class BasicRequestMatcher implements RequestMatcher {
    @Override
    public boolean matches(HttpServletRequest request) {
        String auth = request.getHeader("Authorization");
        return (auth != null && auth.startsWith("Basic"));
    }

相关内容

最新更新