簡體   English   中英

具有基本身份驗證和OAuth訂單問題的Spring Boot安全性

[英]Spring Boot Security with Basic Auth and OAuth Order Issue

我正在嘗試實現一個簡單的彈簧啟動項目。 我得到了幾個REST-Endpoints,我必須以不同的方式保護它們。 一個必須由Basic Auth保護,另一個使用OAuth保護,另一個使用自定義安全實現。

REST的端點:

  • /基本/ AUTH
  • / application / secure(oauth)
  • / application / secure2(自己實現)

從教程中,我知道我要設置安全適配器的順序。 我的第一個目的是以十步為單位設置順序(例如@Order(10) ,@ @Order(20) ),以防我需要在其間添加其他安全過濾器。 通過這樣做,我調查了以下行為:

  • 如果我使用@Order(10)添加基本​​身份驗證過濾器和使用@Order(20) OAuth過濾器,則只有OAuth過濾器有效。
  • 如果我使用@Order(1)@Order(2)添加基本​​auth過濾器和使用@Order(4) OAuth過濾器,則兩個過濾器都可以正常工作。
  • 如果我向@Order(3)添加一個過濾器,我收到一個錯誤,說明訂單3已經在使用中,並且無法配置兩次。

因此,有一個默認的spring安全適配器(或其他)具有默認順序3.我想我通過添加@EnableWebSecurity禁用每個默認的spring安全行為。 在我沒有通過谷歌找到答案后,我的問題是:

  • 我做的是正確的嗎?
  • 什么是彈簧訂單3的這個安全適配器?
  • 默認安全適配器是否阻止我的基本身份驗證實現?

WebSecurityConfig:

   @Configuration
   @EnableWebSecurity
   public class WebSecurityConfig {

    @Order(10)
    @Configuration
    public class BasicAuthConfig extends WebSecurityConfigurerAdapter {
        @Value("${security.user.password}")
        private String password;
        @Value("${security.user.name}")
        private String username;

        private static final String ROLE_ADMIN = "ADMIN";

        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication().withUser(username).password(password).roles(ROLE_ADMIN);
        }

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable();
            http.requestMatchers().antMatchers("/basic/**", "/") //
                    .and().authorizeRequests().anyRequest().authenticated() //
                    .and().httpBasic();
        }
    }

    @Order(20)
    @Configuration
    @EnableResourceServer
    @EnableGlobalMethodSecurity(prePostEnabled = true)
    protected static class Oauth2ServerConfig extends ResourceServerConfigurerAdapter {
        @Override
        public void configure(HttpSecurity http) throws Exception {
            System.out.println("Filter called");
            // @formatter:off
            http.csrf().disable();
            http.authorizeRequests().antMatchers("/application/**").authenticated()
                    // .antMatchers(GET, "/application/secure").authenticated()
                    .anyRequest().authenticated(); 
            // @formatter:on
        }

     // offline token validator

    }

這是一個古老的問題,但如果有人仍然想知道問題是什么,這是我的觀察:

  • @EnableResourceServer導入ResourceServerConfiguration ,其順序為3。
  • 例如,有些方法可以允許您在order 3資源服務器配置器之前添加2個以上的過濾器
    • 通過給它們中的一些負序值(雖然我不認為負值是任何特殊的,但是需要考慮其他隱式Web安全配置器 - 例如,默認情況下啟用0階的配置器 。但是,隨着新功能的引入,框架的不同版本中的過濾器之間可能會發生沖突);
    • 通過將它們添加為資源配置器( ResourceServerConfiguration類不添加任何請求匹配器,但強制執行回anyRequest().authenticated()如果用戶尚未配置任何東西)。
  • 為了更好地理解配置的請求匹配器中路徑的匹配方式,您可以快速瀏覽一下Ant路徑模式

暫無
暫無

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

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