concept Spring Data in category spring

appears as: Spring Data
Spring in Action, Sixth Edition

This is an excerpt from Manning's book Spring in Action, Sixth Edition.

Although the core Spring Framework comes with basic data persistence support, Spring Data provides something quite amazing: the ability to define your application’s data repositories as simple Java interfaces, using a naming convention when defining methods to drive how data is stored and retrieved.

What’s more, Spring Data is capable of working with several different kinds of databases, including relational (via JDBC or JPA), document (Mongo), graph (Neo4j), and others. You’ll use Spring Data to help create repositories for the Taco Cloud application in chapter 3.

One of the most interesting and useful features provided by Spring Data for all of these projects is the ability to automatically create repositories, based on a repository specification interface. Consequently, persistence with Spring Data projects have little or no persistence logic and involve only writing one or more repository interfaces.

Figure 3.2 illustrates how Spring Data parses and understands the readOrdersByDeliveryZipAndPlacedAtBetween() method when generating the repository implementation. As you can see, the verb in readOrdersByDeliveryZipAndPlacedAtBetween() is read. Spring Data also understands find, read, and get as synonymous for fetching one or more entities. Alternatively, you can also use count as the verb if you only want the method to return an int with the count of matching entities.

Figure 3.2. Spring Data parses repository method signatures to determine the query that should be performed.
3 2

Although the subject of the method is optional, here it says Orders. Spring Data ignores most words in a subject, so you could name the method readPuppiesBy… and it would still find TacoOrder entities, as that is the type that CrudRepository is parameterized with.

Spring Security in Action

This is an excerpt from Manning's book Spring Security in Action.

Throughout the book, we’ll refer to vulnerabilities. We’ll discuss vulnerabilities starting with the basic configuration of the authentication and authorization, in chapter 3. Then, we’ll discuss how vulnerabilities relate to the integration of Spring Security and Spring Data and how to design the application to avoid those with OAuth2.

Figure 6.1. The authentication flow in the hands-on application. The custom authentication provider will implement the authentication logic. For this, the authentication provider uses a UserDetailsService implementation and two PasswordEncoder implementations, one for each requested hashing algorithm. The UserDetailsService implementation, called JpaUserDetailsService uses Spring Data and JPA to work with the database and obtain the UserDetails.

In this section, we discuss filtering applied with Spring Data repositories. It’s important to understand this approach because we very often use databases to persist the data of our applications. It is pretty common nowadays to implement Spring Boot applications that use Spring Data as a high-level layer to connect to a database, be it SQL or NoSQL. We discuss two approaches for applying filtering at the repository level when using Spring Data, and we implement them in examples. The first approach we take is the one you’ve learned up to now in the chapter: using the @PreFilter and @PostFilter annotations. The second approach we’ll discuss is the direct integration of the authorization rules in queries. As you’ll learn in this section, you have to be attentive when choosing the way you apply filtering at the Spring Data repositories.

Let’s start our discussion on using filtering with Spring Data. As mentioned, we have two options: