Skip to content

Commit 386e8a8

Browse files
talerngpongmarcusdacoregio
authored andcommitted
add Kotlin examples for Spring Data Integration of servlet application
1 parent 606bd12 commit 386e8a8

File tree

1 file changed

+27
-7
lines changed
  • docs/modules/ROOT/pages/servlet/integrations

1 file changed

+27
-7
lines changed

docs/modules/ROOT/pages/servlet/integrations/data.adoc

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,31 @@
22
= Spring Data Integration
33

44
Spring Security provides Spring Data integration that allows referring to the current user within your queries.
5-
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+
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.
66

77
[[data-configuration]]
88
== Spring Data & Spring Security Configuration
99

10-
To use this support, add the `org.springframework.security:spring-security-data` dependency and provide a bean of type `SecurityEvaluationContextExtension`.
11-
In Java configuration, this would look like:
10+
To use this support, add `org.springframework.security:spring-security-data` dependency and provide a bean of type `SecurityEvaluationContextExtension`:
1211

1312
====
14-
[source,java]
13+
.Java
14+
[source,java,role="primary"]
1515
----
1616
@Bean
1717
public SecurityEvaluationContextExtension securityEvaluationContextExtension() {
1818
return new SecurityEvaluationContextExtension();
1919
}
2020
----
21+
22+
.Kotlin
23+
[source,kotlin,role="secondary"]
24+
----
25+
@Bean
26+
fun securityEvaluationContextExtension(): SecurityEvaluationContextExtension {
27+
return SecurityEvaluationContextExtension()
28+
}
29+
----
2130
====
2231

2332
In XML Configuration, this would look like:
@@ -35,16 +44,27 @@ In XML Configuration, this would look like:
3544
Now you can use Spring Security within your queries:
3645

3746
====
38-
[source,java]
47+
.Java
48+
[source,java,role="primary"]
3949
----
4050
@Repository
4151
public interface MessageRepository extends PagingAndSortingRepository<Message,Long> {
4252
@Query("select m from Message m where m.to.id = ?#{ principal?.id }")
4353
Page<Message> findInbox(Pageable pageable);
4454
}
4555
----
56+
57+
.Kotlin
58+
[source,kotlin,role="secondary"]
59+
----
60+
@Repository
61+
interface MessageRepository : PagingAndSortingRepository<Message,Long> {
62+
@Query("select m from Message m where m.to.id = ?#{ principal?.id }")
63+
fun findInbox(pageable: Pageable): Page<Message>
64+
}
65+
----
4666
====
4767

4868
This checks to see if the `Authentication.getPrincipal().getId()` is equal to the recipient of the `Message`.
49-
Note that this example assumes you have customized the principal to be an `Object` that has an `id` property.
50-
By exposing the `SecurityEvaluationContextExtension` bean, all of the xref:servlet/authorization/expression-based.adoc#common-expressions[Common Security Expressions] are available within the query.
69+
Note that this example assumes you have customized the principal to be an Object that has an id property.
70+
By exposing the `SecurityEvaluationContextExtension` bean, all of the xref:servlet/authorization/expression-based.adoc#common-expressions[Common Security Expressions] are available within the Query.

0 commit comments

Comments
 (0)