Skip to content

Commit 5c88b95

Browse files
Mention that authorizeHttpRequests does not support GrantedAuthorityDefaults
Closes gh-13227
1 parent c1002ff commit 5c88b95

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

docs/modules/ROOT/pages/migration/servlet/authorization.adoc

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,53 @@ public final class AnyRequestAuthenticatedAuthorizationManagerAdapter implements
15451545

15461546
Once you have implemented `AuthorizationManager`, please follow the details in the reference manual for xref:servlet/authorization/authorize-http-requests.adoc#custom-authorization-manager[adding a custom `AuthorizationManager`].
15471547

1548+
[[replace-hasrole-hasauthority]]
1549+
=== Replace `hasRole` with `hasAuthority` if using `GrantedAuthorityDefaults`
1550+
1551+
Currently, the `hasRole` method inside `authorizeHttpRequests` does not support the `GrantedAuthorityDefaults` bean like the `authorizeRequests` does.
1552+
Therefore, if you are using `GrantedAuthorityDefaults` to change the prefix of your roles, you will need to use `hasAuthority` instead of `hasRole`.
1553+
1554+
For example, you will have to change from:
1555+
1556+
====
1557+
.authorizeRequests with custom role prefix
1558+
[source,java]
1559+
----
1560+
@Bean
1561+
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
1562+
http
1563+
.authorizeRequests((authorize) -> authorize
1564+
.anyRequest().hasRole("ADMIN")
1565+
);
1566+
return http.build();
1567+
}
1568+
1569+
@Bean
1570+
public GrantedAuthorityDefaults grantedAuthorityDefaults() {
1571+
return new GrantedAuthorityDefaults("MYPREFIX_");
1572+
}
1573+
----
1574+
====
1575+
1576+
to:
1577+
1578+
====
1579+
.authorizeHttpRequests with hasAuthority and custom role prefix
1580+
[source,java]
1581+
----
1582+
@Bean
1583+
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
1584+
http
1585+
.authorizeHttpRequests((authorize) -> authorize
1586+
.anyRequest().hasAuthority("MYPREFIX_ADMIN")
1587+
);
1588+
return http.build();
1589+
}
1590+
----
1591+
====
1592+
1593+
This should be supported in the future, see https://github.com/spring-projects/spring-security/issues/13215[gh-13227] for more details.
1594+
15481595
[[servlet-authorizationmanager-requests-opt-out]]
15491596
=== Opt-out Steps
15501597

0 commit comments

Comments
 (0)