From 6d61b87213b7ea78f2041ce9635fc02557f7d708 Mon Sep 17 00:00:00 2001 From: Eleftheria Stein Date: Thu, 26 Mar 2020 15:36:44 -0400 Subject: [PATCH] Add security context holder Kotlin samples to docs Issue gh-8172 --- .../architecture/security-context-holder.adoc | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/docs/manual/src/docs/asciidoc/_includes/servlet/authentication/architecture/security-context-holder.adoc b/docs/manual/src/docs/asciidoc/_includes/servlet/authentication/architecture/security-context-holder.adoc index 4c1516a1af..db2f4c6d4c 100644 --- a/docs/manual/src/docs/asciidoc/_includes/servlet/authentication/architecture/security-context-holder.adoc +++ b/docs/manual/src/docs/asciidoc/_includes/servlet/authentication/architecture/security-context-holder.adoc @@ -16,7 +16,8 @@ The simplest way to indicate a user is authenticated is to set the `SecurityCont .Setting `SecurityContextHolder` ==== -[source,java] +.Java +[source,java,role="primary"] ---- SecurityContext context = SecurityContextHolder.createEmptyContext(); // <1> Authentication authentication = @@ -25,6 +26,16 @@ context.setAuthentication(authentication); 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`. @@ -40,7 +51,8 @@ If you wish to obtain information about the authenticated principal, you can do .Access Currently Authenticated User ==== -[source,java] +.Java +[source,java,role="primary"] ---- SecurityContext context = SecurityContextHolder.getContext(); Authentication authentication = context.getAuthentication(); @@ -48,6 +60,16 @@ String username = authentication.getName(); Object principal = authentication.getPrincipal(); Collection 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