在 Tomcat 中设置 CsrfPreventionFilter
Setting up CsrfPreventionFilter in Tomcat
我正在尝试在 Tomcat 8.
中设置内置的 CsrfPreventionFilter
我想我已经阅读了 Whosebug 中关于这个主题的所有问题和答案,但我仍然无法解决我的问题。
我有 home.jsp
和 reports.jsp
home.jsp
的URL(一旦受到response.encodeUrl()
)是
www.example.com/home.jsp?org.apache.catalina.filters.CSRF_NONCE=5E4BD8FEE1B4CC1DA79D874905015911
这是正确呈现的,不是因为 nonce 有效,而是因为我将 home.jsp
设置为入口点。然而,它似乎确实证明 CSRFPreventionFilter 已启动并且 运行 并成功生成随机数。
如果我按 CTRL+N 然后输入 www.example.com/reports.jsp
然后我添加相同的随机数,即 www.example.com/reports.jsp?org.apache.catalina.filters.CSRF_NONCE=5E4BD8FEE1B4CC1DA79D874905015911
然后我得到页面 403。reports.jsp
没有设置为一个 EntryPoint,但我希望它能够成功呈现,因为它已经提供了随机数。
我做错了什么?
(我的问题有点类似于Getting 403 error when using CSRF filter with tomcat 6.0.32; but I feel that the answer is probably not in the way the CSRFPrevention Filter is set up, but perhaps some issue in the way I am dealing with the response/request internally, akin to @Pankaj Kumar's answer in )
更新
这里是web.xml配置
<filter>
<filter-name>CSRFPreventionFilter</filter-name>
<filter-class>org.apache.catalina.filters.CsrfPreventionFilter</filter-class>
<init-param>
<param-name>entryPoints</param-name>
<param-value>/images/404.JPG,/login.jsp,/home.jsp,/images/im_login.gif,/js/events.js,/js/menu.js,/js/amount_validation.js,/js/calendar.js,/js/toolbar.js,/js/tablesort.js,/js/scripts.js,/css/style.css</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CSRFPreventionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
您需要从 entryPoints
中删除 js、css 和图像路径,并以不包含它们的方式为 CsrfPreventionFilter
设置滤镜映射。
原因是在 tomcat 中,nonceCache 默认大小为 5。在您请求 js css 的情况下,tomcat 生成一个新的随机数(因为它们在入口点中)并将其添加到缓存中,在 5 个这样的请求之后您的初始随机数(为 [=17= 生成) ]) 被冲出 nonceCache 并且不再有效。因此,当您使用该随机数发送合法请求时,您会得到一个 403
。
我正在尝试在 Tomcat 8.
中设置内置的 CsrfPreventionFilter我想我已经阅读了 Whosebug 中关于这个主题的所有问题和答案,但我仍然无法解决我的问题。
我有 home.jsp
和 reports.jsp
home.jsp
的URL(一旦受到response.encodeUrl()
)是
www.example.com/home.jsp?org.apache.catalina.filters.CSRF_NONCE=5E4BD8FEE1B4CC1DA79D874905015911
这是正确呈现的,不是因为 nonce 有效,而是因为我将 home.jsp
设置为入口点。然而,它似乎确实证明 CSRFPreventionFilter 已启动并且 运行 并成功生成随机数。
如果我按 CTRL+N 然后输入 www.example.com/reports.jsp
然后我添加相同的随机数,即 www.example.com/reports.jsp?org.apache.catalina.filters.CSRF_NONCE=5E4BD8FEE1B4CC1DA79D874905015911
然后我得到页面 403。reports.jsp
没有设置为一个 EntryPoint,但我希望它能够成功呈现,因为它已经提供了随机数。
我做错了什么?
(我的问题有点类似于Getting 403 error when using CSRF filter with tomcat 6.0.32; but I feel that the answer is probably not in the way the CSRFPrevention Filter is set up, but perhaps some issue in the way I am dealing with the response/request internally, akin to @Pankaj Kumar's answer in
更新
这里是web.xml配置
<filter>
<filter-name>CSRFPreventionFilter</filter-name>
<filter-class>org.apache.catalina.filters.CsrfPreventionFilter</filter-class>
<init-param>
<param-name>entryPoints</param-name>
<param-value>/images/404.JPG,/login.jsp,/home.jsp,/images/im_login.gif,/js/events.js,/js/menu.js,/js/amount_validation.js,/js/calendar.js,/js/toolbar.js,/js/tablesort.js,/js/scripts.js,/css/style.css</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CSRFPreventionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
您需要从 entryPoints
中删除 js、css 和图像路径,并以不包含它们的方式为 CsrfPreventionFilter
设置滤镜映射。
原因是在 tomcat 中,nonceCache 默认大小为 5。在您请求 js css 的情况下,tomcat 生成一个新的随机数(因为它们在入口点中)并将其添加到缓存中,在 5 个这样的请求之后您的初始随机数(为 [=17= 生成) ]) 被冲出 nonceCache 并且不再有效。因此,当您使用该随机数发送合法请求时,您会得到一个 403
。