From f2b2e6002f517e3ddbe832a4038ca3f6f38e4eff Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Thu, 15 Jul 2021 16:40:10 +0800 Subject: [PATCH] Replace static "ROLE_" with customized role prefix Fix gh-4134 --- .../ExpressionUrlAuthorizationConfigurer.java | 43 ++++-- ...essionUrlAuthorizationConfigurerTests.java | 124 +++++++++++++++++- 2 files changed, 152 insertions(+), 15 deletions(-) diff --git a/config/src/main/java/org/springframework/security/config/annotation/web/configurers/ExpressionUrlAuthorizationConfigurer.java b/config/src/main/java/org/springframework/security/config/annotation/web/configurers/ExpressionUrlAuthorizationConfigurer.java index 949b833da4..1c0499693f 100644 --- a/config/src/main/java/org/springframework/security/config/annotation/web/configurers/ExpressionUrlAuthorizationConfigurer.java +++ b/config/src/main/java/org/springframework/security/config/annotation/web/configurers/ExpressionUrlAuthorizationConfigurer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -76,6 +76,7 @@ import org.springframework.util.StringUtils; * * @param the type of {@link HttpSecurityBuilder} that is being configured * @author Rob Winch + * @author Yanming Zhou * @since 3.2 * @see org.springframework.security.config.annotation.web.builders.HttpSecurity#authorizeRequests() */ @@ -94,6 +95,8 @@ public final class ExpressionUrlAuthorizationConfigurer expressionHandler; @@ -103,6 +106,15 @@ public final class ExpressionUrlAuthorizationConfigurer "role should not start with 'ROLE_' since it is automatically inserted. Got '" + role + "'"); - return "hasRole('ROLE_" + role + "')"; + Assert.isTrue(rolePrefix.isEmpty() || !role.startsWith(rolePrefix), () -> "role should not start with '" + + rolePrefix + "' since it is automatically inserted. Got '" + role + "'"); + return "hasRole('" + rolePrefix + role + "')"; } private static String hasAuthority(String authority) { @@ -308,27 +320,30 @@ public final class ExpressionUrlAuthorizationConfigurer