Spring 不同 api 端点的多种身份验证方法

Spring multiple authentication methods for different api endpoints

我想检查不同端点的不同身份验证方法。我想使用的方法是 x509 和 jwt。我需要对某些端点仅使用 x509,对所有其他请求使用 JWT。

这是我的网络安全配置:

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


    @Configuration
    @Order(1)
    public static class ApiWebSecurityConfig extends WebSecurityConfigurerAdapter{

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                .authorizeRequests()
                .antMatchers("/api/transaction/testf").authenticated().and()
                .x509()
                .subjectPrincipalRegex("CN=(.*?)(?:,|$)")
                .userDetailsService(new X509UserDetailsService())
                ;
        }
    }

    @Configuration
    @Order(2)
    public static class ApiTokenSecurityConfig extends WebSecurityConfigurerAdapter{

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests()
                .antMatchers("/oauth/token", "/api/dealer/login").permitAll()
                .and()
                .authorizeRequests()
                .anyRequest()
                .authenticated()
                ;
        }

    }
}

此配置仅检查 /api/transaction/testf 端点的 x509 证书并允许所有其他端点响应。我需要其他端点到 return 503 没有 jwt 令牌。

您有两个过滤器链。它们都没有正确配置的入口点模式 http.antMatcher。这意味着它们被配置为使用 /** 作为它们的入口点模式。

例如

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .anyRequest().fullyAuthenticated()

等同于说:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .antMatcher("/**")
            .authorizeRequests()
                .anyRequest().fullyAuthenticated()

我们这里说的是

  1. http - 安全过滤器链
  2. http.antMatcher - 安全过滤器链的入口点
  3. http.authorizeRequests - 端点访问限制开始
  4. http.authorizeRequests.antMatchers - 具有特定访问权限的 URL 列表

所以您需要做的是更改 @Order(1) 过滤器链以缩小模式。例如:http.antMatcher("/api/transaction/**")

您的配置现在看起来像


    @Configuration
    @Order(1)
    public static class ApiWebSecurityConfig extends WebSecurityConfigurerAdapter{

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                .antMatcher("/api/transaction/**") //customized entry point
                .authorizeRequests()
                .antMatchers("/api/transaction/testf").authenticated().and()
                .x509()
                .subjectPrincipalRegex("CN=(.*?)(?:,|$)")
                .userDetailsService(new X509UserDetailsService())
                ;
        }
    }

    @Configuration
    @Order(2)
    public static class ApiTokenSecurityConfig extends WebSecurityConfigurerAdapter{

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                .antMatcher("/**") //this is default
                .authorizeRequests()
                .antMatchers("/oauth/token", "/api/dealer/login").permitAll()
                .and()
                .authorizeRequests()
                .anyRequest()
                .authenticated()
                ;
        }

使用您现有的配置,名为 ApiWebSecurityConfig 的过滤器链将捕获所有调用。另一个过滤器链 ApiTokenSecurityConfig 从未使用过。

您可以在此看到另一个描述answer

SpringSecurity: Make RESTful API basic-auth authentication possible via only a single endpoint