簡體   English   中英

在同一配置 Spring Security 中使用 RequestHeaderRequestMatcher 和 antmactcher basic auth

[英]Use RequestHeaderRequestMatcher and antmactcher basic auth in the same configuration Spring Security

我有這個過濾器:

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

就在標頭過濾器之后,我想使用下面的標識符 inMemory 制作另一個具有相同配置的過濾器:

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

先謝謝了。

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”的“Caller”,則將返回 403。
  4. 如果路徑匹配,身份驗證成功並且標頭需要鍵值,端點將被執行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM