【问题标题】:Spring-security with Oauth2 and LDAP使用 Oauth2 和 LDAP 的 Spring 安全性
【发布时间】:2016-04-15 03:43:08
【问题描述】:

现在我的 Web 应用程序可以毫无问题地使用 Spring Boot 和 Spring Security,但我需要导出一个使用 Oauth2 进行身份验证的 rest 服务。

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

当其他系统尝试使用我们的 Rest Service 时,我想将 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 提供

【问题讨论】:

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


    【解决方案1】:

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

    查看我对类似问题的回答here

    【讨论】:

      猜你喜欢
      • 2011-09-22
      • 2012-06-15
      • 2017-07-02
      • 2020-04-14
      • 2017-02-09
      • 2018-11-29
      • 2019-04-03
      • 2012-04-11
      • 2015-05-22
      相关资源
      最近更新 更多