Skip to content

Commit 6d61b87

Browse files
committed
Add security context holder Kotlin samples to docs
Issue gh-8172
1 parent 8e5e0c4 commit 6d61b87

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

docs/manual/src/docs/asciidoc/_includes/servlet/authentication/architecture/security-context-holder.adoc

+24-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ The simplest way to indicate a user is authenticated is to set the `SecurityCont
1616

1717
.Setting `SecurityContextHolder`
1818
====
19-
[source,java]
19+
.Java
20+
[source,java,role="primary"]
2021
----
2122
SecurityContext context = SecurityContextHolder.createEmptyContext(); // <1>
2223
Authentication authentication =
@@ -25,6 +26,16 @@ context.setAuthentication(authentication);
2526
2627
SecurityContextHolder.setContext(context); // <3>
2728
----
29+
30+
.Kotlin
31+
[source,kotlin,role="secondary"]
32+
----
33+
val context: SecurityContext = SecurityContextHolder.createEmptyContext() // <1>
34+
val authentication: Authentication = TestingAuthenticationToken("username", "password", "ROLE_USER") // <2>
35+
context.authentication = authentication
36+
37+
SecurityContextHolder.setContext(context) // <3>
38+
----
2839
====
2940

3041
<1> We start by creating an empty `SecurityContext`.
@@ -40,14 +51,25 @@ If you wish to obtain information about the authenticated principal, you can do
4051

4152
.Access Currently Authenticated User
4253
====
43-
[source,java]
54+
.Java
55+
[source,java,role="primary"]
4456
----
4557
SecurityContext context = SecurityContextHolder.getContext();
4658
Authentication authentication = context.getAuthentication();
4759
String username = authentication.getName();
4860
Object principal = authentication.getPrincipal();
4961
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
5062
----
63+
64+
.Kotlin
65+
[source,kotlin,role="secondary"]
66+
----
67+
val context = SecurityContextHolder.getContext()
68+
val authentication = context.authentication
69+
val username = authentication.name
70+
val principal = authentication.principal
71+
val authorities = authentication.authorities
72+
----
5173
====
5274

5375
// FIXME: add links to HttpServletRequest.getRemoteUser() and @CurrentSecurityContext @AuthenticationPrincipal

0 commit comments

Comments
 (0)