简体   繁体   English

CSRF和Spring Security

[英]CSRF and Spring Security

I have been trying to get a grip on spring security and always get confused with the initial configuration. 我一直在努力掌握弹簧的安全性,并总是对初始配置感到困惑。 Where in few tutorial I find CSRF disabled and in few I found it enabled. 在几本教程中,我发现CSRF已禁用,而在几本中,我发现它已启用。

At some forum it's written as it's good to disable it and in some tutorials few people mention it's not a good practice to disable csrf. 在某些论坛上写它是因为禁用它是一件好事,而在一些教程中,很少有人提到禁用csrf不是一个好习惯。

My point is why do we need CSRF? 我的观点是为什么我们需要CSRF? what's the reason behind using CSRF? 使用CSRF的原因是什么? what if we disable it and why if we shouldn't disable it? 如果禁用它会怎样?为什么不禁用它呢?

http.csrf()
.csrfTokenRepository(csrfTokenRepository()).and()
.addFilterAfter(csrfHeaderFilter(), CsrfFilter.class)

and

http.csrf().disable()
.exceptionHandling().and()
.anonymous().and()
.servletApi().and()
.headers().cacheControl().and()
.authorizeRequests()

What's the best configuration if I am using Spring Security with REST? 如果我将Spring Security与REST一起使用,什么是最佳配置? Because in second configuration it's showing me a popup window to login. 因为在第二种配置下,它向我显示了一个用于登录的弹出窗口。 And in first configuration it's giving me 在第一种配置中,它给了我

(Expected CSRF token not found. Has your session expired?) (未找到预期的CSRF令牌。您的会话是否已过期?)

If CSFR is enabled or not depends on The Spring Security version and type of configuration used. 是否启用CSFR取决于Spring Security版本和使用的配置类型。

Before Spring Security 4, when using XML configuration CSFR would be disabled and when using Java based configuration it would be enabled. 在Spring Security 4之前,使用XML配置时将禁用CSFR,而使用基于Java的配置时将启用CSFR。 As of Spring Security 4 CSFR is enabled for both XML and Java based configuration by default. 从Spring Security 4开始,默认情况下针对基于XML和Java的配置都启用CSFR。

Do you need CSFR, well if you have a public facing site or API I would say yes. 您是否需要CSFR,如果您有面向公众的网站或API,我会说是的。 Every security layer you disable makes your application more vulnerable. 禁用的每个安全层都会使您的应用程序更容易受到攻击。

What CSFR is is explained on this page 本页说明 CSFR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM