Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
针对不同api端点的Spring多种身份验证方法_Spring_Spring Boot_Spring Security_Jwt_X509 - Fatal编程技术网

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

针对不同api端点的Spring多种身份验证方法,spring,spring-boot,spring-security,jwt,x509,Spring,Spring Boot,Spring Security,Jwt,X509,我想检查不同端点的不同身份验证方法。我想使用的方法是x509和jwt。我只需要对某些端点使用x509,对所有其他请求使用JWT 以下是我的web安全配置: @Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) public class SecurityConfig extends WebSecurityConfigurerAdapter { @Configuration

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

以下是我的web安全配置:

@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证书,并允许所有其他端点响应。我需要其他端点返回503而不带jwt令牌。

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

比如说

@覆盖
受保护的无效配置(HttpSecurity http)引发异常{
http
.授权请求()
.anyRequest().fullyAuthenticated()
就是说:

@覆盖
受保护的无效配置(HttpSecurity http)引发异常{
http
.antMatcher(“/**”)
.授权请求()
.anyRequest().fullyAuthenticated()
我们在这里说的是

  • http
    -安全过滤器链
  • http.antMatcher
    -安全过滤器链的入口点
  • http.authorizeRequests
    -开始我的端点访问限制
  • http.authorizeRequests.antMatchers
    -具有特定访问权限的URL列表
  • 因此,您需要做的是更改
    @Order(1)
    过滤器链以缩小模式范围。例如:
    http.antMatcher(“/api/transaction/**”)

    您的配置现在看起来像

    
    @配置
    @订单(1)
    公共静态类ApiWebSecurityConfig扩展了WebSecurityConfigureAdapter{
    @凌驾
    受保护的无效配置(HttpSecurity http)引发异常{
    http
    .antMatcher(“/api/transaction/**”)//自定义入口点
    .授权请求()
    .antMatchers(“/api/transaction/testf”).authenticated()和()
    .x509()
    .subjectPrincipalRegex(“CN=(.*?(:,|$)”)
    .userDetailsService(新的X509UserDetailsService())
    ;
    }
    }
    @配置
    @订单(2)
    公共静态类APITokeSecurityConfig扩展了WebSecurity配置适配器{
    @凌驾
    受保护的无效配置(HttpSecurity http)引发异常{
    http
    .antMatcher(“/**”)//这是默认值
    .授权请求()
    .antMatchers(“/oauth/token”,“/api/dealer/login”).permitAll()
    .及()
    .授权请求()
    .anyRequest()
    .authenticated()
    ;
    }
    
    在现有配置中,名为
    ApiWebSecurityConfig
    的筛选器链将捕获所有调用。另一个筛选器链
    ApiTokenSecurityConfig
    从未使用过

    您可以在本文档中看到另一个描述


    对于每个筛选器链,必须指定该链的ant模式。
    http.antMatcher
    (不是http.authorizeRequests作为第一个调用)本文介绍了您需要的: