Skip to content

Commit fee6a9b

Browse files
soooiijzheaux
authored andcommitted
docs: add CurrentSecurityContext section and link references
Signed-off-by: songhee <songhee9327@gmail.com>
1 parent e588a35 commit fee6a9b

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

docs/modules/ROOT/pages/servlet/authentication/anonymous.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ fun method(authentication: Authentication?): String {
137137
will always return "not anonymous", even for anonymous requests.
138138
The reason is that Spring MVC resolves the parameter using `HttpServletRequest#getPrincipal`, which is `null` when the request is anonymous.
139139

140-
If you'd like to obtain the `Authentication` in anonymous requests, use `@CurrentSecurityContext` instead:
140+
If you'd like to obtain the `Authentication` in anonymous requests, use
141+
xref:servlet/integrations/mvc.adoc#mvc-current-security-context[`@CurrentSecurityContext`] instead:
141142

142143
.Use CurrentSecurityContext for Anonymous requests
143144
[tabs]

docs/modules/ROOT/pages/servlet/authentication/architecture.adoc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ val authorities = authentication.authorities
9797
----
9898
======
9999

100-
// FIXME: Add links to and relevant description of HttpServletRequest.getRemoteUser() and @CurrentSecurityContext @AuthenticationPrincipal
100+
In Spring MVC, you can resolve the current principal with
101+
xref:servlet/integrations/mvc.adoc#mvc-authentication-principal[`@AuthenticationPrincipal`]
102+
and the full `SecurityContext` with
103+
xref:servlet/integrations/mvc.adoc#mvc-current-security-context[`@CurrentSecurityContext`].
104+
For Servlet API access, use `HttpServletRequest#getRemoteUser`.
101105

102106
By default, `SecurityContextHolder` uses a `ThreadLocal` to store these details, which means that the `SecurityContext` is always available to methods in the same thread, even if the `SecurityContext` is not explicitly passed around as an argument to those methods.
103107
Using a `ThreadLocal` in this way is quite safe if you take care to clear the thread after the present principal's request is processed.

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,72 @@ open fun findMessagesForUser(@CurrentUser("user_id") userId: String?): ModelAndV
624624
----
625625
======
626626

627+
[[mvc-current-security-context]]
628+
== @CurrentSecurityContext
629+
630+
Spring Security provides `CurrentSecurityContextArgumentResolver`, which can automatically resolve the current `SecurityContext` for Spring MVC arguments.
631+
By using `@EnableWebSecurity`, you automatically have this added to your Spring MVC configuration.
632+
If you use XML-based configuration, you must add this yourself:
633+
634+
[source,xml]
635+
----
636+
<mvc:annotation-driven>
637+
<mvc:argument-resolvers>
638+
<bean class="org.springframework.security.web.method.annotation.CurrentSecurityContextArgumentResolver" />
639+
</mvc:argument-resolvers>
640+
</mvc:annotation-driven>
641+
----
642+
643+
Once `CurrentSecurityContextArgumentResolver` is configured, you can access the `SecurityContext` directly:
644+
645+
[tabs]
646+
======
647+
Java::
648+
+
649+
[source,java,role="primary"]
650+
----
651+
@GetMapping("/me")
652+
public String me(@CurrentSecurityContext SecurityContext context) {
653+
return context.getAuthentication().getName();
654+
}
655+
----
656+
657+
Kotlin::
658+
+
659+
[source,kotlin,role="secondary"]
660+
----
661+
@GetMapping("/me")
662+
fun me(@CurrentSecurityContext context: SecurityContext): String {
663+
return context.authentication.name
664+
}
665+
----
666+
======
667+
668+
You can also use a SpEL expression that is rooted at the `SecurityContext`:
669+
670+
[tabs]
671+
======
672+
Java::
673+
+
674+
[source,java,role="primary"]
675+
----
676+
@GetMapping("/me")
677+
public String me(@CurrentSecurityContext(expression = "authentication") Authentication authentication) {
678+
return authentication.getName();
679+
}
680+
----
681+
682+
Kotlin::
683+
+
684+
[source,kotlin,role="secondary"]
685+
----
686+
@GetMapping("/me")
687+
fun me(@CurrentSecurityContext(expression = "authentication") authentication: Authentication): String {
688+
return authentication.name
689+
}
690+
----
691+
======
692+
627693
[[mvc-async]]
628694
== Spring MVC Async Integration
629695

0 commit comments

Comments
 (0)