日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

使用 SpringSecurity 發送POST請求出現 403

作者:Jothan Zhong 更新時間: 2024-01-07 編程語言

問題場景

在使用 SpringSecurity 時對一些訪問權限進行了設置, 在用戶請求資源時出現了403錯誤 , 通過檢查代碼發現請求權限是開放的, 并且切換成 GET 請求也是可以通過, 換成POST 請求就無法通過。
在這里插入圖片描述

解決方法

在 SpringSecurity 中關閉 CSRF 因為
前端向后臺發送 post 請求時,必須驗證 csrf,否則會報錯 403 Forbidden。

  @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                // CSRF禁用,因為不使用session
                .csrf().disable()
                .antMatchers(
                        HttpMethod.GET,
                        "/",
                        "/*.html",
                        "/**/*.html",
                        "/**/*.css",
                        "/**/*.js",
                        "/profile/**"
                ).permitAll()
       
                // activiti modeler 放行
                .antMatchers("/modeler/**").anonymous()
                .antMatchers( "/activiti/definition/readResource").anonymous()
                // 除上面外的所有請求全部需要鑒權認證
                .anyRequest().authenticated()
                .and()
                .headers().frameOptions().disable();
    }
1234567891011121314151617181920212223

這個時候就有新的問題出現, "/activiti/definition/readResource"已經是允許匿名訪問了,但是在登錄狀態下訪問還是會出現403,不登陸就可以正常獲取,這個情況比較奇葩,大概理解的就是這個方法是匿名訪問的,你登陸就不是匿名了,所以你沒有權限訪問。
看了下 SpringSecurity 權限設置有

    /**
     * anyRequest          |   匹配所有請求路徑
     * access              |   SpringEl表達式結果為true時可以訪問
     * anonymous           |   匿名可以訪問
     * denyAll             |   用戶不能訪問
     * fullyAuthenticated  |   用戶完全認證可以訪問(非remember-me下自動登錄)
     * hasAnyAuthority     |   如果有參數,參數表示權限,則其中任何一個權限可以訪問
     * hasAnyRole          |   如果有參數,參數表示角色,則其中任何一個角色可以訪問
     * hasAuthority        |   如果有參數,參數表示權限,則其權限可以訪問
     * hasIpAddress        |   如果有參數,參數表示IP地址,如果用戶IP和參數匹配,則可以訪問
     * hasRole             |   如果有參數,參數表示角色,則其角色可以訪問
     * permitAll           |   用戶可以任意訪問
     * rememberMe          |   允許通過remember-me登錄的用戶訪問
     * authenticated       |   用戶登錄后可訪問
     */
123456789101112131415

將anonymous 改為permitAll 或者authenticated 后可以正常訪問。

原文鏈接:https://blog.csdn.net/qq_43985303/article/details/135377426

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新