question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Disable CSRF using property `security.enable-csrf`

See original GitHub issue

Summary

I want to disable the CSRF security by setting to false the property security.enable-csrf in a active applicaton .properties file.

Actual Behavior

Based on the official documentation by default this security is enabled. To disable it you must specify it in org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter#configure(org.springframework.security.config.annotation.web.builders.HttpSecurity) with

http.csrf().disable();

or its equivalent in xml configuration.

Expected Behavior

The variable security.enable-csrf is acknowledged as one of the common properties by Spring Boot, yet setting it to false doesn’t solve anything.

Configuration

In my application.yml I have this section

---
spring:
  profiles: dev

security.enable-csrf: false

But setting the profile to dev won’t disable the CRSF Security

Version

I am using spring-security-config-4.2.2.RELEASE

Possible solution

I solved this issue easily by specifying in my implementation of WebSecurityConfigurerAdapter the following code:

@Order(FRONTEND_SECURITY_ORDER)
@EnableAspectJAutoProxy
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    public static final int FRONTEND_SECURITY_ORDER
            = SecurityProperties.ACCESS_OVERRIDE_ORDER + 3;

    @Value("${security.enable-csrf}")
    private boolean csrfEnabled;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        if (!csrfEnabled) {
            http.csrf().disable();
        }

        http
                .httpBasic()
                //..... etc
    }
}

This will disable the csrf security if the property security.enable-csrf is set to false. An equivalent approach could be solved.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
lanoxxcommented, Feb 25, 2018

Just stumbled on this issue. I am confused about whether CSRF is disabled in 1.5.9 by default or not. The documentation for 1.5.9 explicitly mentions that it is enabled by default:

Common low-level features (HSTS, XSS, CSRF, caching) provided by Spring Security are on by default.

https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/htmlsingle/

However further down we find:

security.enable-csrf=false # Enable Cross Site Request Forgery support.

Which seems to imply that the csrf setting is set to false by default.

According to @mbhave’s comment it is disabled which also matches my experience? Please clarify in this issue and possibly update the documentation for 1.5.9?

Thank you.

0reactions
mbhavecommented, Feb 27, 2018

It is a breaking change but it’s done to tighten security. It makes sense for Boot to stick to Spring Security’s defaults in this case and it’s pretty easy to flip it back if people want that with a flag.

because of failing tests when I switched from Spring Boot’s autoconfiguration to a manually configured Spring Security Configuration. The Spring Boot had CSRF default to false, but Spring security was enabling it by default

That shouldn’t be the case with 1.5.11.BUILD-SNAPSHOTS because Spring Boot and Spring Security’s defaults for CSRF are the same there.

For the documentation part of it, I’ve added a new issue here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to disable csrf in Spring using application.properties?
4 and I only added the configure() method to disable the csrf. If I remove that method completely, the property is still not...
Read more >
A Guide to CSRF Protection in Spring Security - Baeldung
In this tutorial, we will discuss Cross-Site Request Forgery (CSRF) attacks and how to prevent them using Spring Security. Further reading: CSRF ......
Read more >
How to disable CRSF in Spring Using an application property
You may have noticed that the Spring boot property security.enable-csrf would take care of enabling and disabling this feature.
Read more >
How to enable and disable CSRF in Spring Boot Security
1. Disable using security configuration code ... The spring boot security application allows to configure the security details in a customized class that...
Read more >
19. Cross Site Request Forgery (CSRF) - Spring
As of Spring Security 4.0, CSRF protection is enabled by default with XML configuration. If you would like to disable CSRF protection, the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found