Skip to content

Commit 1f39a3d

Browse files
committed
Merge branch '6.5.x' into 7.0.x
2 parents d2ed832 + 84b124d commit 1f39a3d

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
@@ -448,6 +448,72 @@ open fun findMessagesForUser(@CurrentUser("user_id") userId: String?): ModelAndV
448448
----
449449
======
450450

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

0 commit comments

Comments
 (0)