日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring MVC,Thymeleaf,Spring Security应用程序中的CSRF保护

發布時間:2023/12/3 javascript 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring MVC,Thymeleaf,Spring Security应用程序中的CSRF保护 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

跨站點請求偽造(CSRF)是一種攻擊,它迫使最終用戶在當前已通過身份驗證的Web應用程序上執行不需要的操作。 如果您使用Spring Security 3.2及更高版本,在Spring MVC / Thymeleaf應用程序中防止CSRF攻擊相當容易。

怎么測試?

為了進行測試,我創建了一個區域受限的應用程序,可以在其中發送表單。 表單的源代碼:

<form class="form-narrow form-horizontal" method="post" th:action="@{/message}" th:object="${messageForm}" action="http://localhost:8080/message"><fieldset><legend>Send a classified message</legend><div class="form-group" th:classappend="${#fields.hasErrors('payload')}? 'has-error'"><label for="payload" class="col-lg-2 control-label">Payload</label><div class="col-lg-10"><input type="text" class="form-control" id="payload" placeholder="Payload" th:field="*{payload}" name="payload"/><span class="help-block" th:if="${#fields.hasErrors('payload')}" th:errors="*{payload}">May not be empty</span></div></div><div class="form-group"><div class="col-lg-offset-2 col-lg-10"><button type="submit" class="btn btn-default">Send</button></div></div></fieldset> </form>

知道操作URL是http:// localhost:8080 / message之后,我創建了一個單獨的頁面,其中包含一個引用該URL(帶有所有參數)的HTTP請求:

<!DOCTYPE html> <html> <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> </head> <body> <form action="http://localhost:8080/message" method="post"><input type="hidden" name="payload" value="Hacked content!"/><input type="submit" value="Hack!" /> </form> </body> </html>

我登錄了該應用程序并執行了上面的代碼。 當然,服務器允許我執行請求,因為我的應用程序容易受到CSRF攻擊。 要了解有關CSRF測試的更多信息,請訪問此鏈接: CSRF測試 。

如何保護?

如果您將XML配置與Spring Security一起使用,則必須啟用CSRF保護:

<security:http auto-config="true" disable-url-rewriting="true" use-expressions="true"><security:csrf /><security:form-login login-page="/signin" authentication-failure-url="/signin?error=1"/><security:logout logout-url="/logout" /><security:remember-me services-ref="rememberMeServices" key="remember-me-key"/><!-- Remaining configuration --></security:http>

如果是Java配置–默認情況下啟用。

從Thymeleaf 2.1版本開始,CSRF令牌將自動添加到具有隱藏輸入的表單中:

<form class="form-narrow form-horizontal" method="post" action="/message"><!-- Fields --><input type="hidden" name="_csrf" value="16e9ae08-76b9-4530-b816-06819983d048" /></form>

現在,當您嘗試重復攻擊時,將看到“ 訪問被拒絕”錯誤。

但是要記住的一件事是,啟用CSRF保護可確保注銷需要CSRF令牌。 我使用JavaScript提交了隱藏表格:

<a href="/logout" th:href="@{#}" onclick="$('#form').submit();">Logout</a><form style="visibility: hidden" id="form" method="post" action="#" th:action="@{/logout}"></form>

摘要

在這篇簡短的文章中,我展示了在使用Spring MVC(3.1 +),Thymeleaf(2.1+)和Spring Security(3.2+)時,如何輕松利用CSRF保護。 從Spring Security 4開始,使用XML配置時,默認情況下也會啟用CSRF。 請注意,使用HTTP會話來存儲CSRF令牌。 但這很容易改變。 有關更多詳細信息,請參見參考。

  • 我在Spring MVC原型中包含CSRF配置。 請檢查!

資源資源

  • Thymeleaf –與RequestDataValueProcessor集成
  • Spring安全– CSRF攻擊
  • OWASP –跨站請求偽造(CSRF)

翻譯自: https://www.javacodegeeks.com/2014/04/csrf-protection-in-spring-mvc-thymeleaf-spring-security-application.html

總結

以上是生活随笔為你收集整理的Spring MVC,Thymeleaf,Spring Security应用程序中的CSRF保护的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。