data.adoc 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. [[data]]
  2. = Spring Data Integration
  3. Spring Security provides Spring Data integration that allows referring to the current user within your queries.
  4. It is not only useful but necessary to include the user in the queries to support paged results, since filtering the results afterwards would not scale.
  5. [[data-configuration]]
  6. == Spring Data & Spring Security Configuration
  7. To use this support, add the `org.springframework.security:spring-security-data` dependency and provide a bean of type `SecurityEvaluationContextExtension`.
  8. In Java configuration, this would look like:
  9. ====
  10. [source,java]
  11. ----
  12. @Bean
  13. public SecurityEvaluationContextExtension securityEvaluationContextExtension() {
  14. return new SecurityEvaluationContextExtension();
  15. }
  16. ----
  17. ====
  18. In XML Configuration, this would look like:
  19. ====
  20. [source,xml]
  21. ----
  22. <bean class="org.springframework.security.data.repository.query.SecurityEvaluationContextExtension"/>
  23. ----
  24. ====
  25. [[data-query]]
  26. == Security Expressions within @Query
  27. Now you can use Spring Security within your queries:
  28. ====
  29. [source,java]
  30. ----
  31. @Repository
  32. public interface MessageRepository extends PagingAndSortingRepository<Message,Long> {
  33. @Query("select m from Message m where m.to.id = ?#{ principal?.id }")
  34. Page<Message> findInbox(Pageable pageable);
  35. }
  36. ----
  37. ====
  38. This checks to see if the `Authentication.getPrincipal().getId()` is equal to the recipient of the `Message`.
  39. Note that this example assumes you have customized the principal to be an `Object` that has an `id` property.
  40. By exposing the `SecurityEvaluationContextExtension` bean, all of the xref:servlet/authorization/expression-based.adoc#common-expressions[Common Security Expressions] are available within the query.