【问题标题】:Spring multiple authentication methods for different api endpoints不同api端点的Spring多种身份验证方法
【发布时间】:2019-07-09 10:01:32
【问题描述】:

我想检查不同端点的不同身份验证方法。我想使用的方法是 x509 和 jwt。我需要 only 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 证书,并允许所有其他端点响应。我需要其他端点在没有 jwt 令牌的情况下返回 503。

【问题讨论】:

  • 对于每个过滤器链,您必须为该链指定蚂蚁模式。 http.antMatcher(不是 http.authorizeRequests 作为第一次调用)这篇文章描述了你需要什么:stackoverflow.com/questions/54657163/…

标签: spring spring-boot spring-security jwt x509


【解决方案1】:

您有两个过滤器链。他们都没有正确配置的入口点模式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

【讨论】:

    猜你喜欢
    • 2011-01-22
    • 1970-01-01
    • 2018-01-01
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    相关资源
    最近更新 更多