Skip to content

Commit 6bda1d2

Browse files
committed
Document WebExpressionAuthorizationManager
Closes gh-12928
1 parent a708007 commit 6bda1d2

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

docs/modules/ROOT/pages/servlet/authorization/authorize-http-requests.adoc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,3 +413,46 @@ open class SecurityConfig {
413413
<3> Allow access to URLs that start with `/user/` to users with the `USER` role, using `AntPathRequestMatcher`
414414
<4> Allow access to URLs that start with `/admin/` to users with the `ADMIN` role, using `RegexRequestMatcher`
415415
<5> Allow access to URLs that match the `MyCustomRequestMatcher` to users with the `SUPERVISOR` role, using a custom `RequestMatcher`
416+
417+
== Expressions
418+
419+
It is recommended that you use type-safe authorization managers instead of SpEL.
420+
However, `WebExpressionAuthorizationManager` is available to help migrate legacy SpEL.
421+
422+
To use `WebExpressionAuthorizationManager`, you can construct one with the expression you are trying to migrate, like so:
423+
424+
====
425+
.Java
426+
[source,java,role="primary"]
427+
----
428+
.requestMatchers("/test/**").access(new WebExpressionAuthorizationManager("hasRole('ADMIN') && hasRole('USER')"))
429+
----
430+
431+
.Kotlin
432+
[source,kotlin,role="secondary"]
433+
----
434+
.requestMatchers("/test/**").access(WebExpressionAuthorizationManager("hasRole('ADMIN') && hasRole('USER')"))
435+
----
436+
====
437+
438+
If you are referring to a bean in your expression like so: `@webSecurity.check(authentication, request)`, it's recommended that you instead call the bean directly, which will look something like the following:
439+
440+
====
441+
.Java
442+
[source,java,role="primary"]
443+
----
444+
.requestMatchers("/test/**").access((authentication, context) ->
445+
new AuthorizationDecision(webSecurity.check(authentication.get(), context.getRequest())))
446+
----
447+
448+
.Kotlin
449+
[source,kotlin,role="secondary"]
450+
----
451+
.requestMatchers("/test/**").access((authentication, context): AuthorizationManager<RequestAuthorizationContext> ->
452+
AuthorizationDecision(webSecurity.check(authentication.get(), context.getRequest())))
453+
----
454+
====
455+
456+
For complex instructions that include bean references as well as other expressions, it is recommended that you change those to implement `AuthorizationManager` and refer to them by calling `.access(AuthorizationManager)`.
457+
458+
If you are not able to do that, you can configure a `DefaultHttpSecurityExpressionHandler` with a bean resolver and supply that to `WebExpressionAuthorizationManager#setExpressionhandler`.

0 commit comments

Comments
 (0)