Spring-Oauth2 和 LDAP 的安全性

Spring-security with Oauth2 and LDAP

如今,我的 Web 应用程序可以在 spring 启动和 Spring 安全性下正常工作,但我需要使用 Oauth2 导出休息服务身份验证。

当用户访问我的网络系统时,他通过具有 spring 安全性和 Active Directory 的表单登录进行身份验证。

当其他系统尝试使用我们的 Rest 服务时,我想在同一个 Active Directory 中使用 Oauth2。

我该怎么做?我的表单登录和活动目录配置工作正常,但我们不知道如何使用 Oauth2

进行身份验证

我的 WebSecurityConfig 是:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private LogoutHandler logoutHandler;

    @Autowired
    private AuthenticationSuccessHandler authenticationSuccessHandler;

    @Autowired
    private AccessDeniedHandler accessDeniedHandler;

    @Autowired
    private AuthenticationFailureHandler authenticationFailureHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .authorizeRequests()
            .antMatchers("/rest/public/**").permitAll()
            .and().csrf().ignoringAntMatchers("/rest/public/**").and()
        .authorizeRequests()
            .antMatchers("/public/**").permitAll()
            .antMatchers("/error/**").permitAll()
            .and()
        .authorizeRequests()
            .antMatchers("/adm/**").hasAnyRole(Role.ROOT,Role.ADM)
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login")
            .successHandler(authenticationSuccessHandler)
            .failureHandler(authenticationFailureHandler)
            .defaultSuccessUrl("/home",true)
            .permitAll()
            .and()
        .logout()
            .logoutSuccessUrl("/login")
            .permitAll()
            .addLogoutHandler(logoutHandler)
            .and()
         .exceptionHandling()
            .accessDeniedHandler(accessDeniedHandler);

    }
}

如何仅为我的 Rest 服务插入 Oauth2 身份验证(此服务将通过路径 ../rest/serviceName

您需要配置另一个过滤器链来拦截您的资源服务器,以仅通过 OAuth 保护您的端点。

查看我对类似问题的回答