Piotr
Piotr

Reputation: 1753

Export PATCH/PUT but not POST in @RepositoryRestResource

I am using spring-data-rest and exposing endpoints for CRUD-ing my entities through repositories.
One of entities should be updatable with PATCH/PUT methods, but it shouldn't be possible to create a new instance with POST method.

It seems that both actions go through save method so it seems not possible to export only some of the requests:

@RestResource(exported = ?)
@Override
<S extends User> S save(S s);

What's the best way to accomplish that?
Should I override save method? Write custom Validator?

Upvotes: 3

Views: 923

Answers (2)

Marc Tarin
Marc Tarin

Reputation: 3169

One solution is to extend WebSecurityConfigurerAdapter (available in spring-security-config) to deny access for POST requests to the target url :

@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers(HttpMethod.POST, "/path_to_target_url").denyAll();
    }

}

Any attempt to POST to the target URL will fail with a 401 Unauthorized error.

Upvotes: 1

benkuly
benkuly

Reputation: 1194

You can use

The first two should listen to BeforeCreateEvent.

Upvotes: 3

Related Questions