在相同的配置中使用RequestHeaderRequestMatcher和antmactcher基本身份验证Spring Security

问题描述 投票:0回答:1

我有这个过滤器:

 @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.requestMatchers(new RequestHeaderRequestMatcher("Caller", "Rem"));
        // add here a seconde filter condition for basic Auth
    }

仅在头过滤器之后,我想使用相同的配置在下面的内存中创建另一个过滤器:

 @Autowired
    public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
            .passwordEncoder(NoOpPasswordEncoder.getInstance())
            .withUser("myAccount").password("MyPassword").roles("USER");
    }

非常感谢。

java spring-boot spring-security basic-authentication request-headers
1个回答
0
投票
http.httpBasic().and()
                .authorizeRequests()
                .requestMatchers(new AndRequestMatcher(new RequestHeaderRequestMatcher("Caller", "Rem"), new AntPathRequestMatcher("/hello/one")))
                .hasRole("USER")
                .and()
                .authorizeRequests().anyRequest().authenticated()
        .anyRequest().denyAll();

在您的方案中,将“ / hello / one”替换为“ / **”。

说明:

  1. 如果请求中的路径与“ / hello / one”不匹配,则服务器将返回403。
  2. 如果路径匹配,它将检查身份验证,如果返回失败的401,否则将转到下一步。
  3. 如果路径匹配并且身份验证成功,但是标头中不包含值“ Rem” 403的“ Caller”,则将返回。
  4. 如果路径匹配,则验证成功,并且标头具有必需的键值,端点将被执行。
© www.soinside.com 2019 - 2024. All rights reserved.