From 386e8a86a1c9634177154ad946286b1f62bc62f1 Mon Sep 17 00:00:00 2001 From: Talerngpong Virojwutikul Date: Mon, 14 Feb 2022 23:15:27 +0700 Subject: [PATCH] add Kotlin examples for Spring Data Integration of servlet application --- .../ROOT/pages/servlet/integrations/data.adoc | 34 +++++++++++++++---- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/docs/modules/ROOT/pages/servlet/integrations/data.adoc b/docs/modules/ROOT/pages/servlet/integrations/data.adoc index 6304c2c5c9..35c7403a8f 100644 --- a/docs/modules/ROOT/pages/servlet/integrations/data.adoc +++ b/docs/modules/ROOT/pages/servlet/integrations/data.adoc @@ -2,22 +2,31 @@ = Spring Data Integration Spring Security provides Spring Data integration that allows referring to the current user within your queries. -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. +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. [[data-configuration]] == Spring Data & Spring Security Configuration -To use this support, add the `org.springframework.security:spring-security-data` dependency and provide a bean of type `SecurityEvaluationContextExtension`. -In Java configuration, this would look like: +To use this support, add `org.springframework.security:spring-security-data` dependency and provide a bean of type `SecurityEvaluationContextExtension`: ==== -[source,java] +.Java +[source,java,role="primary"] ---- @Bean public SecurityEvaluationContextExtension securityEvaluationContextExtension() { return new SecurityEvaluationContextExtension(); } ---- + +.Kotlin +[source,kotlin,role="secondary"] +---- +@Bean +fun securityEvaluationContextExtension(): SecurityEvaluationContextExtension { + return SecurityEvaluationContextExtension() +} +---- ==== In XML Configuration, this would look like: @@ -35,7 +44,8 @@ In XML Configuration, this would look like: Now you can use Spring Security within your queries: ==== -[source,java] +.Java +[source,java,role="primary"] ---- @Repository public interface MessageRepository extends PagingAndSortingRepository { @@ -43,8 +53,18 @@ public interface MessageRepository extends PagingAndSortingRepository findInbox(Pageable pageable); } ---- + +.Kotlin +[source,kotlin,role="secondary"] +---- +@Repository +interface MessageRepository : PagingAndSortingRepository { + @Query("select m from Message m where m.to.id = ?#{ principal?.id }") + fun findInbox(pageable: Pageable): Page +} +---- ==== This checks to see if the `Authentication.getPrincipal().getId()` is equal to the recipient of the `Message`. -Note that this example assumes you have customized the principal to be an `Object` that has an `id` property. -By exposing the `SecurityEvaluationContextExtension` bean, all of the xref:servlet/authorization/expression-based.adoc#common-expressions[Common Security Expressions] are available within the query. +Note that this example assumes you have customized the principal to be an Object that has an id property. +By exposing the `SecurityEvaluationContextExtension` bean, all of the xref:servlet/authorization/expression-based.adoc#common-expressions[Common Security Expressions] are available within the Query.