Add security context holder Kotlin samples to docs
Issue gh-8172
This commit is contained in:
parent
8e5e0c4a9e
commit
6d61b87213
|
@ -16,7 +16,8 @@ The simplest way to indicate a user is authenticated is to set the `SecurityCont
|
||||||
|
|
||||||
.Setting `SecurityContextHolder`
|
.Setting `SecurityContextHolder`
|
||||||
====
|
====
|
||||||
[source,java]
|
.Java
|
||||||
|
[source,java,role="primary"]
|
||||||
----
|
----
|
||||||
SecurityContext context = SecurityContextHolder.createEmptyContext(); // <1>
|
SecurityContext context = SecurityContextHolder.createEmptyContext(); // <1>
|
||||||
Authentication authentication =
|
Authentication authentication =
|
||||||
|
@ -25,6 +26,16 @@ context.setAuthentication(authentication);
|
||||||
|
|
||||||
SecurityContextHolder.setContext(context); // <3>
|
SecurityContextHolder.setContext(context); // <3>
|
||||||
----
|
----
|
||||||
|
|
||||||
|
.Kotlin
|
||||||
|
[source,kotlin,role="secondary"]
|
||||||
|
----
|
||||||
|
val context: SecurityContext = SecurityContextHolder.createEmptyContext() // <1>
|
||||||
|
val authentication: Authentication = TestingAuthenticationToken("username", "password", "ROLE_USER") // <2>
|
||||||
|
context.authentication = authentication
|
||||||
|
|
||||||
|
SecurityContextHolder.setContext(context) // <3>
|
||||||
|
----
|
||||||
====
|
====
|
||||||
|
|
||||||
<1> We start by creating an empty `SecurityContext`.
|
<1> We start by creating an empty `SecurityContext`.
|
||||||
|
@ -40,7 +51,8 @@ If you wish to obtain information about the authenticated principal, you can do
|
||||||
|
|
||||||
.Access Currently Authenticated User
|
.Access Currently Authenticated User
|
||||||
====
|
====
|
||||||
[source,java]
|
.Java
|
||||||
|
[source,java,role="primary"]
|
||||||
----
|
----
|
||||||
SecurityContext context = SecurityContextHolder.getContext();
|
SecurityContext context = SecurityContextHolder.getContext();
|
||||||
Authentication authentication = context.getAuthentication();
|
Authentication authentication = context.getAuthentication();
|
||||||
|
@ -48,6 +60,16 @@ String username = authentication.getName();
|
||||||
Object principal = authentication.getPrincipal();
|
Object principal = authentication.getPrincipal();
|
||||||
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
|
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
|
||||||
----
|
----
|
||||||
|
|
||||||
|
.Kotlin
|
||||||
|
[source,kotlin,role="secondary"]
|
||||||
|
----
|
||||||
|
val context = SecurityContextHolder.getContext()
|
||||||
|
val authentication = context.authentication
|
||||||
|
val username = authentication.name
|
||||||
|
val principal = authentication.principal
|
||||||
|
val authorities = authentication.authorities
|
||||||
|
----
|
||||||
====
|
====
|
||||||
|
|
||||||
// FIXME: add links to HttpServletRequest.getRemoteUser() and @CurrentSecurityContext @AuthenticationPrincipal
|
// FIXME: add links to HttpServletRequest.getRemoteUser() and @CurrentSecurityContext @AuthenticationPrincipal
|
||||||
|
|
Loading…
Reference in New Issue