DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Less Code With Spring Data Rest
  • Introduction To Spring Data JPA With Inheritance in A REST Application
  • The Magic of Spring Data
  • Build Reactive REST APIs With Spring WebFlux

Trending

  • Build a Simple REST API Using Python Flask and SQLite (With Tests)
  • How to Create a Successful API Ecosystem
  • Introducing Graph Concepts in Java With Eclipse JNoSQL
  • Enforcing Architecture With ArchUnit in Java
  1. DZone
  2. Data Engineering
  3. Data
  4. 4 Ways to Control Access to Spring Data REST

4 Ways to Control Access to Spring Data REST

This article looks at four ways to allow or restrict access to Spring Data REST using RepositoryDetectionStrategies methods.

By 
Martin Farrell user avatar
Martin Farrell
·
Jun. 29, 17 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
13.9K Views

Join the DZone community and get the full member experience.

Join For Free

This post looks at four ways to control access to Spring Data REST using RepositoryDetectionStrategies.

This post forms part of a series looking at Spring Data REST –

  • Introduction to Spring Data REST
  • Spring Security and Spring Data REST
  • Securing Spring Data REST with PreAuthorize

RepositoryDetectionStrategies

You can control access to Spring Data REST using the four RepositoryDetectionStrategies:

Image title

Source Code

  • https://github.com/farrelmr/introtospringdatarest/tree/5.0.0
  • https://github.com/farrelmr/introtospringdatarest/releases/tag/5.0.0

You can run the examples with

mvnw spring-boot:run

Examples

I've created a Spring Rest Configuration class for this example:

@Component
public class SpringRestConfiguration extends RepositoryRestConfigurerAdapter {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.setRepositoryDetectionStrategy(RepositoryDetectionStrategy.RepositoryDetectionStrategies.DEFAULT);
}
}

All of the examples will be changing the enumerated value of RepositoryDetectionStrategy.RepositoryDetectionStrategies, and testing what REST endpoints are exposed.

I have also annotated one of our Spring Data Repositories:

@RepositoryRestResource
@PreAuthorize("hasRole('ROLE_USER')")
public interface ParkrunCourseRepository extends CrudRepository<ParkrunCourse, Long> {
@Override
@PreAuthorize("hasRole('ROLE_ADMIN')")
ParkrunCourse save(ParkrunCourse parkrunCourse);
}

RepositoryDetectionStrategies.default

A default setting RepositoryDetectionStrategy detects public interfaces annotated with RepositoryRestResource or RestResource.

So, with our example, we would expect to see both interfaces exposed as REST endpoints.

We can confirm this by calling http://localhost:8080/rest/profile –

curl -u user:user -X GET http://localhost:8080/rest/profile
{
 "_links" : {
 "self" : {
 "href" : "http://localhost:8080/rest/profile"
 },
 "secrets" : {
 "href" : "http://localhost:8080/rest/profile/secrets"
 },
 "parkrunCourses" : {
 "href" : "http://localhost:8080/rest/profile/parkrunCourses"
 }
 }
}

RepositoryDetectionStrategies.annotated

Annotated will only detect interfaces annotated with RepositoryRestResource or RestResource, and an exported flag that is true(default). In our test case, we would only expect to see the end point for ParkrunCourseRepository to be exposed, as it is annotated with @RepositoryRestResource.

We can test and confirm this –

curl -u user:user -X GET http://localhost:8080/rest/profile
{
 "_links" : {
 "self" : {
 "href" : "http://localhost:8080/rest/profile"
 },
 "parkrunCourses" : {
 "href" : "http://localhost:8080/rest/profile/parkrunCourses"
 }
 }
}

RepositoryDetectionStrategies.visibility

VISIBILITY will only detect REST and expose repertories based if they are publicly exposed.

Let's make a change to SecretRepository to leave the interface with default visibility –

interface SecretRepository extends CrudRepository<Secret, Long> {
}

We would not expect to see this end point, and would only expect to see the ParkrunCourseRepository end point. This is confirmed when we call the rest/profile –

curl -u user:user -X GET http://localhost:8080/rest/profile
{
 "_links" : {
 "self" : {
 "href" : "http://localhost:8080/rest/profile"
 },
 "parkrunCourses" : {
 "href" : "http://localhost:8080/rest/profile/parkrunCourses"
 }
 }
}

RepositoryDetectionStrategies.all

Finally, if we use RepositoriesDetectionStrategies.ALL with SecretRepository at default access, then we would expect to see both the parkrunCourses and Secret’s end point. All will also expose endpoints that have an exported false attribute.

Restart the server and test –

curl -u user:user -X GET http://localhost:8080/rest/profile
{
 "_links" : {
 "self" : {
 "href" : "http://localhost:8080/rest/profile"
 },
 "parkrunCourses" : {
 "href" : "http://localhost:8080/rest/profile/parkrunCourses"
 },
 "secrets" : {
 "href" : "http://localhost:8080/rest/profile/secrets"
 }
 }
}

Conclusions

This post looked at four ways to control access to Spring Data REST using RepositoryDetectionStrategies allow or restrict access to the underlying Spring Data JPA repositories. The options are – ALL, ANNOTATED, VISIBILITY or DEFAULT.

REST Web Protocols Spring Data Spring Framework Data (computing)

Published at DZone with permission of Martin Farrell, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Less Code With Spring Data Rest
  • Introduction To Spring Data JPA With Inheritance in A REST Application
  • The Magic of Spring Data
  • Build Reactive REST APIs With Spring WebFlux

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!