I am using Spring Data Rest to expose JPA repositories to our React front-end application. And I also use Spring Security and JWT to protect REST APIs.
For examples, we have Entity and Repository as below:
@Entity
@Table(name = "customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
....
}
public interface CustomerRepository extends PagingAndSortingRepository<Customer, Long> {
}
Spring Data Rest will expose CustomerRepository to REST endpoint /api/customers
So our React application can call REST endpoints to get data. It works well.
However, someone with valid JWT token can use tools like Postman to fetch all customers data from the servers. Of course, we don't want this happen.
My questions:
- How can we prevent such abnormal usage?
- What's the best practice to use Spring Data Rest?