簡體   English   中英

Spring 安全 HttpSecurity 配置

[英]Spring Security HttpSecurity config

我試圖了解 RequestMatcher、AntMatcher 等是如何工作的。 我閱讀了一些帖子並了解了基礎知識。 其實我有這個簡單的基本配置:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.requestMatchers() //1
        .antMatchers("/login", "/oauth/authorize") //2
        .and() //3
        .authorizeRequests() //4
        .anyRequest() //5
        .authenticated() //6;

我真的不明白第 1,2 和 3 點。據我了解,這意味着/login/oauth/authorize的請求已映射並且應該是授權請求。 所有其他請求都需要驗證。

對於端點/user/me意味着我必須進行身份驗證,因為它由第 5 點和第 6 點統治? 對這個端點的調用對我有用。

在我的 ohter 配置中,我嘗試了一種不同的方法:

@Override
protected void configure(HttpSecurity http) throws Exception { // @formatter:off
      http
       .authorizeRequests() //1
        .antMatchers("/login", "/oauth/authorize", "/img/**").permitAll() //2
        .anyRequest() //3
        .authenticated() //4

從我的角度來看,這應該與第一個配置的邏輯相同。 但實際上端點/user/me不再可訪問。

我非常感謝您的澄清


更新1:

這是我現在的配置:

@Override
protected void configure(HttpSecurity http) throws Exception { // @formatter:off
    http
        .requestMatchers()
           .antMatchers("/", "/login", "/oauth/authorize", 
               "/main", "/logout-success", "/single-logout",
               "/password_forgotten", "/enter_new_password", "/img/**",
               "/logout", "/access_denied")
            .and().authorizeRequests()
                .antMatchers("/img/**", "/logout-success", "/password_forgotten",
                    "/enter_new_password", "/access_denied").permitAll()
            .requestMatchers(SecurityUtils::isFrameworkInternalRequest).permitAll()
            .and()
            .authorizeRequests()
            .anyRequest()
            .authenticated()
            .and()
            .formLogin()
            .loginPage("/login")
            .failureUrl("/login?error")
            .defaultSuccessUrl("/main")
            .permitAll()
            .and()
            .logout()
            .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
            .logoutSuccessUrl("/logout-success")
            .deleteCookies("JSESSIONID")
            .invalidateHttpSession(true)
            .and()
            .exceptionHandling()
            .accessDeniedPage("/access_denied")
            .authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/login"))
            .and().csrf().disable();

如果我輸入 URL \user\me作為未經身份驗證的用戶,我會收到 401 和此消息:

<oauth>
<error_description>
Vollständige Authentifikation wird benötigt um auf diese Resource zuzugreifen
</error_description>
<error>unauthorized</error>
</oauth>

哪個沒問題,但意味着此 URL 會發生任何其他 SecurityFilterChain,對嗎?

requestMatchers()配置 URL 是否將由該SecurityFilterChain處理。 因此,如果 URL 與它不匹配,整個SecurityFilterChain將被跳過,這意味着 Spring 之后安全將無法處理此 URL。 如果不配置,則默認匹配所有 URL。

authorizeRequests()為 URL 配置授權內容,例如是否需要對其進行身份驗證或只有某些角色可以訪問它等。它僅對由該 SecurityFilterChain 處理的那些 URL 有效(即那些由requestMatchers()匹配)

所以,回到你的第一個例子:

  http.requestMatchers() //1
        .antMatchers("/login", "/oauth/authorize") //2
        .and() //3
        .authorizeRequests() //4
        .anyRequest() //5
        .authenticated() //6;

這意味着這個 SecurityFilterChain 只會對/login/oauth/authorize有影響。 這兩個 URL 都需要經過身份驗證。 此 SecurityFilterChain 不會處理所有其他 URL。 所以/user/me是否需要認證與 Spring Security 無關。

http
       .authorizeRequests() //1
        .antMatchers("/login", "/oauth/authorize", "/img/**").permitAll() //2
        .anyRequest() //3
        .authenticated() //4

這意味着所有 URL 都將由此 SecurityFilterChain 處理( requestMatchers()的默認值)。 /login/oauth/authorize/img/**不需要任何授權。 其他 URL 需要進行身份驗證。

您的第一個配置
.requestMtchers() //1

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.requestMatchers() //1
        .antMatchers("/login", "/oauth/authorize") //2
        .and() //3
        .authorizeRequests() //4
        .anyRequest() //5
        .authenticated() //6;

讓我解釋一下你的.authorizeRequests() //4

http.authorizeRequests()這是一個通配符( /** )(就像過濾器允許每個請求一樣)
HttpSecurity 配置將只考慮具有這些模式的請求,您可以說

http.authorizeRequests()
//is nothing but equals to
http.antMatcher("/**").authorizeRequests();
//and also equals to
http.requestMatchers()
            .antMatchers("/**")
            .and()
            .authorizeRequests()

如果你像下面這樣配置

http.antMatcher("/api/**").authorizeRequests();

僅當傳入請求 uri 與配置的 antmatcher ( /api/** ) 匹配時才會查詢配置的 Rest ( .hasRole().hasAnyRole.authenticated().authenticated() )

考慮到您需要配置多個 URL(不同的模式),那么您不能只使用一個 antMatcher。 你需要多個然后你應該使用 requestMatcher 如下

http.requestMatchers()
            .antMatchers("/api/**", "employee/**", "/customer/**")
            .and()
            .authorizeRequests()

.antMatchers() //2
用於配置.requestMatchers()RequestMatcherConfigurer返回類型
或者也
用於配置ExpressionInterceptUrlRegistry返回類型的.authorizeRequests()

.and() //3

返回 HttpSecurity 以進行進一步的自定義

.anyRequest() //5

創建 RequestMatcher 后鏈接的 object

所有請求都與配置的請求匹配器模式匹配

.authenticated() //6
下面的配置是自我解釋

.antMatchers("/app/admin/**").hasRole("ADMIN")
//or
.antMatchers("/app/admin/**").hasAnyRole("ADMIN", "USER")
//or
.antMatchers("/app/admin/**").authenticated()
//or
.antMatchers("/app/admin/**").permitAll()

這是關於antMatchers、authorizeRequests和authenticated的粗略想法。 您可以在此鏈接中參考我的答案,了解 spring 安全性中的執行順序

暫無
暫無

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

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