Over a million developers have joined DZone.

Securing Spring Data REST with PreAuthorize

DZone's Guide to

Securing Spring Data REST with PreAuthorize

In this post we take a look at how to secure a Spring Data REST using Spring Security and Spring Boot. Come find out how straight forward this is!

· Security Zone ·
Free Resource

Discover how to provide active runtime protection for your web applications from known and unknown vulnerabilities including Remote Code Execution Attacks.

Securing Spring Data REST with PreAuthorize is an alternative method to securing Spring Data REST APIs, building on the previous apporach covered in Spring Security and Spring Data REST.

Securing Spring Data REST with PreAuthorize code is available on GitHub:



Run the code by typing the following: 

mvnw spring-boot:run



The key points are:

  • Maintain security for roles “ADMIN” and “USER” on “/rest/**” requests.
  • @EnableGlobalMethodSecurity(prePostEnabled = true)  – this enables the annotations on the JPA model.

package com.javabullets.springdata.jparest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;

import org.springframework.http.HttpMethod;

@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
	public void configureGlobal(AuthenticationManagerBuilder auth)
			throws Exception {

    protected void configure(HttpSecurity http) throws Exception {


The key changes are the use of PreAuthorize on the ParkrunCourseRepository:

package com.javabullets.springdata.jparest;

import org.springframework.data.repository.CrudRepository;
import org.springframework.security.access.prepost.PreAuthorize;

public interface ParkrunCourseRepository extends CrudRepository<ParkrunCourse, Long> {
	ParkrunCourse save(ParkrunCourse parkrunCourse);

The key points are:

  • Use of hasRole – note you need to append ROLE_ to your roles.
  • We could also add PreAuthorize security to custom methods defined in the repository.

Putting it Together

mvnw spring-boot:run

POST Methods – Access Forbidden

curl -u user:user -X POST -H "Content-Type:application/json" -d "{  \"courseName\" : \"adminOnly\",  \"url\" : \"url\",  \"averageTime\" : \"10000\" }" http://localhost:8080/rest/parkrunCourses

{"timestamp":1496134011261,"status":403,"error":"Forbidden","message":"Access is denied","path":"/rest/parkrunCourses"}

POST Methods – Access Allowed

curl -u admin:admin -X POST -H "Content-Type:application/json" -d "{  \"courseName\" : \"adminOnly\",  \"url\" : \"url\",  \"averageTime\" : \"10000\" }" http://localhost:8080/rest/parkrunCourses

  "courseName" : "adminOnly",
  "url" : "url",
  "averageTime" : 10000,
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/rest/parkrunCourses/13"
    "parkrunCourse" : {
      "href" : "http://localhost:8080/rest/parkrunCourses/13"


This post shows a different approach to role based security of Spring Data REST with PreAuthorize. It is not a question of which method is better, but which is practical. It may be that you cannot use preAuthorize in your codebase, but can use role based authentication as outlined in Spring Security and Spring Data REST.

From a design point of view, you may want to define a custom repository with your core PreAuthorize rules, allowing your model to inherit security.

This post shows how Spring Security and Spring Data REST can be combined to secure REST API URL’s and HTTP methods. It used a basic form of Spring authentication, combining a MemoryRealm with the security configuration. We have also demonstrated how to restrict access to REST methods based on user group.

My next post will look at how Spring Data REST can restrict access by deciding what methods it exposes, and what fields are exposed

Find out how Waratek’s award-winning application security platform can improve the security of your new and legacy applications and platforms with no false positives, code changes or slowing your application.

api ,authorization ,security ,spring data rest

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}