@@ -16,7 +16,8 @@ The simplest way to indicate a user is authenticated is to set the `SecurityCont
16
16
17
17
.Setting `SecurityContextHolder`
18
18
====
19
- [source,java]
19
+ .Java
20
+ [source,java,role="primary"]
20
21
----
21
22
SecurityContext context = SecurityContextHolder.createEmptyContext(); // <1>
22
23
Authentication authentication =
@@ -25,6 +26,16 @@ context.setAuthentication(authentication);
25
26
26
27
SecurityContextHolder.setContext(context); // <3>
27
28
----
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
+ ----
28
39
====
29
40
30
41
<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
40
51
41
52
.Access Currently Authenticated User
42
53
====
43
- [source,java]
54
+ .Java
55
+ [source,java,role="primary"]
44
56
----
45
57
SecurityContext context = SecurityContextHolder.getContext();
46
58
Authentication authentication = context.getAuthentication();
47
59
String username = authentication.getName();
48
60
Object principal = authentication.getPrincipal();
49
61
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
50
62
----
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
+ ----
51
73
====
52
74
53
75
// FIXME: add links to HttpServletRequest.getRemoteUser() and @CurrentSecurityContext @AuthenticationPrincipal
0 commit comments