使用Oauth2和LDAP进行Spring安全性

时间:2016-01-10 21:57:21

标签: spring oauth-2.0 spring-boot spring-security-oauth2

现在我的Web应用程序使用spring boot和Spring安全性没有问题,但是我需要使用Oauth2导出一个rest服务身份验证。

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

当其他系统尝试使用我们的Rest服务时,我想将Oauth2与相同的Active Directory一起使用。

我该怎么做?我的配置与表单登录和活动目录工作正常,但我们不知道如何使用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

1 个答案:

答案 0 :(得分:1)

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

请参阅我对类似问题here的回答