【问题标题】:How to require x509 Authentication for Spring OAuth Endpoints如何要求 Spring OAuth 端点的 x509 身份验证
【发布时间】:2015-01-18 11:19:40
【问题描述】:

我正在使用 Spring 4.0 java 配置。

我想在我的 oauth 端点上要求 x509 身份验证,但对于所有其他资源端点只需要一个 oauth 令牌。第一个antMatchers 似乎被覆盖了:

@Configuration
@EnableResourceServer
protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

  @Autowired
  RequestMappingUriProvider requestMappingUriProvider;

  @Autowired
  private DelegatedUserManager userManager;

  @Override
  public void configure(ResourceServerSecurityConfigurer resources) {
    // @formatter:off
    resources.resourceId(RESOURCE_ID);
    // @formatter:on
  }

  @Override
  public void configure(HttpSecurity http) throws Exception {
    // @formatter:off
    // Require x509 certificate for obtaining OAuth credentials
    http.requestMatchers().antMatchers("/oauth/**")
        .and()
        .authorizeRequests().anyRequest().hasAnyRole("USER","CLIENT")
        .and()
        .x509().subjectPrincipalRegex("CN=(.*?),").authenticationUserDetailsService(authenticationUserDetailsService())
        .and()
        //Only require a user role for interaction with all other resources
        .requestMatchers().antMatchers(requestMappingUriProvider.uriPatterns())
        .and()
        .authorizeRequests().anyRequest().hasRole("USER");
    // @formatter:on
  }

  @Bean
  public DelegatedAuthenticationUserDetailsService authenticationUserDetailsService() {
    return new DelegatedAuthenticationUserDetailsService(userManager);
  }

}

spring 的调试输出未显示我在 x509 过滤器链中检查的任何 /oauth/** 端点。

【问题讨论】:

    标签: spring spring-security spring-security-oauth2


    【解决方案1】:

    我的问题是我需要多个 HttpSecurity 元素。这篇文章帮助了我:Creating multiple HTTP sections in Spring Security Java Config

    我是这样实现的:

      @Configuration
      @EnableResourceServer
      public static class ResourceServerConfiguration {
    
        @Configuration
        @Order(1)
        public static class OAuthResourceServerConfigAdapter extends ResourceServerConfigurerAdapter {
          @Autowired
          private DelegatedUserManager userManager;
    
          @Override
          public void configure(ResourceServerSecurityConfigurer resources) {
            resources.resourceId(RESOURCE_ID);
          }
    
          @Override
          public void configure(HttpSecurity http) throws Exception {
            // @formatter:off
            // Require x509 certificate for obtaining OAuth credentials
            http.requestMatchers().antMatchers("/oauth/**")
                .and()
                .authorizeRequests().anyRequest().hasAnyRole("USER","CLIENT")
                .and()
                .x509().subjectPrincipalRegex("CN=(.*?),").authenticationUserDetailsService(authenticationUserDetailsService());
            // @formatter:on
          }
    
          @Bean
          public DelegatedAuthenticationUserDetailsService authenticationUserDetailsService() {
            return new DelegatedAuthenticationUserDetailsService(userManager);
          }
    
        }
    
        public static class MyResourceServerConfigAdapter extends ResourceServerConfigurerAdapter {
          @Autowired
          RequestMappingUriProvider requestMappingUriProvider;
    
          @Override
          public void configure(ResourceServerSecurityConfigurer resources) {
            resources.resourceId(RESOURCE_ID);
          }
    
          @Override
          public void configure(HttpSecurity http) throws Exception {
            // @formatter:off
            //Only require a user role for interaction with all other resources
            http.requestMatchers().antMatchers(requestMappingUriProvider.uriPatterns())
                .and()
                .authorizeRequests().anyRequest().hasRole("USER");
            // @formatter:on
          }
    
        }
      }
    

    【讨论】:

    • 大体思路是正确的(两个资源集需要不同的过滤器链),但ResourceServerConfigurerAdapter 用于配置资源服务器,/oauth/** 端点是授权服务器的一部分。以与 /authorize 端点相同的方式保护 /token 端点实际上是非常不寻常的,但如果你有你需要的东西,我想这就是它的方式。
    • 感谢您的评论。出于我的目的,我想保护授权端点以及资源端点。因此,用户必须使用他们的凭据访问授权端点,而客户端必须使用他们的 PKI 凭据访问令牌端点。
    • 我不确定我是否理解这一点。您希望资源受到访问令牌和 x509(作为替代身份验证机制)的保护吗?在任何情况下,“/oauth/**”都包含授权令牌端点,并且以相同的方式保护它们是非常不寻常的(闻所未闻的)。
    • 我的场景是这样工作的: 1. 用户去 /oauth/authorize 获取授权码,而不是输入用户名和密码,他的 PKI 凭据对他进行身份验证。 2. 用户被重定向到接收授权码的客户端,并通过 2-way SSL 向 /oauth/token 发送请求,因此客户端在接收访问令牌之前使用其 PKI 凭据(而不是基本身份验证)进行身份验证。 3. 客户端现在可以使用访问令牌直到它过期。我意识到这是一个不寻常的场景。
    • 听起来不错。想知道这是否可能我自己。您希望能够使用 x509 进行一次身份验证并在其他任何地方使用令牌而不是在每台服务器上配置证书,这似乎并不奇怪。感谢您的帖子/研究。
    猜你喜欢
    • 2018-11-18
    • 1970-01-01
    • 2017-12-11
    • 2022-01-12
    • 2017-05-23
    • 1970-01-01
    • 2019-01-16
    • 2015-02-02
    • 2011-04-24
    相关资源
    最近更新 更多