【问题标题】:Spring Security - Specify which X509 can access which specific endpointSpring Security - 指定哪个 X509 可以访问哪个特定端点
【发布时间】:2021-07-12 13:24:13
【问题描述】:

关于如何使用 Spring Security 指定哪个客户端证书可以访问哪个特定的预定义端点的小问题。

预定义端点是指 Web 应用程序具有默认端点(不是我通过 @RestController 定义的那些),例如执行器端点 /actuator/health/actuator/prometheus,或 Spring Cloud Config 端点,例如 /config/myservice/不可能@PreAuthorize

我只想指定哪个客户端证书可以访问哪个端点,比如:

  • 带有UID=Alice 的客户端证书可以访问/actuator/health/config/myservice
  • 带有UID=Bob的客户端证书可以访问/actuator/prometheus

网上有很多例子,How to extract X509 certificate:

但是如何在应用中进行配置,即这种证书可以访问什么的映射呢?

谢谢

【问题讨论】:

  • 您是否有一个用户存储库,您可以从该存储库中在UserDetailsService 中查找经过身份验证的用户的详细信息?如果是,则应该只是将用户名映射到数据库或文件系统中的某个位置,然后在您的 UserDetailsService.loadUserByUsername 方法中为用户分配正确的角色。至于如何保护特定 url 成为特定角色,您可以使用 antMatcher 以及 WebSecurityConfigurerAdapter 中的 hasRole/hasPermission 方法。
  • 您好 Setu,感谢您的评论。不幸的是,没有用户商店。只有在可能的情况下,我才想避免任何形式的数据库交互,并保持解决方案简单。
  • 即使没有用户存储,原理也一样。在您的UserDetailsService.loadUserByUsername 中有一些 if 条件检查用户名并为 Alice 和 Bob 分配适当的角色,即如果用户名是 Alice,则添加角色 HEALTH 和 MYSERVICE,否则如果用户名是 Bob,则添加角色 PROMETHEUS。然后在WebSecurityConfigurerAdapterconfigure(HttpSecurity http) 方法中定义antMatchershasRole 条件。 Spring 不会直接提供将特定证书与特定 url 访问关联的方法,但使用角色可以实现这一点。
  • 也许这个问题会有所帮助。 stackoverflow.com/questions/1102721/…
  • 当然,谢谢。我还希望除了如何解析 X509(Spring Security 可以做得很好),如何定义,哪个证书可以访问什么路由,如果可能的话,使用代码 sn-p

标签: java spring-boot spring-security


【解决方案1】:

@Setu 为您提供了解决问题的基本信息。

请考虑以下代码改编自您引用的one of the articles 中的代码:

@SpringBootApplication
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class X509AuthenticationServer extends WebSecurityConfigurerAdapter {
  ...

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
      .authorizeRequests()
        // Define the mapping between the different endpoints
        // and the corresponding user roles
        .antMatchers("/actuator/health").hasRole("ACTUATOR_HEALTH")
        .antMatchers("/actuator/prometheus").hasRole("ACTUATOR_PROMETEUS")
        .antMatchers("/config/myservice").hasRole("CONFIG_MYSERVICE")
        // Please, adjust the fallback as appropriate
        .anyRequest().authenticated()
      .and()
        // Configure X509Configurer (https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/config/annotation/web/configurers/X509Configurer.html)
        .x509()
          .subjectPrincipalRegex("CN=(.*?)(?:,|$)")
          .userDetailsService(userDetailsService())
    ;
  }

  @Bean
  public UserDetailsService userDetailsService() {
    // Ideally this information will be obtained from a database or some
    // configuration information
    return new UserDetailsService() {

      @Override
      public UserDetails loadUserByUsername(String username) {

        Objects.requireNonNull(username);

        List<GrantedAuthority> authorities = null;
        switch (username) {
          // Match the different X509 certificate CNs. Maybe you can use the
          // X509 certificate subject distinguished name to include the role in
          // some way and obtain it directly with the subjectPrincipalRegex
          case "Alice":
            authorities = AuthorityUtils
              .commaSeparatedStringToAuthorityList("ROLE_ACTUATOR_HEALTH, ROLE_CONFIG_MYSERVICE");
            break;

          case "Bob":
            authorities = AuthorityUtils
              .commaSeparatedStringToAuthorityList("ROLE_ACTUATOR_PROMETHEUS");
            break;

          default:
            throw new UsernameNotFoundException(String.format("User '%s' not found!", username));
        }

        return new User(username, "", authorities);
      }
    };
  }
}

请根据实际端点和用户的需要调整代码。

【讨论】:

  • 不客气 PatPatPat,我很高兴听到这个答案很有帮助。
猜你喜欢
  • 2017-05-26
  • 2011-01-07
  • 1970-01-01
  • 2015-06-08
  • 1970-01-01
  • 2010-10-02
  • 1970-01-01
  • 1970-01-01
  • 2019-04-25
相关资源
最近更新 更多