Kraken
Kraken

Reputation: 288

Spring boot csrf use

I just want to use CSRF token with some url which are used for direct access.

And I don't want to use CSRF for that url which are basically REST apis for other end.

Upvotes: 2

Views: 155

Answers (2)

Akshay Pethani
Akshay Pethani

Reputation: 2580

I find that the easiest way is to create an array of String with patterns, like this:

String [] publicUrls = new String [] {
            "/public/**",
            "/login",
            "/logout"
    };

Here is the code I use in CSRF. And the code for ignore URLs is this .ignoringAntMatchers(publicUrls):

http.csrf()
        .csrfTokenRepository(csrfTokenRepository())
        .ignoringAntMatchers(publicUrls)

Spring official doc ref

Upvotes: 2

Dev Sabby
Dev Sabby

Reputation: 1427

you can directly use this code for a particular type of request which need to be served with out CSRF.

http.csrf().ignoringAntMatchers("/rest/v1/**");

here /rest/v1/** will allow you to server all the request coming on the url starts with "rest/v1/".

Upvotes: 1

Related Questions