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