Skip to content

Commit 84b124d

Browse files
committed
Merge branch '6.4.x' into 6.5.x
2 parents dd1f097 + fee6a9b commit 84b124d

3 files changed

Lines changed: 73 additions & 2 deletions

File tree

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
@@ -453,6 +453,72 @@ open fun findMessagesForUser(@CurrentUser("user_id") userId: String?): ModelAndV
453453
----
454454
======
455455

456+
[[mvc-current-security-context]]
457+
== @CurrentSecurityContext
458+
459+
Spring Security provides `CurrentSecurityContextArgumentResolver`, which can automatically resolve the current `SecurityContext` for Spring MVC arguments.
460+
By using `@EnableWebSecurity`, you automatically have this added to your Spring MVC configuration.
461+
If you use XML-based configuration, you must add this yourself:
462+
463+
[source,xml]
464+
----
465+
<mvc:annotation-driven>
466+
<mvc:argument-resolvers>
467+
<bean class="org.springframework.security.web.method.annotation.CurrentSecurityContextArgumentResolver" />
468+
</mvc:argument-resolvers>
469+
</mvc:annotation-driven>
470+
----
471+
472+
Once `CurrentSecurityContextArgumentResolver` is configured, you can access the `SecurityContext` directly:
473+
474+
[tabs]
475+
======
476+
Java::
477+
+
478+
[source,java,role="primary"]
479+
----
480+
@GetMapping("/me")
481+
public String me(@CurrentSecurityContext SecurityContext context) {
482+
return context.getAuthentication().getName();
483+
}
484+
----
485+
486+
Kotlin::
487+
+
488+
[source,kotlin,role="secondary"]
489+
----
490+
@GetMapping("/me")
491+
fun me(@CurrentSecurityContext context: SecurityContext): String {
492+
return context.authentication.name
493+
}
494+
----
495+
======
496+
497+
You can also use a SpEL expression that is rooted at the `SecurityContext`:
498+
499+
[tabs]
500+
======
501+
Java::
502+
+
503+
[source,java,role="primary"]
504+
----
505+
@GetMapping("/me")
506+
public String me(@CurrentSecurityContext(expression = "authentication") Authentication authentication) {
507+
return authentication.getName();
508+
}
509+
----
510+
511+
Kotlin::
512+
+
513+
[source,kotlin,role="secondary"]
514+
----
515+
@GetMapping("/me")
516+
fun me(@CurrentSecurityContext(expression = "authentication") authentication: Authentication): String {
517+
return authentication.name
518+
}
519+
----
520+
======
521+
456522
[[mvc-async]]
457523
== Spring MVC Async Integration
458524

0 commit comments

Comments
 (0)